A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

296 lines
8.0 KiB

  1. /*
  2. * Copyright © 2018-2021 A Bunch Tell LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. // Package config holds and assists in the configuration of a writefreely instance.
  11. package config
  12. import (
  13. "net/url"
  14. "strings"
  15. "github.com/writeas/web-core/log"
  16. "golang.org/x/net/idna"
  17. "gopkg.in/ini.v1"
  18. )
  19. const (
  20. // FileName is the default configuration file name
  21. FileName = "config.ini"
  22. UserNormal UserType = "user"
  23. UserAdmin = "admin"
  24. )
  25. type (
  26. UserType string
  27. // ServerCfg holds values that affect how the HTTP server runs
  28. ServerCfg struct {
  29. HiddenHost string `ini:"hidden_host"`
  30. Port int `ini:"port"`
  31. Bind string `ini:"bind"`
  32. TLSCertPath string `ini:"tls_cert_path"`
  33. TLSKeyPath string `ini:"tls_key_path"`
  34. Autocert bool `ini:"autocert"`
  35. TemplatesParentDir string `ini:"templates_parent_dir"`
  36. StaticParentDir string `ini:"static_parent_dir"`
  37. PagesParentDir string `ini:"pages_parent_dir"`
  38. KeysParentDir string `ini:"keys_parent_dir"`
  39. HashSeed string `ini:"hash_seed"`
  40. GopherPort int `ini:"gopher_port"`
  41. Dev bool `ini:"-"`
  42. }
  43. // DatabaseCfg holds values that determine how the application connects to a datastore
  44. DatabaseCfg struct {
  45. Type string `ini:"type"`
  46. FileName string `ini:"filename"`
  47. User string `ini:"username"`
  48. Password string `ini:"password"`
  49. Database string `ini:"database"`
  50. Host string `ini:"host"`
  51. Port int `ini:"port"`
  52. TLS bool `ini:"tls"`
  53. }
  54. WriteAsOauthCfg struct {
  55. ClientID string `ini:"client_id"`
  56. ClientSecret string `ini:"client_secret"`
  57. AuthLocation string `ini:"auth_location"`
  58. TokenLocation string `ini:"token_location"`
  59. InspectLocation string `ini:"inspect_location"`
  60. CallbackProxy string `ini:"callback_proxy"`
  61. CallbackProxyAPI string `ini:"callback_proxy_api"`
  62. }
  63. GitlabOauthCfg struct {
  64. ClientID string `ini:"client_id"`
  65. ClientSecret string `ini:"client_secret"`
  66. Host string `ini:"host"`
  67. DisplayName string `ini:"display_name"`
  68. CallbackProxy string `ini:"callback_proxy"`
  69. CallbackProxyAPI string `ini:"callback_proxy_api"`
  70. }
  71. GiteaOauthCfg struct {
  72. ClientID string `ini:"client_id"`
  73. ClientSecret string `ini:"client_secret"`
  74. Host string `ini:"host"`
  75. DisplayName string `ini:"display_name"`
  76. CallbackProxy string `ini:"callback_proxy"`
  77. CallbackProxyAPI string `ini:"callback_proxy_api"`
  78. }
  79. SlackOauthCfg struct {
  80. ClientID string `ini:"client_id"`
  81. ClientSecret string `ini:"client_secret"`
  82. TeamID string `ini:"team_id"`
  83. CallbackProxy string `ini:"callback_proxy"`
  84. CallbackProxyAPI string `ini:"callback_proxy_api"`
  85. }
  86. GenericOauthCfg struct {
  87. ClientID string `ini:"client_id"`
  88. ClientSecret string `ini:"client_secret"`
  89. Host string `ini:"host"`
  90. DisplayName string `ini:"display_name"`
  91. CallbackProxy string `ini:"callback_proxy"`
  92. CallbackProxyAPI string `ini:"callback_proxy_api"`
  93. TokenEndpoint string `ini:"token_endpoint"`
  94. InspectEndpoint string `ini:"inspect_endpoint"`
  95. AuthEndpoint string `ini:"auth_endpoint"`
  96. Scope string `ini:"scope"`
  97. AllowDisconnect bool `ini:"allow_disconnect"`
  98. MapUserID string `ini:"map_user_id"`
  99. MapUsername string `ini:"map_username"`
  100. MapDisplayName string `ini:"map_display_name"`
  101. MapEmail string `ini:"map_email"`
  102. }
  103. // AppCfg holds values that affect how the application functions
  104. AppCfg struct {
  105. SiteName string `ini:"site_name"`
  106. SiteDesc string `ini:"site_description"`
  107. Host string `ini:"host"`
  108. // Site appearance
  109. Theme string `ini:"theme"`
  110. Editor string `ini:"editor"`
  111. JSDisabled bool `ini:"disable_js"`
  112. WebFonts bool `ini:"webfonts"`
  113. Landing string `ini:"landing"`
  114. SimpleNav bool `ini:"simple_nav"`
  115. WFModesty bool `ini:"wf_modesty"`
  116. // Site functionality
  117. Chorus bool `ini:"chorus"`
  118. Forest bool `ini:"forest"` // The admin cares about the forest, not the trees. Hide unnecessary technical info.
  119. DisableDrafts bool `ini:"disable_drafts"`
  120. // Users
  121. SingleUser bool `ini:"single_user"`
  122. OpenRegistration bool `ini:"open_registration"`
  123. OpenDeletion bool `ini:"open_deletion"`
  124. MinUsernameLen int `ini:"min_username_len"`
  125. MaxBlogs int `ini:"max_blogs"`
  126. // Options for public instances
  127. // Federation
  128. Federation bool `ini:"federation"`
  129. PublicStats bool `ini:"public_stats"`
  130. Monetization bool `ini:"monetization"`
  131. NotesOnly bool `ini:"notes_only"`
  132. // Access
  133. Private bool `ini:"private"`
  134. // Additional functions
  135. LocalTimeline bool `ini:"local_timeline"`
  136. UserInvites string `ini:"user_invites"`
  137. // Defaults
  138. DefaultVisibility string `ini:"default_visibility"`
  139. // Check for Updates
  140. UpdateChecks bool `ini:"update_checks"`
  141. // Disable password authentication if use only Oauth
  142. DisablePasswordAuth bool `ini:"disable_password_auth"`
  143. }
  144. // Config holds the complete configuration for running a writefreely instance
  145. Config struct {
  146. Server ServerCfg `ini:"server"`
  147. Database DatabaseCfg `ini:"database"`
  148. App AppCfg `ini:"app"`
  149. SlackOauth SlackOauthCfg `ini:"oauth.slack"`
  150. WriteAsOauth WriteAsOauthCfg `ini:"oauth.writeas"`
  151. GitlabOauth GitlabOauthCfg `ini:"oauth.gitlab"`
  152. GiteaOauth GiteaOauthCfg `ini:"oauth.gitea"`
  153. GenericOauth GenericOauthCfg `ini:"oauth.generic"`
  154. }
  155. )
  156. // New creates a new Config with sane defaults
  157. func New() *Config {
  158. c := &Config{
  159. Server: ServerCfg{
  160. Port: 8080,
  161. Bind: "localhost", /* IPV6 support when not using localhost? */
  162. },
  163. App: AppCfg{
  164. Host: "http://localhost:8080",
  165. Theme: "write",
  166. WebFonts: true,
  167. SingleUser: true,
  168. MinUsernameLen: 3,
  169. MaxBlogs: 1,
  170. Federation: true,
  171. PublicStats: true,
  172. },
  173. }
  174. c.UseMySQL(true)
  175. return c
  176. }
  177. // UseMySQL resets the Config's Database to use default values for a MySQL setup.
  178. func (cfg *Config) UseMySQL(fresh bool) {
  179. cfg.Database.Type = "mysql"
  180. if fresh {
  181. cfg.Database.Host = "localhost"
  182. cfg.Database.Port = 3306
  183. }
  184. }
  185. // UseSQLite resets the Config's Database to use default values for a SQLite setup.
  186. func (cfg *Config) UseSQLite(fresh bool) {
  187. cfg.Database.Type = "sqlite3"
  188. if fresh {
  189. cfg.Database.FileName = "writefreely.db"
  190. }
  191. }
  192. // IsSecureStandalone returns whether or not the application is running as a
  193. // standalone server with TLS enabled.
  194. func (cfg *Config) IsSecureStandalone() bool {
  195. return cfg.Server.Port == 443 && cfg.Server.TLSCertPath != "" && cfg.Server.TLSKeyPath != ""
  196. }
  197. func (ac *AppCfg) LandingPath() string {
  198. if !strings.HasPrefix(ac.Landing, "/") {
  199. return "/" + ac.Landing
  200. }
  201. return ac.Landing
  202. }
  203. func (ac AppCfg) SignupPath() string {
  204. if !ac.OpenRegistration {
  205. return ""
  206. }
  207. if ac.Chorus || ac.Private || (ac.Landing != "" && ac.Landing != "/") {
  208. return "/signup"
  209. }
  210. return "/"
  211. }
  212. // Load reads the given configuration file, then parses and returns it as a Config.
  213. func Load(fname string) (*Config, error) {
  214. if fname == "" {
  215. fname = FileName
  216. }
  217. cfg, err := ini.Load(fname)
  218. if err != nil {
  219. return nil, err
  220. }
  221. // Parse INI file
  222. uc := &Config{}
  223. err = cfg.MapTo(uc)
  224. if err != nil {
  225. return nil, err
  226. }
  227. // Do any transformations
  228. u, err := url.Parse(uc.App.Host)
  229. if err != nil {
  230. return nil, err
  231. }
  232. d, err := idna.ToASCII(u.Hostname())
  233. if err != nil {
  234. log.Error("idna.ToASCII for %s: %s", u.Hostname(), err)
  235. return nil, err
  236. }
  237. uc.App.Host = u.Scheme + "://" + d
  238. if u.Port() != "" {
  239. uc.App.Host += ":" + u.Port()
  240. }
  241. return uc, nil
  242. }
  243. // Save writes the given Config to the given file.
  244. func Save(uc *Config, fname string) error {
  245. cfg := ini.Empty()
  246. err := ini.ReflectFrom(cfg, uc)
  247. if err != nil {
  248. return err
  249. }
  250. if fname == "" {
  251. fname = FileName
  252. }
  253. return cfg.SaveTo(fname)
  254. }