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.
 
 
 
 
 

271 lines
7.3 KiB

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