MCPcopy Create free account
hub / github.com/goadesign/goa / serverFile

Function serverFile

jsonrpc/codegen/server.go:80–164  ·  view source on GitHub ↗

serverFile returns the file implementing the HTTP server.

(genpkg string, svc *expr.HTTPServiceExpr, services *httpcodegen.ServicesData)

Source from the content-addressed store, hash-verified

78
79// serverFile returns the file implementing the HTTP server.
80func serverFile(genpkg string, svc *expr.HTTPServiceExpr, services *httpcodegen.ServicesData) *codegen.File {
81 data := services.Get(svc.Name())
82 svcName := data.Service.PathName
83 fpath := filepath.Join(codegen.Gendir, "jsonrpc", svcName, "server", "server.go")
84 title := fmt.Sprintf("%s JSON-RPC server", svc.Name())
85 funcs := map[string]any{
86 "isWebSocketEndpoint": httpcodegen.IsWebSocketEndpoint,
87 "isSSEEndpoint": httpcodegen.IsSSEEndpoint,
88 "lowerInitial": lowerInitial,
89 "hasMixedTransports": func() bool { return hasMixedJSONRPCTransports(svc, services) },
90 }
91 imports := make([]*codegen.ImportSpec, 0, 15+len(data.Service.UserTypeImports))
92 imports = append(imports,
93 &codegen.ImportSpec{Path: "bufio"},
94 &codegen.ImportSpec{Path: "bytes"},
95 &codegen.ImportSpec{Path: "context"},
96 &codegen.ImportSpec{Path: "errors"},
97 &codegen.ImportSpec{Path: "fmt"},
98 &codegen.ImportSpec{Path: "io"},
99 &codegen.ImportSpec{Path: "mime/multipart"},
100 &codegen.ImportSpec{Path: "net/http"},
101 &codegen.ImportSpec{Path: "path"},
102 &codegen.ImportSpec{Path: "strings"},
103 codegen.GoaImport(""),
104 codegen.GoaImport("jsonrpc"),
105 codegen.GoaNamedImport("http", "goahttp"),
106 &codegen.ImportSpec{Path: genpkg + "/" + svcName, Name: data.Service.PkgName},
107 &codegen.ImportSpec{Path: genpkg + "/" + svcName + "/" + "views", Name: data.Service.ViewsPkg},
108 )
109 imports = append(imports, data.Service.UserTypeImports...)
110 sections := []*codegen.SectionTemplate{
111 codegen.Header(title, "server", imports),
112 }
113
114 sections = append(sections,
115 &codegen.SectionTemplate{Name: "jsonrpc-server-struct", Source: jsonrpcTemplates.Read(serverStructT), FuncMap: funcs, Data: data},
116 &codegen.SectionTemplate{Name: "jsonrpc-server-init", Source: jsonrpcTemplates.Read(serverInitT), Data: data, FuncMap: funcs},
117 &codegen.SectionTemplate{Name: "jsonrpc-server-service", Source: jsonrpcTemplates.Read(serverServiceT), Data: data},
118 &codegen.SectionTemplate{Name: "jsonrpc-server-use", Source: jsonrpcTemplates.Read(serverUseT), Data: data},
119 &codegen.SectionTemplate{Name: "jsonrpc-server-method-names", Source: jsonrpcTemplates.Read(serverMethodNamesT), Data: data},
120 )
121
122 // Use appropriate server handler based on transport
123 switch {
124 case hasMixedJSONRPCTransports(svc, services):
125 // For mixed transports, we need a unified handler with content negotiation
126 sections = append(sections, &codegen.SectionTemplate{Name: "jsonrpc-mixed-server-handler", Source: jsonrpcTemplates.Read(mixedServerHandlerT), FuncMap: funcs, Data: data})
127 // Include the standard HTTP handlers that the mixed handler delegates to
128 sections = append(sections, &codegen.SectionTemplate{Name: "jsonrpc-server-handler", Source: jsonrpcTemplates.Read(serverHandlerT), FuncMap: funcs, Data: data})
129 // Also include SSE handler for SSE-specific logic
130 sections = append(sections, &codegen.SectionTemplate{Name: "jsonrpc-sse-server-handler", Source: jsonrpcTemplates.Read(sseServerHandlerT), FuncMap: funcs, Data: data})
131 case hasJSONRPCSSE(svc, services):
132 sections = append(sections, &codegen.SectionTemplate{Name: "jsonrpc-sse-server-handler", Source: jsonrpcTemplates.Read(sseServerHandlerT), FuncMap: funcs, Data: data})
133 case httpcodegen.HasWebSocket(data):
134 sections = append(sections, &codegen.SectionTemplate{Name: "jsonrpc-websocket-server-handler", Source: jsonrpcTemplates.Read(websocketServerHandlerT), FuncMap: funcs, Data: data})
135 default:
136 sections = append(sections, &codegen.SectionTemplate{Name: "jsonrpc-server-handler", Source: jsonrpcTemplates.Read(serverHandlerT), FuncMap: funcs, Data: data})
137 }

Callers 1

ServerFilesFunction · 0.70

Calls 6

hasJSONRPCSSEFunction · 0.85
NameMethod · 0.65
GetMethod · 0.45
HeaderMethod · 0.45
ReadMethod · 0.45

Tested by

no test coverage detected