Added config options for http request timeouts

This commit is contained in:
2025-02-12 12:48:10 +11:00
parent e605e6437b
commit 1253c6499d
8 changed files with 91 additions and 4 deletions

View File

@@ -16,6 +16,9 @@ type Config struct {
Port string // Port to listen on
TrustedHost string // Domain/Hostname to accept as trusted
SSL bool // Flag for SSL Mode
ReadHeaderTimeout int // Timeout for reading request headers in seconds
WriteTimeout int // Timeout for writing requests in seconds
IdleTimeout int // Timeout for idle connections in seconds
TursoDBName string // DB Name for Turso DB/Branch
TursoToken string // Bearer token for Turso DB/Branch
SecretKey string // Secret key for signing tokens
@@ -81,6 +84,9 @@ func GetConfig(args map[string]string) (*Config, error) {
Port: port,
TrustedHost: os.Getenv("TRUSTED_HOST"),
SSL: GetEnvBool("SSL_MODE", false),
ReadHeaderTimeout: GetEnvInt("READ_HEADER_TIMEOUT", 2),
WriteTimeout: GetEnvInt("WRITE_TIMEOUT", 10),
IdleTimeout: GetEnvInt("IDLE_TIMEOUT", 120),
TursoDBName: os.Getenv("TURSO_DB_NAME"),
TursoToken: os.Getenv("TURSO_AUTH_TOKEN"),
SecretKey: os.Getenv("SECRET_KEY"),

View File

@@ -4,6 +4,7 @@ import (
"os"
"strconv"
"strings"
"time"
)
// Get an environment variable, specifying a default value if its not set
@@ -15,6 +16,38 @@ func GetEnvDefault(key string, defaultValue string) string {
return val
}
// Get an environment variable as a time.Duration, specifying a default value if its
// not set or can't be parsed properly
func GetEnvDur(key string, defaultValue time.Duration) time.Duration {
val, exists := os.LookupEnv(key)
if !exists {
return defaultValue
}
intVal, err := strconv.Atoi(val)
if err != nil {
return defaultValue
}
return time.Duration(intVal)
}
// Get an environment variable as an int, specifying a default value if its
// not set or can't be parsed properly into an int
func GetEnvInt(key string, defaultValue int) int {
val, exists := os.LookupEnv(key)
if !exists {
return defaultValue
}
intVal, err := strconv.Atoi(val)
if err != nil {
return defaultValue
}
return intVal
}
// Get an environment variable as an int64, specifying a default value if its
// not set or can't be parsed properly into an int64
func GetEnvInt64(key string, defaultValue int64) int64 {