|
8 | 8 | "4-in-a-row/config" |
9 | 9 | "4-in-a-row/db" |
10 | 10 | "4-in-a-row/handlers" |
| 11 | + |
| 12 | + "github.com/rs/cors" |
11 | 13 | ) |
12 | 14 |
|
13 | 15 | func main() { |
@@ -49,15 +51,33 @@ func main() { |
49 | 51 | } |
50 | 52 | } |
51 | 53 |
|
52 | | - http.HandleFunc("/", handlers.RootHandler) |
53 | | - http.HandleFunc("/health", handlers.HealthHandler) |
54 | | - http.HandleFunc("/ws", handlers.WSHandler) |
55 | | - http.HandleFunc("/leaderboard", handlers.LeaderboardHandler) |
56 | | - http.HandleFunc("/metrics", handlers.GameMetricsHandler) |
57 | | - http.HandleFunc("/recent-games", handlers.RecentGamesHandler) |
| 54 | + mux := http.NewServeMux() |
| 55 | + mux.HandleFunc("/", handlers.RootHandler) |
| 56 | + mux.HandleFunc("/health", handlers.HealthHandler) |
| 57 | + mux.HandleFunc("/ws", handlers.WSHandler) |
| 58 | + mux.HandleFunc("/leaderboard", handlers.LeaderboardHandler) |
| 59 | + mux.HandleFunc("/metrics", handlers.GameMetricsHandler) |
| 60 | + mux.HandleFunc("/recent-games", handlers.RecentGamesHandler) |
| 61 | + |
| 62 | + // CORS |
| 63 | + c := cors.New(cors.Options{ |
| 64 | + AllowedOrigins: []string{"*"}, |
| 65 | + AllowedMethods: []string{ |
| 66 | + http.MethodGet, |
| 67 | + http.MethodPost, |
| 68 | + http.MethodPut, |
| 69 | + http.MethodPatch, |
| 70 | + http.MethodDelete, |
| 71 | + http.MethodOptions, |
| 72 | + http.MethodHead, |
| 73 | + }, |
| 74 | + AllowedHeaders: []string{"*"}, |
| 75 | + }) |
| 76 | + |
| 77 | + handler := c.Handler(mux) |
58 | 78 |
|
59 | 79 | log.Printf("Server listening on :%s", cfg.Port) |
60 | | - if err := http.ListenAndServe(":"+cfg.Port, nil); err != nil { |
| 80 | + if err := http.ListenAndServe(":"+cfg.Port, handler); err != nil { |
61 | 81 | log.Fatal("Server error: ", err) |
62 | 82 | } |
63 | 83 | } |
0 commit comments