80 lines
2.0 KiB
Go
80 lines
2.0 KiB
Go
package config
|
|
|
|
import (
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/akyaiy/GSfass/core/config"
|
|
)
|
|
|
|
type StaticConfig struct {
|
|
Enabled bool `mapstructure:"enabled"`
|
|
Dir string `mapstructure:"static_dir"`
|
|
IndexFile string `mapstructure:"index_file"`
|
|
}
|
|
|
|
type BlockConfig struct {
|
|
Enabled bool `mapstructure:"enabled"`
|
|
BlockDir string `mapstructure:"block_dir"`
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
StaticConfig StaticConfig `mapstructure:"static"`
|
|
BlockConfig BlockConfig `mapstructure:"block"`
|
|
Port int `mapstructure:"port"`
|
|
Addr string `mapstructure:"address"`
|
|
LogPath string `mapstructure:"log_path"`
|
|
TimeoutSeconds time.Duration `mapstructure:"timeout_seconds"`
|
|
}
|
|
|
|
type FuncConfig struct {
|
|
FunctionDir string `mapstructure:"func_dir"`
|
|
}
|
|
|
|
type Auth struct {
|
|
RefreshTokenTTL time.Duration `mapstructure:"refresh_token_ttl"`
|
|
AccessTokenTTL time.Duration `mapstructure:"access_token_ttl"`
|
|
}
|
|
|
|
type Config struct {
|
|
Server ServerConfig `mapstructure:"server"`
|
|
Functions FuncConfig `mapstructure:"functions"`
|
|
Auth Auth `mapstructure:"auth"`
|
|
}
|
|
|
|
var configPath atomic.Value // string
|
|
var defaults = map[string]any{
|
|
"server.port": 8080,
|
|
"server.address": "127.0.0.0",
|
|
"server.timeout_seconds": 5,
|
|
"server.log_path": "./logs/server.log",
|
|
"server.static.enabled": true,
|
|
"server.static.static_dir": "./static",
|
|
"server.static.index_file": "index.html",
|
|
"server.block.enabled": true,
|
|
"server.block.block_dir": "./blocks",
|
|
|
|
"functions.func_dir": "./functions",
|
|
|
|
"auth.refresh_token_ttl": 24 * time.Hour,
|
|
"auth.access_token_ttl": 15 * time.Minute,
|
|
}
|
|
|
|
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 := read(&cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &cfg, nil
|
|
}
|
|
|
|
func ReloadConfig(cfg *Config) error {
|
|
return read(cfg)
|
|
}
|