-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (54 loc) · 1.99 KB
/
Copy pathmain.go
File metadata and controls
66 lines (54 loc) · 1.99 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"qwer-api/api"
)
func serverMiddleware(mux *http.ServeMux) http.Handler {
// chain: rate limit -> json logger -> mux
return api.JsonLogger(api.RateLimit(120, mux))
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
mux := http.NewServeMux()
// Static website (serves public/index.html at / and other assets)
fs := http.FileServer(http.Dir("public"))
mux.Handle("/", fs)
// API routes (primary)
mux.HandleFunc("/api", api.IndexHandler)
mux.HandleFunc("/api/", api.IndexHandler)
mux.HandleFunc("/api/band", api.BandHandler)
mux.HandleFunc("/api/members", api.MembersHandler)
mux.HandleFunc("/api/songs", api.SongsHandler)
mux.HandleFunc("/api/albums", api.AlbumsHandler)
mux.HandleFunc("/api/awards", api.AwardsHandler)
// Aliases for compatibility
mux.HandleFunc("/server", api.IndexHandler)
mux.HandleFunc("/server/", api.IndexHandler)
mux.HandleFunc("/server/band", api.BandHandler)
mux.HandleFunc("/server/members", api.MembersHandler)
mux.HandleFunc("/server/songs", api.SongsHandler)
mux.HandleFunc("/server/albums", api.AlbumsHandler)
mux.HandleFunc("/server/awards", api.AwardsHandler)
mux.HandleFunc("/docs", api.DocsHandler)
// Serve OpenAPI spec file under both paths
mux.HandleFunc("/openapi.yaml", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "openapi.yaml")
})
mux.HandleFunc("/openserver.yaml", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "openapi.yaml")
})
fmt.Printf("🎸 QWER Band API Server starting on port %s...\n", port)
fmt.Printf("📋 Available endpoints:\n")
fmt.Printf(" • GET http://localhost:%s/ - Website\n", port)
fmt.Printf(" • GET http://localhost:%s/api - API Index\n", port)
fmt.Printf(" • GET http://localhost:%s/docs - API Docs (ReDoc)\n", port)
fmt.Printf("\n🚀 Server ready! Press Ctrl+C to stop.\n\n")
handler := serverMiddleware(mux)
log.Fatal(http.ListenAndServe(":"+port, handler))
}