Skip to content

Commit e96dc0f

Browse files
feat: add CORS support to the game server
1 parent 61719a2 commit e96dc0f

3 files changed

Lines changed: 30 additions & 7 deletions

File tree

‎backend/go.mod‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/jinzhu/now v1.1.5 // indirect
2424
github.com/klauspost/compress v1.15.9 // indirect
2525
github.com/pierrec/lz4/v4 v4.1.15 // indirect
26+
github.com/rs/cors v1.11.1 // indirect
2627
golang.org/x/crypto v0.37.0 // indirect
2728
golang.org/x/sync v0.13.0 // indirect
2829
golang.org/x/text v0.24.0 // indirect

‎backend/go.sum‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
3535
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3636
github.com/redis/go-redis/v9 v9.17.2 h1:P2EGsA4qVIM3Pp+aPocCJ7DguDHhqrXNhVcEp4ViluI=
3737
github.com/redis/go-redis/v9 v9.17.2/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370=
38+
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
39+
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
3840
github.com/segmentio/kafka-go v0.4.49 h1:GJiNX1d/g+kG6ljyJEoi9++PUMdXGAxb7JGPiDCuNmk=
3941
github.com/segmentio/kafka-go v0.4.49/go.mod h1:Y1gn60kzLEEaW28YshXyk2+VCUKbJ3Qr6DrnT3i4+9E=
4042
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

‎backend/main.go‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"4-in-a-row/config"
99
"4-in-a-row/db"
1010
"4-in-a-row/handlers"
11+
12+
"github.com/rs/cors"
1113
)
1214

1315
func main() {
@@ -49,15 +51,33 @@ func main() {
4951
}
5052
}
5153

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)
5878

5979
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 {
6181
log.Fatal("Server error: ", err)
6282
}
6383
}

0 commit comments

Comments
 (0)