2024-07-19 22:07:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
2024-07-23 17:30:29 +00:00
|
|
|
"grain/relay"
|
|
|
|
"grain/relay/db"
|
|
|
|
"grain/relay/utils"
|
2024-07-23 20:40:39 +00:00
|
|
|
"grain/web"
|
2024-07-20 01:09:42 +00:00
|
|
|
|
2024-07-19 22:07:04 +00:00
|
|
|
"golang.org/x/net/websocket"
|
2024-07-25 03:04:26 +00:00
|
|
|
"golang.org/x/time/rate"
|
2024-07-19 22:07:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-07-20 01:09:42 +00:00
|
|
|
config, err := utils.LoadConfig("config.yml")
|
2024-07-19 23:05:12 +00:00
|
|
|
if err != nil {
|
2024-07-20 01:09:42 +00:00
|
|
|
log.Fatal("Error loading config: ", err)
|
2024-07-19 23:05:12 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 14:53:01 +00:00
|
|
|
_, err = db.InitDB(config.MongoDB.URI, config.MongoDB.Database)
|
2024-07-19 23:05:12 +00:00
|
|
|
if err != nil {
|
2024-07-20 01:09:42 +00:00
|
|
|
log.Fatal("Error initializing database: ", err)
|
2024-07-19 23:05:12 +00:00
|
|
|
}
|
2024-07-23 14:53:01 +00:00
|
|
|
defer db.DisconnectDB()
|
2024-07-20 12:41:47 +00:00
|
|
|
|
2024-07-25 13:57:24 +00:00
|
|
|
rateLimiter := utils.NewRateLimiter(
|
|
|
|
rate.Limit(config.RateLimit.WsLimit),
|
|
|
|
config.RateLimit.WsBurst,
|
2024-07-25 20:43:46 +00:00
|
|
|
rate.Limit(config.RateLimit.EventLimit),
|
|
|
|
config.RateLimit.EventBurst,
|
2024-07-26 14:02:34 +00:00
|
|
|
rate.Limit(config.RateLimit.ReqLimit),
|
|
|
|
config.RateLimit.ReqBurst,
|
2024-07-25 13:57:24 +00:00
|
|
|
)
|
2024-07-25 13:03:34 +00:00
|
|
|
|
2024-07-25 03:04:26 +00:00
|
|
|
for _, kindLimit := range config.RateLimit.KindLimits {
|
2024-07-25 13:03:34 +00:00
|
|
|
rateLimiter.AddKindLimit(kindLimit.Kind, rate.Limit(kindLimit.Limit), kindLimit.Burst)
|
2024-07-25 03:04:26 +00:00
|
|
|
}
|
|
|
|
|
2024-07-25 13:57:24 +00:00
|
|
|
for category, categoryLimit := range config.RateLimit.CategoryLimits {
|
|
|
|
rateLimiter.AddCategoryLimit(category, rate.Limit(categoryLimit.Limit), categoryLimit.Burst)
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.SetRateLimiter(rateLimiter)
|
2024-07-25 03:04:26 +00:00
|
|
|
|
2024-07-23 20:40:39 +00:00
|
|
|
mux := http.NewServeMux()
|
2024-07-25 13:57:24 +00:00
|
|
|
mux.HandleFunc("/", ListenAndServe)
|
2024-07-23 20:40:39 +00:00
|
|
|
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static"))))
|
2024-07-25 02:09:54 +00:00
|
|
|
mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.ServeFile(w, r, "web/static/img/favicon.ico")
|
|
|
|
})
|
|
|
|
|
|
|
|
fmt.Printf("Server is running on http://localhost%s\n", config.Server.Port)
|
|
|
|
err = http.ListenAndServe(config.Server.Port, mux)
|
2024-07-19 23:05:12 +00:00
|
|
|
if err != nil {
|
2024-07-25 02:09:54 +00:00
|
|
|
fmt.Println("Error starting server:", err)
|
|
|
|
}
|
|
|
|
}
|
2024-07-25 13:57:24 +00:00
|
|
|
|
|
|
|
func ListenAndServe(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Header.Get("Upgrade") == "websocket" {
|
|
|
|
websocket.Handler(func(ws *websocket.Conn) {
|
|
|
|
relay.WebSocketHandler(ws)
|
|
|
|
}).ServeHTTP(w, r)
|
|
|
|
} else {
|
|
|
|
web.RootHandler(w, r)
|
|
|
|
}
|
|
|
|
}
|