2018-12-24 18:45:15 +01:00
|
|
|
/*
|
2019-01-05 01:58:44 +01:00
|
|
|
* Copyright © 2018-2019 A Bunch Tell LLC.
|
2018-12-24 18:45:15 +01:00
|
|
|
*
|
|
|
|
* This file is part of WriteFreely.
|
|
|
|
*
|
|
|
|
* WriteFreely is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, included
|
|
|
|
* in the LICENSE file in this source code package.
|
|
|
|
*/
|
2018-12-31 07:05:26 +01:00
|
|
|
|
2019-01-05 01:58:44 +01:00
|
|
|
// Package config holds and assists in the configuration of a writefreely instance.
|
2018-10-15 20:44:15 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gopkg.in/ini.v1"
|
2019-05-13 02:11:53 +02:00
|
|
|
"strings"
|
2018-10-15 20:44:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-01-05 01:58:44 +01:00
|
|
|
// FileName is the default configuration file name
|
2018-10-25 15:15:10 +02:00
|
|
|
FileName = "config.ini"
|
2019-01-18 06:05:50 +01:00
|
|
|
|
|
|
|
UserNormal UserType = "user"
|
|
|
|
UserAdmin = "admin"
|
2018-10-15 20:44:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2019-01-18 06:05:50 +01:00
|
|
|
UserType string
|
|
|
|
|
2019-01-05 01:58:44 +01:00
|
|
|
// ServerCfg holds values that affect how the HTTP server runs
|
2018-10-15 20:44:15 +02:00
|
|
|
ServerCfg struct {
|
2018-11-08 07:31:01 +01:00
|
|
|
HiddenHost string `ini:"hidden_host"`
|
|
|
|
Port int `ini:"port"`
|
2018-11-26 16:50:36 +01:00
|
|
|
Bind string `ini:"bind"`
|
2018-11-08 04:13:16 +01:00
|
|
|
|
2018-11-22 00:26:19 +01:00
|
|
|
TLSCertPath string `ini:"tls_cert_path"`
|
|
|
|
TLSKeyPath string `ini:"tls_key_path"`
|
2019-07-21 02:49:20 +02:00
|
|
|
Autocert bool `ini:"autocert"`
|
2018-11-22 00:26:19 +01:00
|
|
|
|
2019-01-19 00:57:04 +01:00
|
|
|
TemplatesParentDir string `ini:"templates_parent_dir"`
|
|
|
|
StaticParentDir string `ini:"static_parent_dir"`
|
|
|
|
PagesParentDir string `ini:"pages_parent_dir"`
|
|
|
|
KeysParentDir string `ini:"keys_parent_dir"`
|
|
|
|
|
2018-11-08 04:13:16 +01:00
|
|
|
Dev bool `ini:"-"`
|
2018-10-15 20:44:15 +02:00
|
|
|
}
|
|
|
|
|
2019-01-05 01:58:44 +01:00
|
|
|
// DatabaseCfg holds values that determine how the application connects to a datastore
|
2018-10-15 20:44:15 +02:00
|
|
|
DatabaseCfg struct {
|
2018-10-17 04:31:27 +02:00
|
|
|
Type string `ini:"type"`
|
2018-12-08 19:34:29 +01:00
|
|
|
FileName string `ini:"filename"`
|
2018-10-17 04:31:27 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-05 01:58:44 +01:00
|
|
|
// AppCfg holds values that affect how the application functions
|
2018-10-15 20:44:15 +02:00
|
|
|
AppCfg struct {
|
2018-10-25 15:15:10 +02:00
|
|
|
SiteName string `ini:"site_name"`
|
2018-12-04 00:36:33 +01:00
|
|
|
SiteDesc string `ini:"site_description"`
|
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"`
|
2019-05-13 02:11:53 +02:00
|
|
|
Landing string `ini:"landing"`
|
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"`
|
2019-06-17 00:55:50 +02:00
|
|
|
|
|
|
|
// Access
|
|
|
|
Private bool `ini:"private"`
|
2018-12-10 22:02:42 +01:00
|
|
|
|
|
|
|
// Additional functions
|
2019-01-18 06:05:50 +01:00
|
|
|
LocalTimeline bool `ini:"local_timeline"`
|
|
|
|
UserInvites string `ini:"user_invites"`
|
2018-10-15 20:44:15 +02:00
|
|
|
}
|
|
|
|
|
2019-01-05 01:58:44 +01:00
|
|
|
// Config holds the complete configuration for running a writefreely instance
|
2018-10-15 20:44:15 +02:00
|
|
|
Config struct {
|
|
|
|
Server ServerCfg `ini:"server"`
|
|
|
|
Database DatabaseCfg `ini:"database"`
|
|
|
|
App AppCfg `ini:"app"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-01-05 01:58:44 +01:00
|
|
|
// New creates a new Config with sane defaults
|
2018-10-15 20:44:15 +02:00
|
|
|
func New() *Config {
|
2018-12-08 19:36:51 +01:00
|
|
|
c := &Config{
|
2018-10-15 20:44:15 +02:00
|
|
|
Server: ServerCfg{
|
|
|
|
Port: 8080,
|
2018-11-26 16:50:36 +01:00
|
|
|
Bind: "localhost", /* IPV6 support when not using localhost? */
|
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
|
|
|
},
|
|
|
|
}
|
2018-12-08 19:36:51 +01: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 20:44:15 +02:00
|
|
|
}
|
|
|
|
|
2019-01-05 01:58:44 +01:00
|
|
|
// IsSecureStandalone returns whether or not the application is running as a
|
|
|
|
// standalone server with TLS enabled.
|
2018-11-22 00:26:19 +01:00
|
|
|
func (cfg *Config) IsSecureStandalone() bool {
|
|
|
|
return cfg.Server.Port == 443 && cfg.Server.TLSCertPath != "" && cfg.Server.TLSKeyPath != ""
|
|
|
|
}
|
|
|
|
|
2019-05-13 02:11:53 +02:00
|
|
|
func (ac *AppCfg) LandingPath() string {
|
|
|
|
if !strings.HasPrefix(ac.Landing, "/") {
|
|
|
|
return "/" + ac.Landing
|
|
|
|
}
|
|
|
|
return ac.Landing
|
|
|
|
}
|
|
|
|
|
2019-01-05 01:58:44 +01:00
|
|
|
// Load reads the given configuration file, then parses and returns it as a Config.
|
2018-12-08 23:49:19 +01:00
|
|
|
func Load(fname string) (*Config, error) {
|
|
|
|
if fname == "" {
|
|
|
|
fname = FileName
|
|
|
|
}
|
|
|
|
cfg, err := ini.Load(fname)
|
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
|
|
|
|
}
|
|
|
|
|
2019-01-05 01:58:44 +01:00
|
|
|
// Save writes the given Config to the given file.
|
2018-12-08 23:49:19 +01:00
|
|
|
func Save(uc *Config, fname string) error {
|
2018-10-15 20:44:15 +02:00
|
|
|
cfg := ini.Empty()
|
|
|
|
err := ini.ReflectFrom(cfg, uc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-08 23:49:19 +01:00
|
|
|
if fname == "" {
|
|
|
|
fname = FileName
|
|
|
|
}
|
|
|
|
return cfg.SaveTo(fname)
|
2018-10-15 20:44:15 +02:00
|
|
|
}
|