-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
79 lines (68 loc) · 2.31 KB
/
Copy pathmain.go
File metadata and controls
79 lines (68 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"snowy_viewer/internal/cache"
"snowy_viewer/internal/config"
"snowy_viewer/internal/handlers"
"snowy_viewer/internal/masterdata"
"snowy_viewer/internal/middleware"
)
func main() {
// Load configuration
cfg := config.Load()
// Initialize cache (Redis with memory fallback)
appCache := cache.New(cfg.RedisURL)
defer appCache.Close()
// Initialize and load master data
store := masterdata.NewStore(cfg.MasterDataPath)
if err := store.Fetch(); err != nil {
fmt.Printf("Initial fetch error: %v\n", err)
}
// Create router and register handlers
mux := http.NewServeMux()
handler := handlers.New(store)
handler.RegisterRoutes(mux)
// Prevent unknown /api/* paths from bouncing between Go and Next.js.
mux.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
})
// Set up frontend proxy or default to API-only mode
if cfg.FrontendProxyURL != "" && cfg.FrontendProxyURL != "none" {
nextjsURL, err := url.Parse(cfg.FrontendProxyURL)
if err != nil {
fmt.Printf("Invalid FRONTEND_PROXY_URL %q: %v\n", cfg.FrontendProxyURL, err)
setupAPIOnlyMode(mux)
} else {
nextjsProxy := httputil.NewSingleHostReverseProxy(nextjsURL)
nextjsProxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
fmt.Printf("Next.js proxy error for %s: %v\n", r.URL.Path, err)
http.Error(w, "frontend upstream unavailable", http.StatusBadGateway)
}
fmt.Printf("Proxying frontend requests to Next.js server on %s\n", cfg.FrontendProxyURL)
mux.Handle("/", nextjsProxy)
}
} else {
setupAPIOnlyMode(mux)
}
// Apply middlewares and start server
finalHandler := middleware.Chain(mux, middleware.CORS, middleware.Gzip)
fmt.Printf("Server starting on :%s...\n", cfg.Port)
if err := http.ListenAndServe(":"+cfg.Port, finalHandler); err != nil {
fmt.Printf("Error starting server: %s\n", err)
}
}
func setupAPIOnlyMode(mux *http.ServeMux) {
fmt.Println("Frontend proxy is disabled. API-only mode active.")
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"status":"ok","message":"PJSK Moe API Server"}`))
return
}
http.NotFound(w, r)
})
}