1
0

main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 database/model and 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, "database/model"),
  22. StructAllow: setOf(
  23. "User",
  24. "Inbound",
  25. "FallbackParentInfo",
  26. "OutboundTraffics",
  27. "InboundClientIps",
  28. "ApiToken",
  29. "HistoryOfSeeders",
  30. "Setting",
  31. "Node",
  32. "CustomGeoResource",
  33. "ClientReverse",
  34. "Client",
  35. "ClientRecord",
  36. "ClientInbound",
  37. "InboundFallback",
  38. ),
  39. AliasAllow: setOf("Protocol"),
  40. Overrides: map[string][]walkOverride{
  41. "Inbound": {
  42. {Field: "Settings", Kind: KindAny},
  43. {Field: "StreamSettings", Kind: KindAny},
  44. {Field: "Sniffing", Kind: KindAny},
  45. },
  46. "ClientRecord": {
  47. {Field: "Reverse", Kind: KindAny},
  48. },
  49. "InboundClientIps": {
  50. {Field: "Ips", Kind: KindAny},
  51. },
  52. },
  53. },
  54. {
  55. Path: resolveRel(root, "web/entity"),
  56. StructAllow: setOf(
  57. "Msg",
  58. "AllSetting",
  59. "AllSettingView",
  60. ),
  61. },
  62. {
  63. Path: resolveRel(root, "xray"),
  64. StructAllow: setOf(
  65. "ClientTraffic",
  66. ),
  67. },
  68. }
  69. schemas, aliases, err := walkPackages(requests)
  70. if err != nil {
  71. return err
  72. }
  73. schemas = flattenEmbedded(schemas)
  74. if len(schemas) == 0 {
  75. return fmt.Errorf("no schemas produced; nothing to write")
  76. }
  77. target := filepath.Join(root, outDir)
  78. if err := os.MkdirAll(target, 0o755); err != nil {
  79. return err
  80. }
  81. zodBuf := &bytes.Buffer{}
  82. if err := emitZod(zodBuf, schemas, aliases); err != nil {
  83. return err
  84. }
  85. typesBuf := &bytes.Buffer{}
  86. if err := emitTypes(typesBuf, schemas, aliases); err != nil {
  87. return err
  88. }
  89. if err := os.WriteFile(filepath.Join(target, "zod.ts"), zodBuf.Bytes(), 0o644); err != nil {
  90. return err
  91. }
  92. if err := os.WriteFile(filepath.Join(target, "types.ts"), typesBuf.Bytes(), 0o644); err != nil {
  93. return err
  94. }
  95. fmt.Printf("openapigen: wrote %d schemas to %s\n", len(schemas), target)
  96. return nil
  97. }
  98. func setOf(names ...string) map[string]bool {
  99. m := make(map[string]bool, len(names))
  100. for _, n := range names {
  101. m[n] = true
  102. }
  103. return m
  104. }