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 (
|
2019-05-13 02:11:53 +02:00
|
|
|
"strings"
|
2019-08-30 00:05:59 +02:00
|
|
|
|
|
|
|
"gopkg.in/ini.v1"
|
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"`
|
|
|
|
|
2020-01-03 19:50:21 +01:00
|
|
|
HashSeed string `ini:"hash_seed"`
|
|
|
|
|
2020-03-02 02:12:47 +01:00
|
|
|
GopherPort int `ini:"gopher_port"`
|
|
|
|
|
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"`
|
2020-04-16 06:04:16 +02:00
|
|
|
TLS bool `ini:"tls"`
|
2018-10-15 20:44:15 +02:00
|
|
|
}
|
|
|
|
|
2019-12-28 21:15:47 +01:00
|
|
|
WriteAsOauthCfg struct {
|
2020-01-08 03:47:23 +01:00
|
|
|
ClientID string `ini:"client_id"`
|
|
|
|
ClientSecret string `ini:"client_secret"`
|
|
|
|
AuthLocation string `ini:"auth_location"`
|
|
|
|
TokenLocation string `ini:"token_location"`
|
|
|
|
InspectLocation string `ini:"inspect_location"`
|
|
|
|
CallbackProxy string `ini:"callback_proxy"`
|
|
|
|
CallbackProxyAPI string `ini:"callback_proxy_api"`
|
2019-12-28 21:15:47 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 11:51:12 +01:00
|
|
|
GitlabOauthCfg struct {
|
|
|
|
ClientID string `ini:"client_id"`
|
|
|
|
ClientSecret string `ini:"client_secret"`
|
2020-03-16 09:00:04 +01:00
|
|
|
Host string `ini:"host"`
|
|
|
|
DisplayName string `ini:"display_name"`
|
2020-03-11 11:51:12 +01:00
|
|
|
CallbackProxy string `ini:"callback_proxy"`
|
|
|
|
CallbackProxyAPI string `ini:"callback_proxy_api"`
|
|
|
|
}
|
|
|
|
|
2020-08-19 19:26:15 +02:00
|
|
|
GiteaOauthCfg struct {
|
|
|
|
ClientID string `ini:"client_id"`
|
|
|
|
ClientSecret string `ini:"client_secret"`
|
|
|
|
Host string `ini:"host"`
|
|
|
|
DisplayName string `ini:"display_name"`
|
|
|
|
CallbackProxy string `ini:"callback_proxy"`
|
|
|
|
CallbackProxyAPI string `ini:"callback_proxy_api"`
|
|
|
|
}
|
|
|
|
|
2019-12-28 21:15:47 +01:00
|
|
|
SlackOauthCfg struct {
|
2020-01-08 03:47:23 +01:00
|
|
|
ClientID string `ini:"client_id"`
|
|
|
|
ClientSecret string `ini:"client_secret"`
|
|
|
|
TeamID string `ini:"team_id"`
|
|
|
|
CallbackProxy string `ini:"callback_proxy"`
|
|
|
|
CallbackProxyAPI string `ini:"callback_proxy_api"`
|
2019-12-27 19:40:11 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 04:09:14 +02:00
|
|
|
GenericOauthCfg struct {
|
|
|
|
ClientID string `ini:"client_id"`
|
|
|
|
ClientSecret string `ini:"client_secret"`
|
|
|
|
Host string `ini:"host"`
|
|
|
|
DisplayName string `ini:"display_name"`
|
|
|
|
CallbackProxy string `ini:"callback_proxy"`
|
|
|
|
CallbackProxyAPI string `ini:"callback_proxy_api"`
|
|
|
|
TokenEndpoint string `ini:"token_endpoint"`
|
|
|
|
InspectEndpoint string `ini:"inspect_endpoint"`
|
|
|
|
AuthEndpoint string `ini:"auth_endpoint"`
|
2020-06-06 23:52:26 +02:00
|
|
|
AllowDisconnect bool `ini:"allow_disconnect"`
|
2020-05-31 04:09:14 +02:00
|
|
|
}
|
2020-03-28 05:28:19 +01: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"`
|
2019-08-05 04:20:30 +02:00
|
|
|
Editor string `ini:"editor"`
|
2018-10-25 15:15:10 +02:00
|
|
|
JSDisabled bool `ini:"disable_js"`
|
|
|
|
WebFonts bool `ini:"webfonts"`
|
2019-05-13 02:11:53 +02:00
|
|
|
Landing string `ini:"landing"`
|
2019-08-05 15:27:51 +02:00
|
|
|
SimpleNav bool `ini:"simple_nav"`
|
2019-08-01 04:53:10 +02:00
|
|
|
WFModesty bool `ini:"wf_modesty"`
|
2019-08-09 19:53:41 +02:00
|
|
|
|
|
|
|
// Site functionality
|
|
|
|
Chorus bool `ini:"chorus"`
|
2020-02-14 20:08:53 +01:00
|
|
|
Forest bool `ini:"forest"` // The admin cares about the forest, not the trees. Hide unnecessary technical info.
|
2019-08-09 19:53:41 +02:00
|
|
|
DisableDrafts bool `ini:"disable_drafts"`
|
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"`
|
2019-08-01 04:18:40 +02:00
|
|
|
|
|
|
|
// Defaults
|
|
|
|
DefaultVisibility string `ini:"default_visibility"`
|
2019-08-30 00:05:59 +02:00
|
|
|
|
|
|
|
// Check for Updates
|
|
|
|
UpdateChecks bool `ini:"update_checks"`
|
2020-06-14 00:27:25 +02:00
|
|
|
|
|
|
|
// Disable password authentication if use only Oauth
|
|
|
|
DisablePasswordAuth bool `ini:"disable_password_auth"`
|
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 {
|
2019-12-28 21:15:47 +01:00
|
|
|
Server ServerCfg `ini:"server"`
|
|
|
|
Database DatabaseCfg `ini:"database"`
|
|
|
|
App AppCfg `ini:"app"`
|
|
|
|
SlackOauth SlackOauthCfg `ini:"oauth.slack"`
|
|
|
|
WriteAsOauth WriteAsOauthCfg `ini:"oauth.writeas"`
|
2020-03-11 11:51:12 +01:00
|
|
|
GitlabOauth GitlabOauthCfg `ini:"oauth.gitlab"`
|
2020-04-03 12:26:59 +02:00
|
|
|
GiteaOauth GiteaOauthCfg `ini:"oauth.gitea"`
|
2020-08-19 19:26:15 +02:00
|
|
|
GenericOauth GenericOauthCfg `ini:"oauth.generic"`
|
2018-10-15 20:44:15 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-02 23:34:44 +01:00
|
|
|
func (ac AppCfg) SignupPath() string {
|
|
|
|
if !ac.OpenRegistration {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if ac.Chorus || ac.Private || (ac.Landing != "" && ac.Landing != "/") {
|
|
|
|
return "/signup"
|
|
|
|
}
|
|
|
|
return "/"
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|