82 lines
1.2 KiB
Go
82 lines
1.2 KiB
Go
package config
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
|
|
ini "gopkg.in/ini.v1"
|
|
)
|
|
|
|
type Config struct {
|
|
Debug bool
|
|
HTTP HTTPConfig
|
|
Gitea GiteaConfig
|
|
Data DataConfig
|
|
}
|
|
|
|
type HTTPConfig struct {
|
|
Address string
|
|
PublicDir string
|
|
}
|
|
|
|
type GiteaConfig struct {
|
|
BaseURL string
|
|
ClientID string
|
|
ClientSecret string
|
|
RedirectURL string
|
|
LogoutURL string
|
|
AuthURL string
|
|
TokenURL string
|
|
Scopes []string
|
|
APIBaseURL string
|
|
}
|
|
|
|
type DataConfig struct {
|
|
DBPath string
|
|
}
|
|
|
|
// NewFromFile retrieves the configuration from the given file
|
|
func NewFromFile(filepath string) (*Config, error) {
|
|
config := NewDefault()
|
|
|
|
cfg, err := ini.Load(filepath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cfg.ValueMapper = os.ExpandEnv
|
|
|
|
if err := cfg.MapTo(config); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return config, nil
|
|
}
|
|
|
|
func NewDefault() *Config {
|
|
return &Config{
|
|
Debug: false,
|
|
HTTP: HTTPConfig{
|
|
Address: ":3000",
|
|
PublicDir: "${GENGITKAN_HTTP_PUBDIR}",
|
|
},
|
|
Gitea: GiteaConfig{},
|
|
Data: DataConfig{
|
|
DBPath: "${GENGITKAN_DATA_DBPATH}",
|
|
},
|
|
}
|
|
}
|
|
|
|
func Dump(config *Config, w io.Writer) error {
|
|
cfg := ini.Empty()
|
|
if err := cfg.ReflectFrom(config); err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err := cfg.WriteTo(w); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|