package main

import (
	"context"
	"errors"
	"fmt"
	"log/slog"
	"net/http"
	"os"

	"update_server/internal/app"
	"update_server/internal/config"
)

func main() {
	cfg, err := config.Load()
	if err != nil {
		exitWithError(err)
	}

	logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: cfg.LogLevel}))

	application, err := app.New(cfg, logger)
	if err != nil {
		exitWithError(err)
	}

	if err := application.Run(context.Background()); err != nil && !errors.Is(err, http.ErrServerClosed) {
		exitWithError(err)
	}
}

func exitWithError(err error) {
	fmt.Fprintf(os.Stderr, "update_server: %v\n", err)
	os.Exit(1)
}
