main.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package main
  2. import (
  3. "bytes"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. )
  9. func main() {
  10. root := flag.String("root", ".", "repository root containing internal/database/model and internal/web/entity")
  11. outDir := flag.String("out", "frontend/src/generated", "output directory relative to root")
  12. flag.Parse()
  13. if err := run(*root, *outDir); err != nil {
  14. fmt.Fprintln(os.Stderr, "openapigen:", err)
  15. os.Exit(1)
  16. }
  17. }
  18. func run(root, outDir string) error {
  19. requests := []packageRequest{
  20. {
  21. Path: resolveRel(root, "internal/database/model"),
  22. StructAllow: setOf(
  23. "User",
  24. "Inbound",
  25. "FallbackParentInfo",
  26. "OutboundTraffics",
  27. "InboundClientIps",
  28. "ApiToken",
  29. "HistoryOfSeeders",
  30. "Setting",
  31. "Node",
  32. "ClientReverse",
  33. "Client",
  34. "ClientRecord",
  35. "ClientInbound",
  36. "InboundFallback",
  37. "Host",
  38. "SubBalancer",
  39. ),
  40. AliasAllow: setOf("Protocol"),
  41. Overrides: map[string][]walkOverride{
  42. "Inbound": {
  43. {Field: "Settings", Kind: KindAny},
  44. {Field: "StreamSettings", Kind: KindAny},
  45. {Field: "Sniffing", Kind: KindAny},
  46. },
  47. "ClientRecord": {
  48. {Field: "Reverse", Kind: KindAny},
  49. },
  50. "InboundClientIps": {
  51. {Field: "Ips", Kind: KindAny},
  52. },
  53. "Host": {
  54. {Field: "MuxParams", Kind: KindAny},
  55. {Field: "SockoptParams", Kind: KindAny},
  56. },
  57. },
  58. },
  59. {
  60. Path: resolveRel(root, "internal/web/entity"),
  61. StructAllow: setOf(
  62. "Msg",
  63. "AllSetting",
  64. "AllSettingView",
  65. "HostGroup",
  66. ),
  67. },
  68. {
  69. Path: resolveRel(root, "internal/xray"),
  70. StructAllow: setOf(
  71. "ClientTraffic",
  72. ),
  73. },
  74. {
  75. Path: resolveRel(root, "internal/xray/geodata"),
  76. StructAllow: setOf(
  77. "GeoFile",
  78. "GeoCategory",
  79. "GeoEntry",
  80. "GeoCategoryPage",
  81. "GeoEntryPage",
  82. ),
  83. AliasAllow: setOf("GeoKind"),
  84. },
  85. {
  86. Path: resolveRel(root, "internal/web/service"),
  87. StructAllow: setOf(
  88. "InboundOption",
  89. "NodeMutationRequest",
  90. "NodeView",
  91. "ProbeResultUI",
  92. "RealityScanResult",
  93. "GeodataTokenIssue",
  94. ),
  95. },
  96. {
  97. Path: resolveRel(root, "internal/web/service/panel"),
  98. StructAllow: setOf("ApiTokenView", "PanelUpdateStatus"),
  99. },
  100. }
  101. schemas, aliases, err := walkPackages(requests)
  102. if err != nil {
  103. return err
  104. }
  105. schemas = flattenEmbedded(schemas)
  106. if len(schemas) == 0 {
  107. return fmt.Errorf("no schemas produced; nothing to write")
  108. }
  109. target := filepath.Join(root, outDir)
  110. if err := os.MkdirAll(target, 0o755); err != nil {
  111. return err
  112. }
  113. zodBuf := &bytes.Buffer{}
  114. if err := emitZod(zodBuf, schemas, aliases); err != nil {
  115. return err
  116. }
  117. typesBuf := &bytes.Buffer{}
  118. if err := emitTypes(typesBuf, schemas, aliases); err != nil {
  119. return err
  120. }
  121. examplesBuf := &bytes.Buffer{}
  122. if err := emitExamples(examplesBuf, schemas, aliases); err != nil {
  123. return err
  124. }
  125. schemasBuf := &bytes.Buffer{}
  126. if err := emitJSONSchema(schemasBuf, schemas, aliases); err != nil {
  127. return err
  128. }
  129. if err := os.WriteFile(filepath.Join(target, "zod.ts"), zodBuf.Bytes(), 0o644); err != nil {
  130. return err
  131. }
  132. if err := os.WriteFile(filepath.Join(target, "types.ts"), typesBuf.Bytes(), 0o644); err != nil {
  133. return err
  134. }
  135. if err := os.WriteFile(filepath.Join(target, "examples.ts"), examplesBuf.Bytes(), 0o644); err != nil {
  136. return err
  137. }
  138. if err := os.WriteFile(filepath.Join(target, "schemas.ts"), schemasBuf.Bytes(), 0o644); err != nil {
  139. return err
  140. }
  141. fmt.Printf("openapigen: wrote %d schemas to %s\n", len(schemas), target)
  142. return nil
  143. }
  144. func setOf(names ...string) map[string]bool {
  145. m := make(map[string]bool, len(names))
  146. for _, n := range names {
  147. m[n] = true
  148. }
  149. return m
  150. }