37 lines
842 B
Go
37 lines
842 B
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
Addr, DatabaseURL, RedisURL string
|
|
Origins map[string]bool
|
|
SessionTTL time.Duration
|
|
}
|
|
|
|
func Load() (Config, error) {
|
|
c := Config{
|
|
Addr: os.Getenv("XHCHAT_ADDR"),
|
|
DatabaseURL: os.Getenv("DATABASE_URL"),
|
|
RedisURL: os.Getenv("REDIS_URL"),
|
|
Origins: make(map[string]bool),
|
|
SessionTTL: 12 * time.Hour,
|
|
}
|
|
if c.Addr == "" {
|
|
c.Addr = "127.0.0.1:8080"
|
|
}
|
|
for _, origin := range strings.Split(os.Getenv("XHCHAT_ORIGINS"), ",") {
|
|
if origin = strings.TrimSpace(origin); origin != "" {
|
|
c.Origins[origin] = true
|
|
}
|
|
}
|
|
if c.DatabaseURL == "" || c.RedisURL == "" || len(c.Origins) == 0 {
|
|
return Config{}, errors.New("DATABASE_URL, REDIS_URL and XHCHAT_ORIGINS are required")
|
|
}
|
|
return c, nil
|
|
}
|