ExampleServer returns an example HTTP server implementation.
(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr, services *ServicesData)
| 29 | |
| 30 | // ExampleServer returns an example HTTP server implementation. |
| 31 | func ExampleServer(genpkg string, root *expr.RootExpr, svr *expr.ServerExpr, services *ServicesData) *codegen.File { |
| 32 | svrdata := example.Servers.Get(svr, root) |
| 33 | fpath := filepath.Join("cmd", svrdata.Dir, "http.go") |
| 34 | specs := make([]*codegen.ImportSpec, 0, 12+2*len(root.API.HTTP.Services)) |
| 35 | baseSpecs := []*codegen.ImportSpec{ |
| 36 | {Path: "context"}, |
| 37 | {Path: "net/http"}, |
| 38 | {Path: "net/url"}, |
| 39 | {Path: "os"}, |
| 40 | {Path: "sync"}, |
| 41 | {Path: "time"}, |
| 42 | codegen.GoaNamedImport("http", "goahttp"), |
| 43 | {Path: "goa.design/clue/debug"}, |
| 44 | {Path: "goa.design/clue/log"}, |
| 45 | codegen.GoaImport("middleware"), |
| 46 | {Path: "github.com/gorilla/websocket"}, |
| 47 | } |
| 48 | specs = append(specs, baseSpecs...) |
| 49 | |
| 50 | scope := codegen.NewNameScope() |
| 51 | for _, svc := range root.API.HTTP.Services { |
| 52 | sd := services.Get(svc.Name()) |
| 53 | svcName := sd.Service.PathName |
| 54 | specs = append(specs, |
| 55 | &codegen.ImportSpec{ |
| 56 | Path: path.Join(genpkg, "http", 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 | } |
| 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(services.Root.API.Name, false) + "api")) |
| 77 | } |
| 78 | specs = append(specs, &codegen.ImportSpec{Path: rootPath, Name: apiPkg}) |
| 79 | |
| 80 | var svcdata []*ServiceData |
| 81 | for _, svc := range svr.Services { |
| 82 | if data := services.Get(svc); data != nil { |
| 83 | svcdata = append(svcdata, data) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | sections := []*codegen.SectionTemplate{ |
| 88 | codegen.Header("", "main", specs), |
no test coverage detected