package config import ( "bufio" "fmt" "log/slog" "os" "go.lindenii.runxiyu.org/forge/forged/internal/common/scfg" ) type Config struct { DB DB `scfgs:"db"` Web Web `scfgs:"web"` Hooks Hooks `scfgs:"hooks"` LMTP LMTP `scfgs:"lmtp"` SSH SSH `scfgs:"ssh"` IRC IRC `scfgs:"irc"` Git Git `scfgs:"git"` General General `scfgs:"general"` Pprof Pprof `scfgs:"pprof"` } type DB struct { Conn string `scfgs:"conn"` } type Web struct { Net string `scfgs:"net"` Addr string `scfgs:"addr"` Root string `scfgs:"root"` CookieExpiry int `scfgs:"cookie_expiry"` ReadTimeout uint32 `scfgs:"read_timeout"` WriteTimeout uint32 `scfgs:"write_timeout"` IdleTimeout uint32 `scfgs:"idle_timeout"` MaxHeaderBytes int `scfgs:"max_header_bytes"` ReverseProxy bool `scfgs:"reverse_proxy"` ShutdownTimeout uint32 `scfgs:"shutdown_timeout"` TemplatesPath string `scfgs:"templates_path"` StaticPath string `scfgs:"static_path"` } type Hooks struct { Socket string `scfgs:"socket"` Execs string `scfgs:"execs"` } type LMTP struct { Socket string `scfgs:"socket"` Domain string `scfgs:"domain"` MaxSize int64 `scfgs:"max_size"` WriteTimeout uint32 `scfgs:"write_timeout"` ReadTimeout uint32 `scfgs:"read_timeout"` } type SSH struct { Net string `scfgs:"net"` Addr string `scfgs:"addr"` Key string `scfgs:"key"` Root string `scfgs:"root"` ShutdownTimeout uint32 `scfgs:"shutdown_timeout"` } type IRC struct { Net string `scfgs:"net"` Addr string `scfgs:"addr"` TLS bool `scfgs:"tls"` SendQ uint `scfgs:"sendq"` Nick string `scfgs:"nick"` User string `scfgs:"user"` Gecos string `scfgs:"gecos"` } type Git struct { RepoDir string `scfgs:"repo_dir"` Socket string `scfgs:"socket"` } type General struct { Title string `scfgs:"title"` } type Pprof struct { Net string `scfgs:"net"` Addr string `scfgs:"addr"` } func Open(path string) (config Config, err error) { var configFile *os.File configFile, err = os.Open(path) //#nosec G304 if err != nil { err = fmt.Errorf("open config file: %w", err) return config, err } defer func() { _ = configFile.Close() }() decoder := scfgs.NewDecoder(bufio.NewReader(configFile)) err = decoder.Decode(&config) if err != nil { err = fmt.Errorf("decode config file: %w", err) return config, err } for _, u := range decoder.UnknownDirectives() { slog.Warn("unknown configuration directive", "directive", u) } return config, err }