LocalhostBunny/config.go

167 lines
4.3 KiB
Go
Raw Normal View History

2023-12-01 09:56:06 +01:00
package bunny
import (
"github.com/rs/xid"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/confext"
"os"
"time"
)
const APILevel = 1
2023-12-01 11:24:22 +01:00
var SelfProcessID int
2023-12-01 09:56:06 +01:00
2023-12-01 11:24:22 +01:00
type Config struct {
2023-12-01 13:44:58 +01:00
Namespace string
GinDebug bool `env:"GINDEBUG"`
ReturnRawErrors bool `env:"RETURNERRORS"`
Custom404 bool `env:"CUSTOM404"`
LogLevel zerolog.Level `env:"LOGLEVEL"`
ServerIP string `env:"IP"`
ServerPort int `env:"PORT"`
RequestTimeout time.Duration `env:"REQUEST_TIMEOUT"`
Cors bool `env:"CORS"`
VerifyConnTimeoutHTML time.Duration `env:"VERIFY_CONN_TIMEOUT_HTML"`
VerifyConnTimeoutAPI time.Duration `env:"VERIFY_CONN_TIMEOUT_API"`
LiveReload *string `env:"LIVERELOAD"`
CacheDuration time.Duration `env:"CACHE_DURATION"`
2023-12-01 09:56:06 +01:00
}
var Conf Config
var configLocHost = func() Config {
return Config{
2023-12-01 13:44:58 +01:00
Namespace: "local",
GinDebug: true,
ServerIP: "0.0.0.0",
ServerPort: 80,
Custom404: true,
ReturnRawErrors: true,
RequestTimeout: 16 * time.Second,
LogLevel: zerolog.DebugLevel,
Cors: true,
VerifyConnTimeoutAPI: 2 * time.Second,
VerifyConnTimeoutHTML: 500 * time.Millisecond,
LiveReload: nil,
CacheDuration: 8 * time.Second,
2023-12-01 09:56:06 +01:00
}
}
var configLocDocker = func() Config {
return Config{
2023-12-01 13:44:58 +01:00
Namespace: "local-docker",
GinDebug: true,
ServerIP: "0.0.0.0",
ServerPort: 80,
Custom404: true,
ReturnRawErrors: true,
RequestTimeout: 16 * time.Second,
LogLevel: zerolog.DebugLevel,
Cors: true,
VerifyConnTimeoutAPI: 2 * time.Second,
VerifyConnTimeoutHTML: 500 * time.Millisecond,
LiveReload: nil,
CacheDuration: 8 * time.Second,
2023-12-01 09:56:06 +01:00
}
}
var configDev = func() Config {
return Config{
2023-12-01 13:44:58 +01:00
Namespace: "develop",
GinDebug: true,
ServerIP: "0.0.0.0",
ServerPort: 80,
Custom404: false,
ReturnRawErrors: false,
RequestTimeout: 16 * time.Second,
LogLevel: zerolog.DebugLevel,
Cors: false,
VerifyConnTimeoutAPI: 2 * time.Second,
VerifyConnTimeoutHTML: 500 * time.Millisecond,
LiveReload: nil,
CacheDuration: 8 * time.Second,
2023-12-01 09:56:06 +01:00
}
}
var configStag = func() Config {
return Config{
2023-12-01 13:44:58 +01:00
Namespace: "staging",
GinDebug: true,
ServerIP: "0.0.0.0",
ServerPort: 80,
Custom404: false,
ReturnRawErrors: false,
RequestTimeout: 16 * time.Second,
LogLevel: zerolog.DebugLevel,
Cors: false,
VerifyConnTimeoutAPI: 2 * time.Second,
VerifyConnTimeoutHTML: 500 * time.Millisecond,
LiveReload: nil,
CacheDuration: 8 * time.Second,
2023-12-01 09:56:06 +01:00
}
}
var configProd = func() Config {
return Config{
2023-12-01 13:44:58 +01:00
Namespace: "production",
GinDebug: false,
ServerIP: "0.0.0.0",
ServerPort: 80,
Custom404: false,
ReturnRawErrors: false,
RequestTimeout: 16 * time.Second,
LogLevel: zerolog.InfoLevel,
Cors: false,
VerifyConnTimeoutAPI: 2 * time.Second,
VerifyConnTimeoutHTML: 500 * time.Millisecond,
LiveReload: nil,
CacheDuration: 8 * time.Second,
2023-12-01 09:56:06 +01:00
}
}
var allConfig = map[string]func() Config{
"local-host": configLocHost,
"local-docker": configLocDocker,
"develop": configDev,
"staging": configStag,
"production": configProd,
}
var instID xid.ID
func InstanceID() string {
return instID.String()
}
func getConfig(ns string) (Config, bool) {
if ns == "" {
2023-12-01 13:44:58 +01:00
ns = "production"
2023-12-01 09:56:06 +01:00
}
if cfn, ok := allConfig[ns]; ok {
c := cfn()
err := confext.ApplyEnvOverrides("BUNNY_", &c, "_")
if err != nil {
panic(err)
}
return c, true
}
return Config{}, false
}
func init() {
instID = xid.New()
ns := os.Getenv("CONF_NS")
cfg, ok := getConfig(ns)
if !ok {
log.Fatal().Str("ns", ns).Msg("Unknown config-namespace")
}
Conf = cfg
2023-12-01 11:24:22 +01:00
SelfProcessID = os.Getpid()
2023-12-01 09:56:06 +01:00
}