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-26 15:50:36 +00:00
|
|
|
Bind string `ini:"bind"`
|
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"`
|
2018-12-08 18:34:29 +00:00
|
|
|
FileName string `ini:"filename"`
|
2018-10-17 02:31:27 +00:00
|
|
|
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-12-03 23:36:33 +00:00
|
|
|
SiteDesc string `ini:"site_description"`
|
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 {
|
2018-12-08 18:36:51 +00:00
|
|
|
c := &Config{
|
2018-10-15 18:44:15 +00:00
|
|
|
Server: ServerCfg{
|
|
|
|
Port: 8080,
|
2018-11-26 15:50:36 +00:00
|
|
|
Bind: "localhost", /* IPV6 support when not using localhost? */
|
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-12-08 18:36:51 +00:00
|
|
|
c.UseMySQL(true)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// UseMySQL resets the Config's Database to use default values for a MySQL setup.
|
|
|
|
func (cfg *Config) UseMySQL(fresh bool) {
|
|
|
|
cfg.Database.Type = "mysql"
|
|
|
|
if fresh {
|
|
|
|
cfg.Database.Host = "localhost"
|
|
|
|
cfg.Database.Port = 3306
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UseSQLite resets the Config's Database to use default values for a SQLite setup.
|
|
|
|
func (cfg *Config) UseSQLite(fresh bool) {
|
|
|
|
cfg.Database.Type = "sqlite3"
|
|
|
|
if fresh {
|
|
|
|
cfg.Database.FileName = "writefreely.db"
|
|
|
|
}
|
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
|
|
|
}
|