fix: restructure env handling for better testing capabillities #181
All checks were successful
Build Docker Image / Explore-Gitea-Actions (push) Successful in 47s
Build and Push Docker Image / Explore-Gitea-Actions (push) Successful in 55s

This commit was merged in pull request #195.
This commit is contained in:
2024-09-29 23:51:23 +02:00
parent 25e82be339
commit a70138f2f7
15 changed files with 191 additions and 152 deletions

21
main.go
View File

@@ -2,7 +2,9 @@ package main
import (
"me-fit/handler"
"me-fit/types"
"me-fit/utils"
"os"
"context"
"database/sql"
@@ -20,21 +22,22 @@ import (
)
func main() {
run(context.Background())
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
run(context.Background(), os.Getenv)
}
func run(ctx context.Context) {
func run(ctx context.Context, env func(string) string) {
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer cancel()
slog.Info("Starting server...")
// init env
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
utils.MustInitEnv()
// init server settings
serverSettings := types.NewServerSettingsFromEnv(env)
// init db
db, err := sql.Open("sqlite3", "./data.db")
@@ -51,7 +54,7 @@ func run(ctx context.Context) {
}
httpServer := &http.Server{
Addr: ":8080",
Handler: handler.GetHandler(db),
Handler: handler.GetHandler(db, serverSettings),
}
go startServer(prometheusServer)
go startServer(httpServer)