exampleServer returns an example gRPC server implementation.
(genpkg string, services *ServicesData, svr *expr.ServerExpr)
| 24 | |
| 25 | // exampleServer returns an example gRPC server implementation. |
| 26 | func 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 |
no test coverage detected