|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.phc.dm.unipi.it/phc/website/libs/sl"
|
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Mode string
|
|
|
|
Host string
|
|
|
|
|
|
|
|
NpmCommand string
|
|
|
|
}
|
|
|
|
|
|
|
|
var Slot = sl.NewSlot[Config]()
|
|
|
|
|
|
|
|
func setFromEnvOrDefault(target *string, m map[string]string, key string, defaultValue string) {
|
|
|
|
v, ok := m[key]
|
|
|
|
if ok {
|
|
|
|
*target = v
|
|
|
|
} else {
|
|
|
|
*target = defaultValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Configure(l *sl.ServiceLocator) (Config, error) {
|
|
|
|
env, err := godotenv.Read(".env")
|
|
|
|
if err != nil {
|
|
|
|
return Config{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var cfg Config
|
|
|
|
|
|
|
|
setFromEnvOrDefault(&cfg.Mode, env, "MODE", "production")
|
|
|
|
setFromEnvOrDefault(&cfg.Host, env, "HOST", ":4000")
|
|
|
|
setFromEnvOrDefault(&cfg.NpmCommand, env, "NPM_COMMAND", "npm")
|
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var ExampleProductionConfig = Config{
|
|
|
|
Mode: "production",
|
|
|
|
Host: ":4000",
|
|
|
|
|
|
|
|
NpmCommand: "npm",
|
|
|
|
}
|
|
|
|
|
|
|
|
var ExampleDevelopmentConfig = Config{
|
|
|
|
Mode: "development",
|
|
|
|
Host: ":4000",
|
|
|
|
|
|
|
|
NpmCommand: "npm",
|
|
|
|
}
|