2018-10-15 18:44:15 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-10-25 13:15:10 +00:00
|
|
|
FileName = "config.ini"
|
2018-10-15 18:44:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
ServerCfg struct {
|
|
|
|
Host string `ini:"host"`
|
|
|
|
Port int `ini:"port"`
|
|
|
|
}
|
|
|
|
|
|
|
|
DatabaseCfg struct {
|
2018-10-17 02:31:27 +00:00
|
|
|
Type string `ini:"type"`
|
|
|
|
User string `ini:"username"`
|
|
|
|
Password string `ini:"password"`
|
|
|
|
Database string `ini:"database"`
|
|
|
|
Host string `ini:"host"`
|
|
|
|
Port int `ini:"port"`
|
2018-10-15 18:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AppCfg struct {
|
2018-10-25 13:15:10 +00:00
|
|
|
SiteName string `ini:"site_name"`
|
2018-10-15 18:44:15 +00:00
|
|
|
|
2018-10-25 13:15:10 +00:00
|
|
|
// Site appearance
|
|
|
|
Theme string `ini:"theme"`
|
|
|
|
JSDisabled bool `ini:"disable_js"`
|
|
|
|
WebFonts bool `ini:"webfonts"`
|
2018-10-15 18:44:15 +00:00
|
|
|
|
2018-10-25 13:15:10 +00:00
|
|
|
// Users
|
|
|
|
SingleUser bool `ini:"single_user"`
|
|
|
|
OpenRegistration bool `ini:"open_registration"`
|
|
|
|
MinUsernameLen int `ini:"min_username_len"`
|
2018-10-15 18:44:15 +00:00
|
|
|
|
2018-10-25 13:15:10 +00:00
|
|
|
// Federation
|
|
|
|
Federation bool `ini:"federation"`
|
|
|
|
PublicStats bool `ini:"public_stats"`
|
|
|
|
Private bool `ini:"private"`
|
2018-10-15 18:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Config struct {
|
|
|
|
Server ServerCfg `ini:"server"`
|
|
|
|
Database DatabaseCfg `ini:"database"`
|
|
|
|
App AppCfg `ini:"app"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func New() *Config {
|
|
|
|
return &Config{
|
|
|
|
Server: ServerCfg{
|
|
|
|
Host: "http://localhost:8080",
|
|
|
|
Port: 8080,
|
|
|
|
},
|
|
|
|
Database: DatabaseCfg{
|
|
|
|
Type: "mysql",
|
|
|
|
Host: "localhost",
|
2018-10-16 20:57:55 +00:00
|
|
|
Port: 3306,
|
2018-10-15 18:44:15 +00:00
|
|
|
},
|
|
|
|
App: AppCfg{
|
2018-10-25 13:15:10 +00:00
|
|
|
Theme: "write",
|
|
|
|
WebFonts: true,
|
|
|
|
SingleUser: true,
|
|
|
|
MinUsernameLen: 3,
|
2018-10-15 18:44:15 +00:00
|
|
|
Federation: true,
|
2018-10-17 22:57:37 +00:00
|
|
|
PublicStats: true,
|
2018-10-15 18:44:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Load() (*Config, error) {
|
2018-10-25 13:15:10 +00:00
|
|
|
cfg, err := ini.Load(FileName)
|
2018-10-15 18:44:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse INI file
|
|
|
|
uc := &Config{}
|
|
|
|
err = cfg.MapTo(uc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return uc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Save(uc *Config) error {
|
|
|
|
cfg := ini.Empty()
|
|
|
|
err := ini.ReflectFrom(cfg, uc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-25 13:15:10 +00:00
|
|
|
return cfg.SaveTo(FileName)
|
2018-10-15 18:44:15 +00:00
|
|
|
}
|