main.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // Embedded frontend static files
  25. distFS, err := fs.Sub(assets, "frontend/dist")
  26. if err != nil {
  27. log.Fatal("failed to get embedded frontend:", err)
  28. }
  29. fileServer := http.FileServer(http.FS(distFS))
  30. // Create a handler that proxies /api to Gin, serves everything else from embed
  31. mux := http.NewServeMux()
  32. mux.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
  33. // Strip /api prefix before forwarding to Gin
  34. r.URL.Path = r.URL.Path[4:] // remove "/api"
  35. if r.URL.Path == "" {
  36. r.URL.Path = "/"
  37. }
  38. r.RequestURI = r.URL.RequestURI()
  39. proxy.ServeHTTP(w, r)
  40. })
  41. mux.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
  42. r.URL.Path = "/"
  43. r.RequestURI = "/"
  44. proxy.ServeHTTP(w, r)
  45. })
  46. mux.Handle("/", fileServer)
  47. err = wails.Run(&options.App{
  48. Title: "智慧停车管理系统",
  49. Width: 1400,
  50. Height: 900,
  51. MinWidth: 1024,
  52. MinHeight: 768,
  53. AssetServer: &assetserver.Options{
  54. Assets: assets,
  55. Handler: mux,
  56. },
  57. OnStartup: func(ctx context.Context) {
  58. log.Println("Wails app started, backend running on :8888")
  59. },
  60. Windows: &windows.Options{
  61. WebviewIsTransparent: false,
  62. WindowIsTranslucent: false,
  63. },
  64. })
  65. if err != nil {
  66. log.Fatal(err)
  67. }
  68. }