add reading configs
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s

This commit is contained in:
Thomas Peetz
2026-09-23 23:19:10 +02:00
parent a0ad4e5c26
commit ba83d70764
6 changed files with 116 additions and 24 deletions
+51 -9
View File
@@ -2,6 +2,7 @@ package main
import (
// "kontor-api-gin/pkg/handler"
"fmt"
"html/template"
"kontor-api-gin/assets"
"kontor-api-gin/pkg/globals"
@@ -13,16 +14,53 @@ import (
"log/slog"
"net/http"
"os"
"strings"
"strconv"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
gin_slog "github.com/gin-contrib/slog"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
func initConfig() error {
viper.SetConfigName("kontor-gin")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.config")
viper.AddConfigPath("/etc/kontor")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
// Read the config file
if err := viper.ReadInConfig(); err != nil {
// Handle the case where config file is not found
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
slog.Info("No config file found, using defaults")
return nil
}
return fmt.Errorf("error reading config file: %w", err)
}
slog.Info("initConfig", "Using config file: ", viper.ConfigFileUsed())
return nil
}
func main() {
slog.Info("Kontor started")
viper.SetDefault("db.host", "localhost")
viper.SetDefault("db.port", 5432)
viper.SetDefault("db.name", "kontor")
viper.SetDefault("db.user", "kontor")
viper.SetDefault("db.password", "kontor")
viper.SetDefault("port", 8550)
if err := initConfig(); err != nil {
log.Fatalf("Failed to initialize config: %v", err)
}
// Set Gin mode based on environment
// Production mode disables debug output
if os.Getenv("GIN_MODE") == "release" {
@@ -63,22 +101,26 @@ func main() {
routes.SetupComicRoutes(api)
// Setup public Webapp routes
public := router.Group("/")
routes.PublicRoutes(public)
routes.PublicRoutes(router)
// Setup protected Webapp routes
protected := router.Group("/")
protected := router.Group("/auth")
protected.Use(middleware.AuthRequired)
routes.ProtectedRoutes(protected)
routes.ProtectedRoutes(router)
comics := router.Group("/comics")
comics.Use(middleware.AuthRequired)
routes.ComicRoutes(router)
media := router.Group("/media")
media.Use(middleware.AuthRequired)
routes.MediaRoutes(router)
// Start server
port := os.Getenv("PORT")
if port == "" {
port = "8550"
}
port := viper.GetInt("port")
slog.Info("Server starting", "port", port)
if err := router.Run(":" + port); err != nil {
if err := router.Run(":" + strconv.Itoa(port)); err != nil {
log.Fatal("Failed to start server:", err)
}