exampleSvrMain returns the default main function for the given server expression.
(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr, services *service.ServicesData)
| 26 | // exampleSvrMain returns the default main function for the given server |
| 27 | // expression. |
| 28 | func 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 | { |
no test coverage detected