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

Function exampleServer

grpc/codegen/example_server.go:26–124  ·  view source on GitHub ↗

exampleServer returns an example gRPC server implementation.

(genpkg string, services *ServicesData, svr *expr.ServerExpr)

Source from the content-addressed store, hash-verified

24
25// exampleServer returns an example gRPC server implementation.
26func exampleServer(genpkg string, services *ServicesData, svr *expr.ServerExpr) *codegen.File {
27 var (
28 mainPath string
29
30 svrdata = example.Servers.Get(svr, services.Root)
31 )
32 mainPath = filepath.Join("cmd", svrdata.Dir, "grpc.go")
33 if _, err := os.Stat(mainPath); !os.IsNotExist(err) {
34 return nil // file already exists, skip it.
35 }
36
37 var scope = codegen.NewNameScope()
38
39 specs := []*codegen.ImportSpec{
40 {Path: "context"},
41 {Path: "fmt"},
42 {Path: "net"},
43 {Path: "net/url"},
44 {Path: "sync"},
45 codegen.GoaNamedImport("grpc", "goagrpc"),
46 {Path: "goa.design/clue/debug"},
47 {Path: "goa.design/clue/log"},
48 {Path: "google.golang.org/grpc"},
49 {Path: "google.golang.org/grpc/reflection"},
50 }
51 for _, svc := range services.Root.API.GRPC.Services {
52 sd := services.Get(svc.Name())
53 svcName := sd.Service.PathName
54 specs = append(specs,
55 &codegen.ImportSpec{
56 Path: path.Join(genpkg, "grpc", svcName, "server"),
57 Name: scope.Unique(sd.Service.PkgName + "svr"),
58 },
59 &codegen.ImportSpec{
60 Path: path.Join(genpkg, svcName),
61 Name: scope.Unique(sd.Service.PkgName),
62 },
63 &codegen.ImportSpec{
64 Path: path.Join(genpkg, "grpc", svcName, pbPkgName),
65 Name: scope.Unique(svcName + pbPkgName),
66 })
67 }
68
69 var (
70 rootPath string
71 apiPkg string
72 )
73 // genpkg is created by path.Join so the separator is / regardless of operating system
74 idx := strings.LastIndex(genpkg, string("/"))
75 rootPath = "."
76 if idx > 0 {
77 rootPath = genpkg[:idx]
78 }
79 apiPkg = scope.Unique(strings.ToLower(codegen.Goify(services.Root.API.Name, false)), "api")
80 specs = append(specs, &codegen.ImportSpec{Path: rootPath, Name: apiPkg})
81
82 var (
83 sections []*codegen.SectionTemplate

Callers 1

ExampleServerFilesFunction · 0.70

Calls 8

NewNameScopeFunction · 0.92
GoaNamedImportFunction · 0.92
GoifyFunction · 0.92
HeaderFunction · 0.92
UniqueMethod · 0.80
NameMethod · 0.65
GetMethod · 0.45
ReadMethod · 0.45

Tested by

no test coverage detected