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 {
|
2018-11-08 06:31:01 +00:00
|
|
|
HiddenHost string `ini:"hidden_host"`
|
|
|
|
Port int `ini:"port"`
|
2018-11-08 03:13:16 +00:00
|
|
|
|
2018-11-21 23:26:19 +00:00
|
|
|
TLSCertPath string `ini:"tls_cert_path"`
|
|
|
|
TLSKeyPath string `ini:"tls_key_path"`
|
|
|
|
|
2018-11-08 03:13:16 +00:00
|
|
|
Dev bool `ini:"-"`
|
2018-10-15 18:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-27 21:02:40 +00:00
|
|
|
Host string `ini:"host"`
|
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-11-08 03:06:34 +00:00
|
|
|
MaxBlogs int `ini:"max_blogs"`
|
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{
|
|
|
|
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-27 21:02:40 +00:00
|
|
|
Host: "http://localhost:8080",
|
2018-10-25 13:15:10 +00:00
|
|
|
Theme: "write",
|
|
|
|
WebFonts: true,
|
|
|
|
SingleUser: true,
|
|
|
|
MinUsernameLen: 3,
|
2018-11-08 03:06:34 +00:00
|
|
|
MaxBlogs: 1,
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 23:26:19 +00:00
|
|
|
func (cfg *Config) IsSecureStandalone() bool {
|
|
|
|
return cfg.Server.Port == 443 && cfg.Server.TLSCertPath != "" && cfg.Server.TLSKeyPath != ""
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|