main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. "AmneziaWGLogs",
  95. "PeerActivity",
  96. ),
  97. },
  98. {
  99. Path: resolveRel(root, "internal/web/service/panel"),
  100. StructAllow: setOf("ApiTokenView", "PanelUpdateStatus"),
  101. },
  102. {
  103. Path: resolveRel(root, "internal/amneziawg"),
  104. StructAllow: setOf("ServerSettings"),
  105. },
  106. }
  107. schemas, aliases, err := walkPackages(requests)
  108. if err != nil {
  109. return err
  110. }
  111. schemas = flattenEmbedded(schemas)
  112. if len(schemas) == 0 {
  113. return fmt.Errorf("no schemas produced; nothing to write")
  114. }
  115. target := filepath.Join(root, outDir)
  116. if err := os.MkdirAll(target, 0o755); err != nil {
  117. return err
  118. }
  119. zodBuf := &bytes.Buffer{}
  120. if err := emitZod(zodBuf, schemas, aliases); err != nil {
  121. return err
  122. }
  123. typesBuf := &bytes.Buffer{}
  124. if err := emitTypes(typesBuf, schemas, aliases); err != nil {
  125. return err
  126. }
  127. examplesBuf := &bytes.Buffer{}
  128. if err := emitExamples(examplesBuf, schemas, aliases); err != nil {
  129. return err
  130. }
  131. schemasBuf := &bytes.Buffer{}
  132. if err := emitJSONSchema(schemasBuf, schemas, aliases); err != nil {
  133. return err
  134. }
  135. if err := os.WriteFile(filepath.Join(target, "zod.ts"), zodBuf.Bytes(), 0o644); err != nil {
  136. return err
  137. }
  138. if err := os.WriteFile(filepath.Join(target, "types.ts"), typesBuf.Bytes(), 0o644); err != nil {
  139. return err
  140. }
  141. if err := os.WriteFile(filepath.Join(target, "examples.ts"), examplesBuf.Bytes(), 0o644); err != nil {
  142. return err
  143. }
  144. if err := os.WriteFile(filepath.Join(target, "schemas.ts"), schemasBuf.Bytes(), 0o644); err != nil {
  145. return err
  146. }
  147. fmt.Printf("openapigen: wrote %d schemas to %s\n", len(schemas), target)
  148. return nil
  149. }
  150. func setOf(names ...string) map[string]bool {
  151. m := make(map[string]bool, len(names))
  152. for _, n := range names {
  153. m[n] = true
  154. }
  155. return m
  156. }