2024-07-19 22:07:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-08-06 19:35:52 +00:00
|
|
|
"grain/app/src/api"
|
|
|
|
"grain/app/src/routes"
|
2024-07-30 15:27:38 +00:00
|
|
|
"grain/config"
|
2024-07-31 15:56:55 +00:00
|
|
|
configTypes "grain/config/types"
|
2024-07-31 18:12:33 +00:00
|
|
|
relay "grain/server"
|
|
|
|
"grain/server/db"
|
|
|
|
"grain/server/utils"
|
2024-07-31 15:28:41 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-08-09 20:22:27 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
2024-08-01 11:54:59 +00:00
|
|
|
"time"
|
2024-07-20 01:09:42 +00:00
|
|
|
|
2024-07-19 22:07:04 +00:00
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-08-18 02:00:03 +00:00
|
|
|
utils.EnsureFileExists("config.yml", "app/static/examples/config.example.yml")
|
|
|
|
utils.EnsureFileExists("relay_metadata.json", "app/static/examples/relay_metadata.example.json")
|
2024-07-31 16:27:56 +00:00
|
|
|
|
2024-08-09 20:22:27 +00:00
|
|
|
restartChan := make(chan struct{})
|
2024-09-05 15:02:45 +00:00
|
|
|
go config.WatchConfigFile("config.yml", restartChan) // Critical goroutine
|
2024-07-19 23:05:12 +00:00
|
|
|
|
2024-08-09 20:22:27 +00:00
|
|
|
signalChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
|
2024-07-20 12:41:47 +00:00
|
|
|
|
2024-08-09 20:22:27 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
for {
|
2024-08-19 17:47:23 +00:00
|
|
|
wg.Add(1) // Add to WaitGroup for the server goroutine
|
|
|
|
|
2024-08-09 20:22:27 +00:00
|
|
|
cfg, err := config.LoadConfig("config.yml")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error loading config: ", err)
|
|
|
|
}
|
2024-07-30 13:58:20 +00:00
|
|
|
|
2024-09-05 15:04:12 +00:00
|
|
|
config.SetResourceLimit(&cfg.ResourceLimits) // Apply limits once before starting the server
|
2024-08-19 17:47:23 +00:00
|
|
|
|
2024-08-09 20:22:27 +00:00
|
|
|
client, err := db.InitDB(cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error initializing database: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.SetupRateLimiter(cfg)
|
|
|
|
config.SetupSizeLimiter(cfg)
|
|
|
|
|
2024-09-02 00:51:02 +00:00
|
|
|
config.ClearTemporaryBans()
|
2024-08-15 20:20:22 +00:00
|
|
|
|
2024-08-09 20:22:27 +00:00
|
|
|
err = utils.LoadRelayMetadataJSON()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to load relay metadata: ", err)
|
|
|
|
}
|
2024-07-30 13:58:20 +00:00
|
|
|
|
2024-08-09 20:22:27 +00:00
|
|
|
mux := setupRoutes()
|
2024-08-19 17:47:23 +00:00
|
|
|
|
|
|
|
// Start the server
|
2024-08-09 20:22:27 +00:00
|
|
|
server := startServer(cfg, mux, &wg)
|
2024-07-30 13:58:20 +00:00
|
|
|
|
2024-08-09 20:22:27 +00:00
|
|
|
select {
|
|
|
|
case <-restartChan:
|
|
|
|
log.Println("Restarting server...")
|
2024-08-19 17:47:23 +00:00
|
|
|
server.Close() // Stop the current server instance
|
|
|
|
wg.Wait() // Wait for the server goroutine to finish
|
2024-08-15 20:20:22 +00:00
|
|
|
time.Sleep(3 * time.Second)
|
2024-08-19 17:47:23 +00:00
|
|
|
|
2024-08-09 20:22:27 +00:00
|
|
|
case <-signalChan:
|
|
|
|
log.Println("Shutting down server...")
|
2024-09-02 00:51:02 +00:00
|
|
|
server.Close() // Stop the server
|
|
|
|
db.DisconnectDB(client) // Disconnect from MongoDB
|
|
|
|
wg.Wait() // Wait for all goroutines to finish
|
2024-08-09 20:22:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-07-30 13:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setupRoutes() *http.ServeMux {
|
2024-07-23 20:40:39 +00:00
|
|
|
mux := http.NewServeMux()
|
2024-08-01 14:48:24 +00:00
|
|
|
mux.HandleFunc("/", ListenAndServe)
|
2024-08-06 19:35:52 +00:00
|
|
|
mux.HandleFunc("/import-results", api.ImportEvents)
|
|
|
|
mux.HandleFunc("/import-events", routes.ImportEvents)
|
2024-08-01 14:48:24 +00:00
|
|
|
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("app/static"))))
|
|
|
|
mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
2024-07-31 19:54:42 +00:00
|
|
|
http.ServeFile(w, r, "app/static/img/favicon.ico")
|
2024-08-01 14:48:24 +00:00
|
|
|
})
|
2024-07-30 13:58:20 +00:00
|
|
|
return mux
|
|
|
|
}
|
2024-07-25 02:09:54 +00:00
|
|
|
|
2024-08-09 20:22:27 +00:00
|
|
|
func startServer(config *configTypes.ServerConfig, mux *http.ServeMux, wg *sync.WaitGroup) *http.Server {
|
2024-08-01 11:54:59 +00:00
|
|
|
server := &http.Server{
|
|
|
|
Addr: config.Server.Port,
|
|
|
|
Handler: mux,
|
2024-08-08 12:40:23 +00:00
|
|
|
ReadTimeout: time.Duration(config.Server.ReadTimeout) * time.Second,
|
|
|
|
WriteTimeout: time.Duration(config.Server.WriteTimeout) * time.Second,
|
|
|
|
IdleTimeout: time.Duration(config.Server.IdleTimeout) * time.Second,
|
2024-08-01 11:54:59 +00:00
|
|
|
}
|
2024-08-09 20:22:27 +00:00
|
|
|
|
|
|
|
go func() {
|
2024-08-19 17:47:23 +00:00
|
|
|
defer wg.Done() // Notify that the server goroutine is done
|
2024-08-09 20:22:27 +00:00
|
|
|
fmt.Printf("Server is running on http://localhost%s\n", config.Server.Port)
|
|
|
|
err := server.ListenAndServe()
|
|
|
|
if err != nil && err != http.ErrServerClosed {
|
|
|
|
fmt.Println("Error starting server:", err)
|
|
|
|
}
|
|
|
|
}()
|
2024-08-19 17:47:23 +00:00
|
|
|
|
2024-08-09 20:22:27 +00:00
|
|
|
return server
|
2024-07-25 02:09:54 +00:00
|
|
|
}
|
2024-08-08 12:40:23 +00:00
|
|
|
|
2024-08-01 14:43:17 +00:00
|
|
|
var wsServer = &websocket.Server{
|
2024-08-08 12:40:23 +00:00
|
|
|
Handshake: func(config *websocket.Config, r *http.Request) error {
|
|
|
|
// Skip origin check
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Handler: websocket.Handler(relay.WebSocketHandler),
|
2024-08-01 14:43:17 +00:00
|
|
|
}
|
2024-07-25 13:57:24 +00:00
|
|
|
|
|
|
|
func ListenAndServe(w http.ResponseWriter, r *http.Request) {
|
2024-08-08 12:40:23 +00:00
|
|
|
if r.Header.Get("Upgrade") == "websocket" {
|
|
|
|
wsServer.ServeHTTP(w, r)
|
|
|
|
} else if r.Header.Get("Accept") == "application/nostr+json" {
|
2024-08-09 12:55:30 +00:00
|
|
|
utils.RelayInfoHandler(w, r)
|
2024-08-08 12:40:23 +00:00
|
|
|
} else {
|
2024-08-09 13:24:32 +00:00
|
|
|
routes.IndexHandler(w, r)
|
2024-08-08 12:40:23 +00:00
|
|
|
}
|
2024-08-18 02:00:03 +00:00
|
|
|
}
|