grain/main.go

114 lines
2.7 KiB
Go
Raw Normal View History

2024-07-19 22:07:04 +00:00
package main
import (
"fmt"
"log"
"net/http"
2024-07-30 15:27:38 +00:00
"grain/config"
2024-07-23 17:30:29 +00:00
"grain/relay"
"grain/relay/db"
2024-07-23 20:40:39 +00:00
"grain/web"
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-30 15:27:38 +00:00
cfg, err := loadConfiguration()
if err != nil {
log.Fatal("Error loading config: ", err)
}
2024-07-30 15:27:38 +00:00
err = initializeDatabase(cfg)
if err != nil {
log.Fatal("Error initializing database: ", err)
}
defer db.DisconnectDB()
2024-07-20 12:41:47 +00:00
2024-07-30 15:27:38 +00:00
setupRateLimiter(cfg)
setupSizeLimiter(cfg)
2024-07-30 13:58:20 +00:00
err = loadRelayMetadata()
if err != nil {
log.Fatal("Failed to load relay metadata: ", err)
}
mux := setupRoutes()
2024-07-30 15:27:38 +00:00
startServer(cfg, mux)
2024-07-30 13:58:20 +00:00
}
2024-07-30 15:27:38 +00:00
func loadConfiguration() (*config.Config, error) {
return config.LoadConfig("config.yml")
2024-07-30 13:58:20 +00:00
}
2024-07-30 15:27:38 +00:00
func initializeDatabase(config *config.Config) error {
2024-07-30 13:58:20 +00:00
_, err := db.InitDB(config.MongoDB.URI, config.MongoDB.Database)
return err
}
2024-07-30 15:27:38 +00:00
func setupRateLimiter(cfg *config.Config) {
rateLimiter := config.NewRateLimiter(
rate.Limit(cfg.RateLimit.WsLimit),
cfg.RateLimit.WsBurst,
rate.Limit(cfg.RateLimit.EventLimit),
cfg.RateLimit.EventBurst,
rate.Limit(cfg.RateLimit.ReqLimit),
cfg.RateLimit.ReqBurst,
2024-07-25 13:57:24 +00:00
)
2024-07-30 15:27:38 +00:00
for _, kindLimit := range cfg.RateLimit.KindLimits {
rateLimiter.AddKindLimit(kindLimit.Kind, rate.Limit(kindLimit.Limit), kindLimit.Burst)
2024-07-25 03:04:26 +00:00
}
2024-07-30 15:27:38 +00:00
for category, categoryLimit := range cfg.RateLimit.CategoryLimits {
2024-07-25 13:57:24 +00:00
rateLimiter.AddCategoryLimit(category, rate.Limit(categoryLimit.Limit), categoryLimit.Burst)
}
2024-07-30 15:27:38 +00:00
config.SetRateLimiter(rateLimiter)
2024-07-30 13:58:20 +00:00
}
2024-07-25 03:04:26 +00:00
2024-07-30 15:27:38 +00:00
func setupSizeLimiter(cfg *config.Config) {
sizeLimiter := config.NewSizeLimiter(cfg.RateLimit.MaxEventSize)
for _, kindSizeLimit := range cfg.RateLimit.KindSizeLimits {
2024-07-26 20:46:01 +00:00
sizeLimiter.AddKindSizeLimit(kindSizeLimit.Kind, kindSizeLimit.MaxSize)
}
2024-07-27 20:46:25 +00:00
2024-07-30 15:27:38 +00:00
config.SetSizeLimiter(sizeLimiter)
2024-07-30 13:58:20 +00:00
}
2024-07-26 20:46:01 +00:00
2024-07-30 13:58:20 +00:00
func loadRelayMetadata() error {
return web.LoadRelayMetadata("relay_metadata.json")
}
2024-07-27 20:46:25 +00:00
2024-07-30 13:58:20 +00:00
func setupRoutes() *http.ServeMux {
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"))))
mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "web/static/img/favicon.ico")
})
2024-07-30 13:58:20 +00:00
return mux
}
2024-07-30 15:27:38 +00:00
func startServer(config *config.Config, mux *http.ServeMux) {
fmt.Printf("Server is running on http://localhost%s\n", config.Server.Port)
2024-07-30 13:58:20 +00:00
err := http.ListenAndServe(config.Server.Port, mux)
if err != nil {
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)
2024-07-28 20:25:20 +00:00
} else if r.Header.Get("Accept") == "application/nostr+json" {
web.RelayInfoHandler(w, r)
2024-07-25 13:57:24 +00:00
} else {
web.RootHandler(w, r)
}
}