(genpkg string, data *httpcodegen.ServicesData, svr *expr.ServerExpr, files []*codegen.File)
| 23 | } |
| 24 | |
| 25 | func exampleServer(genpkg string, data *httpcodegen.ServicesData, svr *expr.ServerExpr, files []*codegen.File) *codegen.File { |
| 26 | svrdata := example.Servers.Get(svr, data.Root) |
| 27 | httppath := filepath.Join("cmd", svrdata.Dir, "http.go") |
| 28 | |
| 29 | // Retrieve existing HTTP server file or create a new one |
| 30 | var file *codegen.File |
| 31 | var hasHTTP bool |
| 32 | for _, f := range files { |
| 33 | if f.Path == httppath { |
| 34 | file = f |
| 35 | hasHTTP = true |
| 36 | break |
| 37 | } |
| 38 | } |
| 39 | if file == nil { |
| 40 | file = httpcodegen.ExampleServer(genpkg, data.Root, svr, data) |
| 41 | updateHeader(file) |
| 42 | } |
| 43 | |
| 44 | // Add JSON-RPC imports to the HTTP server file |
| 45 | header := file.SectionTemplates[0] |
| 46 | scope := codegen.NewNameScope() |
| 47 | for _, svc := range data.Root.API.JSONRPC.Services { |
| 48 | sd := data.Get(svc.Name()) |
| 49 | svcName := sd.Service.PathName |
| 50 | codegen.AddImport(header, &codegen.ImportSpec{ |
| 51 | Path: path.Join(genpkg, svcName), |
| 52 | Name: scope.Unique(sd.Service.PkgName), |
| 53 | }) |
| 54 | codegen.AddImport(header, &codegen.ImportSpec{ |
| 55 | Path: path.Join(genpkg, "jsonrpc", svcName, "server"), |
| 56 | Name: scope.Unique(sd.Service.PkgName + "jssvr"), |
| 57 | }) |
| 58 | } |
| 59 | |
| 60 | // Add JSON-RPC to the HTTP server file |
| 61 | var svcdata []*httpcodegen.ServiceData |
| 62 | for _, svc := range svr.Services { |
| 63 | if d := data.Get(svc); d != nil { |
| 64 | svcdata = append(svcdata, d) |
| 65 | } |
| 66 | } |
| 67 | sections := make([]*codegen.SectionTemplate, 0, len(file.SectionTemplates)+2) |
| 68 | for _, s := range file.SectionTemplates { |
| 69 | switch s.Name { |
| 70 | case "server-http-start": |
| 71 | // Check if the main template already has JSONRPCServices data |
| 72 | data := s.Data.(map[string]any) |
| 73 | if _, hasJSONRPCServices := data["JSONRPCServices"]; !hasJSONRPCServices { |
| 74 | // Main template doesn't have JSON-RPC services, so we need to add them |
| 75 | data["JSONRPCServices"] = svcdata |
| 76 | // Replace with JSON-RPC template that includes service parameters in function signature |
| 77 | s.Source = jsonrpcTemplates.Read(serverHttpStartT) |
| 78 | } |
| 79 | case "server-http-end": |
| 80 | updateData(s, svcdata, hasHTTP) |
| 81 | mountCode := logJSONRPCMount |
| 82 | if hasHTTP { |
no test coverage detected