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

Function exampleSvrMain

codegen/example/example_server.go:28–158  ·  view source on GitHub ↗

exampleSvrMain returns the default main function for the given server expression.

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

Source from the content-addressed store, hash-verified

26// exampleSvrMain returns the default main function for the given server
27// expression.
28func exampleSvrMain(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr, services *service.ServicesData) *codegen.File {
29 svrdata := Servers.Get(svr, root)
30 mainPath := filepath.Join("cmd", svrdata.Dir, "main.go")
31 if _, err := os.Stat(mainPath); !os.IsNotExist(err) {
32 return nil // file already exists, skip it.
33 }
34 specs := []*codegen.ImportSpec{
35 {Path: "context"},
36 {Path: "flag"},
37 {Path: "fmt"},
38 {Path: "net"},
39 {Path: "net/url"},
40 {Path: "os"},
41 {Path: "os/signal"},
42 {Path: "strings"},
43 {Path: "sync"},
44 {Path: "syscall"},
45 {Path: "time"},
46 {Path: "goa.design/clue/debug"},
47 {Path: "goa.design/clue/log"},
48 }
49
50 // Iterate through services listed in the server expression.
51 svcData := make([]*service.Data, len(svr.Services))
52 scope := codegen.NewNameScope()
53 hasInterceptors := false
54 for i, svc := range svr.Services {
55 sd := services.Get(svc)
56 svcData[i] = sd
57 specs = append(specs, &codegen.ImportSpec{
58 Path: path.Join(genpkg, sd.PathName),
59 Name: scope.Unique(sd.PkgName, "svc"),
60 })
61 hasInterceptors = hasInterceptors || len(sd.ServerInterceptors) > 0
62 }
63 interPkg := scope.Unique("interceptors", "ex")
64
65 var (
66 rootPath string
67 apiPkg string
68 )
69 {
70 // genpkg is created by path.Join so the separator is / regardless of operating system
71 idx := strings.LastIndex(genpkg, string("/"))
72 rootPath = "."
73 if idx > 0 {
74 rootPath = genpkg[:idx]
75 }
76 apiPkg = scope.Unique(strings.ToLower(codegen.Goify(root.API.Name, false)), "api")
77 }
78 specs = append(specs, &codegen.ImportSpec{Path: rootPath, Name: apiPkg})
79 if hasInterceptors {
80 specs = append(specs, &codegen.ImportSpec{Path: path.Join(rootPath, "interceptors"), Name: interPkg})
81 }
82
83 sections := []*codegen.SectionTemplate{
84 codegen.Header("", "main", specs),
85 {

Callers 1

ServerFilesFunction · 0.85

Calls 7

UniqueMethod · 0.95
NewNameScopeFunction · 0.92
GoifyFunction · 0.92
HeaderFunction · 0.92
hasJSONRPCEndpointsFunction · 0.85
GetMethod · 0.45
ReadMethod · 0.45

Tested by

no test coverage detected