main.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. fmt.Sprintf("--asset-proxy-url=%s", proxyURL))
  35. c.Dir = root
  36. c.Stdout = os.Stdout
  37. c.Stderr = os.Stderr
  38. return c.Start()
  39. }
  40. func main() {
  41. var flags struct {
  42. Root string
  43. Vite struct {
  44. Port int
  45. }
  46. }
  47. pflag.StringVar(
  48. &flags.Root,
  49. "root",
  50. ".",
  51. "Root directory for the server")
  52. pflag.IntVar(
  53. &flags.Vite.Port,
  54. "vite.port",
  55. 3000,
  56. "Port for the vite server")
  57. pflag.Parse()
  58. ctx, done := signal.NotifyContext(context.Background(), os.Interrupt)
  59. defer done()
  60. if err := startViteServer(
  61. ctx,
  62. flags.Root,
  63. flags.Vite.Port,
  64. ); err != nil {
  65. log.Panic(err)
  66. }
  67. if err := startGoServer(
  68. ctx,
  69. flags.Root,
  70. fmt.Sprintf("http://localhost:%d", flags.Vite.Port),
  71. ); err != nil {
  72. log.Panic(err)
  73. }
  74. <-ctx.Done()
  75. }