[PHASE-8] Add config load

This commit is contained in:
Savely Krendelhoff 2025-08-23 16:06:16 +07:00
parent 0bf84576a8
commit 451403f8d1
No known key found for this signature in database
GPG key ID: F70DFD34F40238DE

View file

@ -2,32 +2,48 @@ package main
import ( import (
"context" "context"
"flag"
"log/slog" "log/slog"
"net/http"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"time" "time"
"hash-of-wisdom/internal/config"
"hash-of-wisdom/internal/lib/sl" "hash-of-wisdom/internal/lib/sl"
"hash-of-wisdom/internal/pow/challenge" "hash-of-wisdom/internal/pow/challenge"
"hash-of-wisdom/internal/quotes" "hash-of-wisdom/internal/quotes"
"hash-of-wisdom/internal/server" "hash-of-wisdom/internal/server"
"hash-of-wisdom/internal/service" "hash-of-wisdom/internal/service"
"github.com/prometheus/client_golang/prometheus/promhttp"
_ "net/http/pprof"
) )
func main() { func main() {
addr := ":8080" configPath := flag.String("config", "", "path to configuration file")
if len(os.Args) > 1 { flag.Parse()
addr = os.Args[1]
// Load configuration
cfg, err := config.Load(*configPath)
if err != nil {
slog.Error("failed to load config", sl.Err(err))
os.Exit(1)
} }
logger := slog.Default() logger := slog.Default()
logger.Info("starting word of wisdom server", "address", addr) logger.Info("starting word of wisdom server", "address", cfg.Server.Address)
// Create components // Create components using config
challengeConfig, err := challenge.NewConfig() challengeConfig, err := challenge.NewConfig(
challenge.WithDefaultDifficulty(cfg.PoW.Difficulty),
challenge.WithMaxDifficulty(cfg.PoW.MaxDifficulty),
challenge.WithChallengeTTL(cfg.PoW.TTL),
challenge.WithHMACSecret([]byte(cfg.PoW.HMACSecret)),
)
if err != nil { if err != nil {
logger.Error("failed to create config", sl.Err(err)) logger.Error("failed to create challenge config", sl.Err(err))
os.Exit(1) os.Exit(1)
} }
generator := challenge.NewGenerator(challengeConfig) generator := challenge.NewGenerator(challengeConfig)