main.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "os"
  7. "os/exec"
  8. "os/signal"
  9. "github.com/spf13/pflag"
  10. )
  11. func startViteServer(
  12. ctx context.Context,
  13. root string,
  14. port int,
  15. ) error {
  16. c := exec.CommandContext(
  17. ctx,
  18. "node_modules/.bin/vite",
  19. "--clearScreen=false",
  20. fmt.Sprintf("--port=%d", port))
  21. c.Dir = root
  22. c.Stdout = os.Stdout
  23. c.Stderr = os.Stderr
  24. return c.Start()
  25. }
  26. func startGoServer(
  27. ctx context.Context,
  28. root string,
  29. proxyURL string,
  30. ) error {
  31. c := exec.CommandContext(
  32. ctx,
  33. "bin/go",
  34. "--host=http://go",
  35. fmt.Sprintf("--asset-proxy-url=%s", proxyURL))
  36. c.Dir = root
  37. c.Stdout = os.Stdout
  38. c.Stderr = os.Stderr
  39. return c.Start()
  40. }
  41. func main() {
  42. var flags struct {
  43. Root string
  44. Vite struct {
  45. Port int
  46. }
  47. }
  48. pflag.StringVar(
  49. &flags.Root,
  50. "root",
  51. ".",
  52. "Root directory for the server")
  53. pflag.IntVar(
  54. &flags.Vite.Port,
  55. "vite.port",
  56. 3000,
  57. "Port for the vite server")
  58. pflag.Parse()
  59. ctx, done := signal.NotifyContext(context.Background(), os.Interrupt)
  60. defer done()
  61. if err := startViteServer(
  62. ctx,
  63. flags.Root,
  64. flags.Vite.Port,
  65. ); err != nil {
  66. log.Panic(err)
  67. }
  68. if err := startGoServer(
  69. ctx,
  70. flags.Root,
  71. fmt.Sprintf("http://localhost:%d", flags.Vite.Port),
  72. ); err != nil {
  73. log.Panic(err)
  74. }
  75. <-ctx.Done()
  76. }