Moved config and finished JWT module

This commit is contained in:
2025-02-10 22:10:03 +11:00
parent 04049bb73a
commit e73805a02d
13 changed files with 237 additions and 69 deletions

View File

@@ -1,106 +0,0 @@
package server
import (
"errors"
"fmt"
"os"
"projectreshoot/logging"
"github.com/joho/godotenv"
"github.com/rs/zerolog"
)
type Config struct {
Host string // Host to listen on
Port string // Port to listen on
TrustedHost string // Domain/Hostname to accept as trusted
TursoDBName string // DB Name for Turso DB/Branch
TursoToken string // Bearer token for Turso DB/Branch
SecretKey string // Secret key for signing tokens
AccessTokenExpiry int64 // Access token expiry in minutes
RefreshTokenExpiry int64 // Refresh token expiry in minutes
TokenFreshTime int64 // Time for tokens to stay fresh in minutes
LogLevel zerolog.Level // Log level for global logging. Defaults to info
LogOutput string // "file", "console", or "both". Defaults to console
LogDir string // Path to create log files
}
// Load the application configuration and get a pointer to the Config object
func GetConfig(args map[string]string) (*Config, error) {
err := godotenv.Load(".env")
if err != nil {
fmt.Println(".env file not found.")
}
var (
host string
port string
logLevel zerolog.Level
logOutput string
valid bool
)
if args["host"] != "" {
host = args["host"]
} else {
host = GetEnvDefault("HOST", "127.0.0.1")
}
if args["port"] != "" {
port = args["port"]
} else {
port = GetEnvDefault("PORT", "3333")
}
if args["loglevel"] != "" {
logLevel = logging.GetLogLevel(args["loglevel"])
} else {
logLevel = logging.GetLogLevel(GetEnvDefault("LOG_LEVEL", "info"))
}
if args["logoutput"] != "" {
opts := map[string]string{
"both": "both",
"file": "file",
"console": "console",
}
logOutput, valid = opts[args["logoutput"]]
if !valid {
logOutput = "console"
fmt.Println(
"Log output type was not parsed correctly. Defaulting to console only",
)
}
} else {
logOutput = GetEnvDefault("LOG_OUTPUT", "console")
}
if logOutput != "both" && logOutput != "console" && logOutput != "file" {
logOutput = "console"
}
config := &Config{
Host: host,
Port: port,
TrustedHost: os.Getenv("TRUSTED_HOST"),
TursoDBName: os.Getenv("TURSO_DB_NAME"),
TursoToken: os.Getenv("TURSO_AUTH_TOKEN"),
SecretKey: os.Getenv("SECRET_KEY"),
AccessTokenExpiry: GetEnvInt64("ACCESS_TOKEN_EXPIRY", 5),
RefreshTokenExpiry: GetEnvInt64("REFRESH_TOKEN_EXPIRY", 1440), // defaults to 1 day
TokenFreshTime: GetEnvInt64("TOKEN_FRESH_TIME", 5),
LogLevel: logLevel,
LogOutput: logOutput,
LogDir: GetEnvDefault("LOG_DIR", ""),
}
if config.TrustedHost == "" {
return nil, errors.New("Envar not set: TRUSTED_HOST")
}
if config.TursoDBName == "" {
return nil, errors.New("Envar not set: TURSO_DB_NAME")
}
if config.TursoToken == "" {
return nil, errors.New("Envar not set: TURSO_AUTH_TOKEN")
}
if config.SecretKey == "" {
return nil, errors.New("Envar not set: SECRET_KEY")
}
return config, nil
}

View File

@@ -1,31 +0,0 @@
package server
import (
"os"
"strconv"
)
// Get an environment variable, specifying a default value if its not set
func GetEnvDefault(key string, defaultValue string) string {
val, exists := os.LookupEnv(key)
if !exists {
return defaultValue
}
return val
}
// 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 {
val, exists := os.LookupEnv(key)
if !exists {
return defaultValue
}
intVal, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return defaultValue
}
return intVal
}

View File

@@ -4,6 +4,7 @@ import (
"database/sql"
"net/http"
"projectreshoot/config"
"projectreshoot/handlers"
"projectreshoot/view/page"
@@ -14,7 +15,7 @@ import (
func addRoutes(
mux *http.ServeMux,
logger *zerolog.Logger,
config *Config,
config *config.Config,
conn *sql.DB,
) {
// Static files

View File

@@ -4,6 +4,7 @@ import (
"database/sql"
"net/http"
"projectreshoot/config"
"projectreshoot/middleware"
"github.com/rs/zerolog"
@@ -11,7 +12,7 @@ import (
// Returns a new http.Handler with all the routes and middleware added
func NewServer(
config *Config,
config *config.Config,
logger *zerolog.Logger,
conn *sql.DB,
) http.Handler {