main.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package main
  2. import (
  3. "context"
  4. "embed"
  5. "io/fs"
  6. "log"
  7. "net/http"
  8. "net/http/httputil"
  9. "net/url"
  10. "wails-app/internal"
  11. "github.com/wailsapp/wails/v2"
  12. "github.com/wailsapp/wails/v2/pkg/options"
  13. "github.com/wailsapp/wails/v2/pkg/options/assetserver"
  14. "github.com/wailsapp/wails/v2/pkg/options/windows"
  15. )
  16. //go:embed all:frontend/dist
  17. var assets embed.FS
  18. func main() {
  19. // Initialize the Gin backend (non-blocking goroutine, listens on :8888)
  20. internal.InitBackend()
  21. // Reverse proxy for /api/* requests to Gin backend (strips /api prefix)
  22. backendURL, _ := url.Parse("http://127.0.0.1:8888")
  23. proxy := httputil.NewSingleHostReverseProxy(backendURL)
  24. // Flush each MJPEG chunk immediately through the Wails asset proxy instead
  25. // of buffering it until the response is considered complete.
  26. proxy.FlushInterval = -1
  27. // Embedded frontend static files
  28. distFS, err := fs.Sub(assets, "frontend/dist")
  29. if err != nil {
  30. log.Fatal("failed to get embedded frontend:", err)
  31. }
  32. fileServer := http.FileServer(http.FS(distFS))
  33. // Create a handler that proxies /api to Gin, serves everything else from embed
  34. mux := http.NewServeMux()
  35. mux.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
  36. // Strip /api prefix before forwarding to Gin
  37. r.URL.Path = r.URL.Path[4:] // remove "/api"
  38. if r.URL.Path == "" {
  39. r.URL.Path = "/"
  40. }
  41. r.RequestURI = r.URL.RequestURI()
  42. proxy.ServeHTTP(w, r)
  43. })
  44. mux.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
  45. r.URL.Path = "/"
  46. r.RequestURI = "/"
  47. proxy.ServeHTTP(w, r)
  48. })
  49. // Proxy uploaded edge images to the Gin backend. The backend owns the
  50. // configured local storage directory and serves /uploads/file/*.
  51. mux.HandleFunc("/uploads/", func(w http.ResponseWriter, r *http.Request) {
  52. proxy.ServeHTTP(w, r)
  53. })
  54. mux.Handle("/", fileServer)
  55. err = wails.Run(&options.App{
  56. Title: "智慧停车管理系统",
  57. Width: 1400,
  58. Height: 900,
  59. MinWidth: 1024,
  60. MinHeight: 768,
  61. AssetServer: &assetserver.Options{
  62. Assets: assets,
  63. Handler: mux,
  64. },
  65. OnStartup: func(ctx context.Context) {
  66. log.Println("Wails app started, backend running on :8888")
  67. },
  68. Windows: &windows.Options{
  69. WebviewIsTransparent: false,
  70. WindowIsTranslucent: false,
  71. },
  72. })
  73. if err != nil {
  74. log.Fatal(err)
  75. }
  76. }