-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
159 lines (123 loc) · 5.11 KB
/
main.go
File metadata and controls
159 lines (123 loc) · 5.11 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
_ "embed"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"os"
"time"
"net/http"
"github.com/jmoiron/sqlx"
account "github.com/justinfarrelldev/open-ctp-server/internal/account"
auth "github.com/justinfarrelldev/open-ctp-server/internal/auth"
game "github.com/justinfarrelldev/open-ctp-server/internal/game"
health "github.com/justinfarrelldev/open-ctp-server/internal/health"
lobby "github.com/justinfarrelldev/open-ctp-server/internal/lobby"
_ "github.com/justinfarrelldev/open-ctp-server/docs"
"github.com/flowchartsman/swaggerui"
"github.com/didip/tollbooth/v7"
_ "github.com/lib/pq"
"github.com/joho/godotenv"
)
// @title Open Call to Power Server
// @description This is the open-source Call to Power and Call to Power 2 server project. This project is not sponsored, maintained or affiliated with Activision.
// @contact.name API Support
// @contact.email justinfarrellwebdev@gmail.com
type Server struct {
}
type Message struct {
Status string `json:"status"`
Body string `json:"body"`
}
var (
port = 9000
sleep = flag.Duration("sleep", time.Second*5, "duration between changes in health")
system = "" // empty string represents the health of the system
)
//go:embed docs/swagger.json
var spec []byte
func main() {
if os.Getenv("SUPABASE_DB_URL") == "" {
// Load .env file
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file")
}
}
// Tollbooth
message := Message{
Status: "Request Failed",
Body: "The API is at capacity, try again later.",
}
jsonMessage, _ := json.Marshal(message)
// TODO move these tollbooth initializations to its own module to clean up the code path
// Limiter for standard operations
tollboothLimiter := tollbooth.NewLimiter(5, nil)
tollboothLimiter.SetMessageContentType("application/json")
tollboothLimiter.SetMessage(string(jsonMessage))
tollboothLimiter.SetTokenBucketExpirationTTL(time.Hour)
tollboothLimiter.SetBasicAuthExpirationTTL(time.Hour)
tollboothLimiter.SetHeaderEntryExpirationTTL(time.Hour)
// Limiter for DB operations
tollboothLimiterMinute := tollbooth.NewLimiter(1.0/60.0, nil)
tollboothLimiterMinute.SetMessageContentType("application/json")
tollboothLimiterMinute.SetMessage(string(jsonMessage))
tollboothLimiterMinute.SetTokenBucketExpirationTTL(time.Hour)
tollboothLimiterMinute.SetBasicAuthExpirationTTL(time.Hour)
tollboothLimiterMinute.SetHeaderEntryExpirationTTL(time.Hour)
// Limiter for the health check
tollboothLimiterHealth := tollbooth.NewLimiter(5, nil)
tollboothLimiterHealth.SetMessageContentType("application/json")
tollboothLimiterHealth.SetMessage(string(jsonMessage))
tollboothLimiterHealth.SetTokenBucketExpirationTTL(time.Hour)
tollboothLimiterHealth.SetBasicAuthExpirationTTL(time.Hour)
tollboothLimiterHealth.SetHeaderEntryExpirationTTL(time.Hour)
// Postgres
db, err := sqlx.Open("postgres", os.Getenv("SUPABASE_DB_URL"))
if err != nil {
log.Fatal(err)
}
fmt.Println("opened connection to database successfully")
sessionStore := auth.NewSessionStore(db)
// Handlers
mux := http.NewServeMux()
mux.Handle("/game/create_game", tollbooth.LimitFuncHandler(tollboothLimiter, func(w http.ResponseWriter, r *http.Request) {
game.GameHandler(w, r, db)
}))
mux.Handle("/account/create_account", tollbooth.LimitFuncHandler(tollboothLimiterMinute, func(w http.ResponseWriter, r *http.Request) {
account.CreateAccountHandler(w, r, db, sessionStore)
}))
mux.Handle("/account/get_account", tollbooth.LimitFuncHandler(tollboothLimiter, func(w http.ResponseWriter, r *http.Request) {
account.GetAccountHandler(w, r, db, sessionStore)
}))
mux.Handle("/account/update_account", tollbooth.LimitFuncHandler(tollboothLimiter, func(w http.ResponseWriter, r *http.Request) {
account.UpdateAccountHandler(w, r, db, sessionStore)
}))
mux.Handle("/account/delete_account", tollbooth.LimitFuncHandler(tollboothLimiter, func(w http.ResponseWriter, r *http.Request) {
account.DeleteAccountHandler(w, r, db, sessionStore)
}))
mux.Handle("/lobby/create_lobby", tollbooth.LimitFuncHandler(tollboothLimiterMinute, func(w http.ResponseWriter, r *http.Request) {
lobby.CreateLobbyHandler(w, r, db, sessionStore)
}))
mux.Handle("/lobby/get_lobby", tollbooth.LimitFuncHandler(tollboothLimiter, func(w http.ResponseWriter, r *http.Request) {
lobby.GetLobbyHandler(w, r, db, sessionStore)
}))
mux.Handle("/lobby/update_lobby", tollbooth.LimitFuncHandler(tollboothLimiter, func(w http.ResponseWriter, r *http.Request) {
lobby.UpdateLobbyHandler(w, r, db, sessionStore)
}))
mux.Handle("/lobby/delete_lobby", tollbooth.LimitFuncHandler(tollboothLimiter, func(w http.ResponseWriter, r *http.Request) {
lobby.DeleteLobbyHandler(w, r, db, sessionStore)
}))
mux.Handle("/health", tollbooth.LimitFuncHandler(tollboothLimiterHealth, health.HealthCheckHandler))
mux.Handle("/docs/", http.StripPrefix("/docs", swaggerui.Handler(spec)))
fmt.Printf("\nNow serving on port %d\n", port)
err = http.ListenAndServe(fmt.Sprintf(":%d", port), mux)
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("server closed\n")
} else if err != nil {
fmt.Printf("error starting server: %s\n", err)
os.Exit(1)
}
}