From f5a0f6560da4313d0f58f2d0629937e2393d5515 Mon Sep 17 00:00:00 2001 From: 0ceanSlim Date: Thu, 8 Aug 2024 08:40:23 -0400 Subject: [PATCH] moved timeouts to config --- config/types/serverConfig.go | 5 ++++- main.go | 33 +++++++++++++++++---------------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/config/types/serverConfig.go b/config/types/serverConfig.go index 2602445..b4f0836 100644 --- a/config/types/serverConfig.go +++ b/config/types/serverConfig.go @@ -6,7 +6,10 @@ type ServerConfig struct { Database string `yaml:"database"` } `yaml:"mongodb"` Server struct { - Port string `yaml:"port"` + Port string `yaml:"port"` + ReadTimeout int `yaml:"read_timeout"` // Timeout in seconds + WriteTimeout int `yaml:"write_timeout"` // Timeout in seconds + IdleTimeout int `yaml:"idle_timeout"` // Timeout in seconds } `yaml:"server"` RateLimit RateLimitConfig `yaml:"rate_limit"` PubkeyWhitelist PubkeyWhitelistConfig `yaml:"pubkey_whitelist"` diff --git a/main.go b/main.go index 8188f0d..9c1c7d2 100644 --- a/main.go +++ b/main.go @@ -62,9 +62,9 @@ func startServer(config *configTypes.ServerConfig, mux *http.ServeMux) { server := &http.Server{ Addr: config.Server.Port, Handler: mux, - ReadTimeout: 10 * time.Second, - WriteTimeout: 10 * time.Second, - IdleTimeout: 120 * time.Second, + ReadTimeout: time.Duration(config.Server.ReadTimeout) * time.Second, + WriteTimeout: time.Duration(config.Server.WriteTimeout) * time.Second, + IdleTimeout: time.Duration(config.Server.IdleTimeout) * time.Second, } fmt.Printf("Server is running on http://localhost%s\n", config.Server.Port) err := server.ListenAndServe() @@ -72,20 +72,21 @@ func startServer(config *configTypes.ServerConfig, mux *http.ServeMux) { fmt.Println("Error starting server:", err) } } + var wsServer = &websocket.Server{ - Handshake: func(config *websocket.Config, r *http.Request) error { - // Skip origin check - return nil - }, - Handler: websocket.Handler(relay.WebSocketHandler), + Handshake: func(config *websocket.Config, r *http.Request) error { + // Skip origin check + return nil + }, + Handler: websocket.Handler(relay.WebSocketHandler), } func ListenAndServe(w http.ResponseWriter, r *http.Request) { - if r.Header.Get("Upgrade") == "websocket" { - wsServer.ServeHTTP(w, r) - } else if r.Header.Get("Accept") == "application/nostr+json" { - nip.RelayInfoHandler(w, r) - } else { - app.RootHandler(w, r) - } -} \ No newline at end of file + if r.Header.Get("Upgrade") == "websocket" { + wsServer.ServeHTTP(w, r) + } else if r.Header.Get("Accept") == "application/nostr+json" { + nip.RelayInfoHandler(w, r) + } else { + app.RootHandler(w, r) + } +}