2018-12-24 18:45:15 +01:00
|
|
|
/*
|
2022-11-11 05:49:16 +01:00
|
|
|
* Copyright © 2018-2021 Musing Studio 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 (
|
2021-04-26 17:18:51 +02:00
|
|
|
"net/url"
|
2019-05-13 02:11:53 +02:00
|
|
|
"strings"
|
2019-08-30 00:05:59 +02:00
|
|
|
|
2022-04-29 20:28:55 +02:00
|
|
|
"github.com/go-ini/ini"
|
2021-04-26 17:18:51 +02:00
|
|
|
"github.com/writeas/web-core/log"
|
|
|
|
"golang.org/x/net/idna"
|
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-10-13 05:54:48 +02:00
|
|
|
Scope string `ini:"scope"`
|
2020-06-06 23:52:26 +02:00
|
|
|
AllowDisconnect bool `ini:"allow_disconnect"`
|
2020-10-22 21:15:55 +02:00
|
|
|
MapUserID string `ini:"map_user_id"`
|
|
|
|
MapUsername string `ini:"map_username"`
|
|
|
|
MapDisplayName string `ini:"map_display_name"`
|
|
|
|
MapEmail string `ini:"map_email"`
|
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"`
|
2021-04-22 19:13:47 +02:00
|
|
|
OpenDeletion bool `ini:"open_deletion"`
|
2018-10-25 15:15:10 +02:00
|
|
|
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
|
|
|
|
2020-09-30 21:18:21 +02:00
|
|
|
// Options for public instances
|
2018-10-25 15:15:10 +02:00
|
|
|
// Federation
|
2020-09-30 21:18:21 +02:00
|
|
|
Federation bool `ini:"federation"`
|
|
|
|
PublicStats bool `ini:"public_stats"`
|
|
|
|
Monetization bool `ini:"monetization"`
|
2021-03-04 17:42:49 +01:00
|
|
|
NotesOnly bool `ini:"notes_only"`
|
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
|
|
|
}
|
|
|
|
|
2023-09-25 20:26:41 +02:00
|
|
|
EmailCfg struct {
|
2021-06-22 00:24:40 +02:00
|
|
|
Domain string `ini:"domain"`
|
|
|
|
MailgunPrivate string `ini:"mailgun_private"`
|
|
|
|
}
|
|
|
|
|
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"`
|
2023-09-25 20:26:41 +02:00
|
|
|
Email EmailCfg `ini:"email"`
|
2019-12-28 21:15:47 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-25 20:26:41 +02:00
|
|
|
func (lc EmailCfg) Enabled() bool {
|
2021-06-22 00:24:40 +02:00
|
|
|
return lc.Domain != "" && lc.MailgunPrivate != ""
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2021-04-26 17:18:51 +02:00
|
|
|
|
|
|
|
// Do any transformations
|
|
|
|
u, err := url.Parse(uc.App.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
d, err := idna.ToASCII(u.Hostname())
|
|
|
|
if err != nil {
|
2021-04-26 17:54:42 +02:00
|
|
|
log.Error("idna.ToASCII for %s: %s", u.Hostname(), err)
|
2021-04-26 17:18:51 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
uc.App.Host = u.Scheme + "://" + d
|
|
|
|
if u.Port() != "" {
|
|
|
|
uc.App.Host += ":" + u.Port()
|
|
|
|
}
|
|
|
|
|
2018-10-15 20:44:15 +02:00
|
|
|
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
|
|
|
}
|