mirror of
https://github.com/anotherhadi/iknowyou.git
synced 2026-04-12 00:47:26 +02:00
init
This commit is contained in:
64
back/config/env/env.go
vendored
Normal file
64
back/config/env/env.go
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
package env
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Port int
|
||||
ConfigPath string
|
||||
FrontDir string // when set, serves the Astro static build at "/"
|
||||
SearchTTL time.Duration
|
||||
CleanupInterval time.Duration
|
||||
Demo bool // when true, disables searches and config mutations
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
cfg := &Config{
|
||||
Port: 8080,
|
||||
ConfigPath: "/etc/iky/config.yaml",
|
||||
SearchTTL: 48 * time.Hour,
|
||||
CleanupInterval: time.Hour,
|
||||
}
|
||||
|
||||
if v := os.Getenv("IKY_PORT"); v != "" {
|
||||
p, err := strconv.Atoi(v)
|
||||
if err != nil || p < 1 || p > 65535 {
|
||||
return nil, fmt.Errorf("env: IKY_PORT %q is not a valid port number", v)
|
||||
}
|
||||
cfg.Port = p
|
||||
}
|
||||
|
||||
if v := os.Getenv("IKY_CONFIG"); v != "" {
|
||||
cfg.ConfigPath = v
|
||||
}
|
||||
|
||||
if v := os.Getenv("IKY_FRONT_DIR"); v != "" {
|
||||
cfg.FrontDir = v
|
||||
}
|
||||
|
||||
if v := os.Getenv("IKY_SEARCH_TTL"); v != "" {
|
||||
d, err := time.ParseDuration(v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("env: IKY_SEARCH_TTL %q is not a valid duration", v)
|
||||
}
|
||||
cfg.SearchTTL = d
|
||||
}
|
||||
|
||||
if v := os.Getenv("IKY_CLEANUP_INTERVAL"); v != "" {
|
||||
d, err := time.ParseDuration(v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("env: IKY_CLEANUP_INTERVAL %q is not a valid duration", v)
|
||||
}
|
||||
cfg.CleanupInterval = d
|
||||
}
|
||||
|
||||
if v := os.Getenv("IKY_DEMO"); v == "true" || v == "1" {
|
||||
cfg.Demo = true
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
Reference in New Issue
Block a user