ServerFiles returns the generated JSON-RPC server files if any.
(genpkg string, data *httpcodegen.ServicesData)
| 12 | |
| 13 | // ServerFiles returns the generated JSON-RPC server files if any. |
| 14 | func ServerFiles(genpkg string, data *httpcodegen.ServicesData) []*codegen.File { |
| 15 | jsvcs := data.Root.API.JSONRPC.Services |
| 16 | files := make([]*codegen.File, 0, len(jsvcs)*3) |
| 17 | for _, svc := range jsvcs { |
| 18 | files = append(files, serverFile(genpkg, svc, data)) |
| 19 | // Generate either WebSocket or SSE file based on transport type |
| 20 | if hasJSONRPCSSE(svc, data) { |
| 21 | if f := sseServerStreamFile(genpkg, svc, data); f != nil { |
| 22 | files = append(files, f) |
| 23 | } |
| 24 | } else if f := websocketServerFile(genpkg, svc, data); f != nil { |
| 25 | files = append(files, f) |
| 26 | } |
| 27 | } |
| 28 | for _, svc := range jsvcs { |
| 29 | f := httpcodegen.ServerEncodeDecodeFile(genpkg, svc, data) |
| 30 | if f == nil { |
| 31 | continue |
| 32 | } |
| 33 | updateHeader(f) |
| 34 | var sections []*codegen.SectionTemplate |
| 35 | for _, s := range f.SectionTemplates { |
| 36 | // Add the JSON-RPC imports. |
| 37 | if s.Name == "source-header" { |
| 38 | codegen.AddImport(s, &codegen.ImportSpec{Path: "bytes"}) |
| 39 | codegen.AddImport(s, &codegen.ImportSpec{Path: "io"}) |
| 40 | codegen.AddImport(s, codegen.GoaImport("jsonrpc")) |
| 41 | } |
| 42 | // Replace HTTP request decoder with proper JSON-RPC version |
| 43 | if s.Name == "request-decoder" { |
| 44 | // Surgical modification 1: Update function signatures for JSON-RPC |
| 45 | s.Source = strings.Replace(s.Source, |
| 46 | "func(*http.Request) (", |
| 47 | "func(*http.Request, *jsonrpc.RawRequest) (", 1) |
| 48 | |
| 49 | // Surgical modification 2: Inject JSON-RPC body handling + signature |
| 50 | s.Source = strings.Replace(s.Source, |
| 51 | "return func(r *http.Request) ({{ .Payload.Ref }}, error) {", |
| 52 | `return func(r *http.Request, req *jsonrpc.RawRequest) ({{ .Payload.Ref }}, error) { |
| 53 | r.Body = io.NopCloser(bytes.NewReader(req.Params))`, 1) |
| 54 | |
| 55 | // Surgical modification 3: Fix return values (nil -> zero values) |
| 56 | s.Source = strings.ReplaceAll(s.Source, |
| 57 | "return nil, ", |
| 58 | `var zero {{ .Payload.Ref }} |
| 59 | return zero, `) |
| 60 | |
| 61 | s.Name = "jsonrpc-request-decoder" |
| 62 | sections = append(sections, s) |
| 63 | continue |
| 64 | } |
| 65 | // Remove the error encoder sections, JSON-RPC |
| 66 | // inlines the error encoding in each handler. |
| 67 | if s.Name != "error-encoder" { |
| 68 | s.Name = "jsonrpc-" + s.Name |
| 69 | sections = append(sections, s) |
| 70 | } |
| 71 | } |