2018-10-15 20:44:15 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-10-25 15:15:10 +02:00
|
|
|
FileName = "config.ini"
|
2018-10-15 20:44:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
ServerCfg struct {
|
2018-11-08 07:31:01 +01:00
|
|
|
HiddenHost string `ini:"hidden_host"`
|
|
|
|
Port int `ini:"port"`
|
2018-11-08 04:13:16 +01:00
|
|
|
|
|
|
|
Dev bool `ini:"-"`
|
2018-10-15 20:44:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DatabaseCfg struct {
|
2018-10-17 04:31:27 +02: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 20:44:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AppCfg struct {
|
2018-10-25 15:15:10 +02:00
|
|
|
SiteName string `ini:"site_name"`
|
2018-10-27 23:02:40 +02:00
|
|
|
Host string `ini:"host"`
|
2018-10-15 20:44:15 +02:00
|
|
|
|
2018-10-25 15:15:10 +02:00
|
|
|
// Site appearance
|
|
|
|
Theme string `ini:"theme"`
|
|
|
|
JSDisabled bool `ini:"disable_js"`
|
|
|
|
WebFonts bool `ini:"webfonts"`
|
2018-10-15 20:44:15 +02:00
|
|
|
|
2018-10-25 15:15:10 +02:00
|
|
|
// Users
|
|
|
|
SingleUser bool `ini:"single_user"`
|
|
|
|
OpenRegistration bool `ini:"open_registration"`
|
|
|
|
MinUsernameLen int `ini:"min_username_len"`
|
2018-11-08 04:06:34 +01:00
|
|
|
MaxBlogs int `ini:"max_blogs"`
|
2018-10-15 20:44:15 +02:00
|
|
|
|
2018-10-25 15:15:10 +02:00
|
|
|
// Federation
|
|
|
|
Federation bool `ini:"federation"`
|
|
|
|
PublicStats bool `ini:"public_stats"`
|
|
|
|
Private bool `ini:"private"`
|
2018-10-15 20:44:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Config struct {
|
|
|
|
Server ServerCfg `ini:"server"`
|
|
|
|
Database DatabaseCfg `ini:"database"`
|
|
|
|
App AppCfg `ini:"app"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func New() *Config {
|
|
|
|
return &Config{
|
|
|
|
Server: ServerCfg{
|
|
|
|
Port: 8080,
|
|
|
|
},
|
|
|
|
Database: DatabaseCfg{
|
|
|
|
Type: "mysql",
|
|
|
|
Host: "localhost",
|
2018-10-16 22:57:55 +02:00
|
|
|
Port: 3306,
|
2018-10-15 20:44:15 +02:00
|
|
|
},
|
|
|
|
App: AppCfg{
|
2018-10-27 23:02:40 +02:00
|
|
|
Host: "http://localhost:8080",
|
2018-10-25 15:15:10 +02:00
|
|
|
Theme: "write",
|
|
|
|
WebFonts: true,
|
|
|
|
SingleUser: true,
|
|
|
|
MinUsernameLen: 3,
|
2018-11-08 04:06:34 +01:00
|
|
|
MaxBlogs: 1,
|
2018-10-15 20:44:15 +02:00
|
|
|
Federation: true,
|
2018-10-18 00:57:37 +02:00
|
|
|
PublicStats: true,
|
2018-10-15 20:44:15 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Load() (*Config, error) {
|
2018-10-25 15:15:10 +02:00
|
|
|
cfg, err := ini.Load(FileName)
|
2018-10-15 20:44:15 +02: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 15:15:10 +02:00
|
|
|
return cfg.SaveTo(FileName)
|
2018-10-15 20:44:15 +02:00
|
|
|
}
|