2024-07-19 22:07:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-07-31 18:12:33 +00:00
|
|
|
"grain/app"
|
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"
|
2024-07-31 18:41:48 +00:00
|
|
|
"grain/server/nip"
|
2024-07-31 18:12:33 +00:00
|
|
|
"grain/server/utils"
|
2024-07-31 16:27:56 +00:00
|
|
|
|
2024-07-31 15:28:41 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-07-20 01:09:42 +00:00
|
|
|
|
2024-07-19 22:07:04 +00:00
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-07-31 18:41:48 +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-07-31 15:28:41 +00:00
|
|
|
cfg, err := config.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-31 15:28:41 +00:00
|
|
|
client, err := db.InitDB(cfg)
|
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-31 15:28:41 +00:00
|
|
|
defer db.DisconnectDB(client)
|
2024-07-20 12:41:47 +00:00
|
|
|
|
2024-07-30 15:51:05 +00:00
|
|
|
config.SetupRateLimiter(cfg)
|
|
|
|
config.SetupSizeLimiter(cfg)
|
2024-07-30 13:58:20 +00:00
|
|
|
|
2024-07-31 18:41:48 +00:00
|
|
|
err = nip.LoadRelayMetadataJSON()
|
2024-07-30 13:58:20 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-31 19:54:42 +00:00
|
|
|
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("app/static"))))
|
2024-07-25 02:09:54 +00:00
|
|
|
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-07-25 02:09:54 +00:00
|
|
|
})
|
2024-07-30 13:58:20 +00:00
|
|
|
return mux
|
|
|
|
}
|
2024-07-25 02:09:54 +00:00
|
|
|
|
2024-07-31 15:56:55 +00:00
|
|
|
func startServer(config *configTypes.ServerConfig, mux *http.ServeMux) {
|
2024-07-25 02:09:54 +00:00
|
|
|
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)
|
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)
|
2024-07-28 20:25:20 +00:00
|
|
|
} else if r.Header.Get("Accept") == "application/nostr+json" {
|
2024-07-31 18:41:48 +00:00
|
|
|
nip.RelayInfoHandler(w, r)
|
2024-07-25 13:57:24 +00:00
|
|
|
} else {
|
2024-07-31 18:12:33 +00:00
|
|
|
app.RootHandler(w, r)
|
2024-07-25 13:57:24 +00:00
|
|
|
}
|
|
|
|
}
|