server with basic hot reload

This commit is contained in:
2025-11-30 10:09:53 +02:00
parent f1ed3c977a
commit 44e92bcfef
14 changed files with 700 additions and 18 deletions

View File

@@ -1,26 +1,43 @@
package config
import (
"sync/atomic"
"github.com/akyaiy/GSfass/core/config"
)
type ServerConfig struct {
Port int `mapstructure:"port"`
Addr string `mapstructure:"address"`
Port int `mapstructure:"port"`
Addr string `mapstructure:"address"`
StaticFilesPath string `mapstructure:"static_dir"`
LogPath string `mapstructure:"log_path"`
}
type Config struct {
Server ServerConfig `mapstructure:"server"`
}
var configPath atomic.Value // string
var defaults = map[string]any{
"server.port": 8080,
"server.address": "127.0.0.0",
"server.static_dir": "./static",
}
func read(cfg *Config) error {
return config.Read().Config().FilePath(configPath.Load().(string)).SetBy(cfg).SetDefaults(defaults).End()
}
func LoadConfig(path string) (*Config, error) {
configPath.Store(path)
var cfg Config
err := config.Read().Config().FilePath(path).SetBy(&cfg).SetDefaults(map[string]any{
"server.port": 8080,
"server.address": "127.0.0.0",
}).End()
err := read(&cfg)
if err != nil {
return nil, err
}
return &cfg, nil
}
func ReloadConfig(cfg *Config) error {
return read(cfg)
}