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.
 
 
 
 
 

270 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. AllowDisconnect bool `ini:"allow_disconnect"`
  94. }
  95. // AppCfg holds values that affect how the application functions
  96. AppCfg struct {
  97. SiteName string `ini:"site_name"`
  98. SiteDesc string `ini:"site_description"`
  99. Host string `ini:"host"`
  100. // Site appearance
  101. Theme string `ini:"theme"`
  102. Editor string `ini:"editor"`
  103. JSDisabled bool `ini:"disable_js"`
  104. WebFonts bool `ini:"webfonts"`
  105. Landing string `ini:"landing"`
  106. SimpleNav bool `ini:"simple_nav"`
  107. WFModesty bool `ini:"wf_modesty"`
  108. // Site functionality
  109. Chorus bool `ini:"chorus"`
  110. Forest bool `ini:"forest"` // The admin cares about the forest, not the trees. Hide unnecessary technical info.
  111. DisableDrafts bool `ini:"disable_drafts"`
  112. // Users
  113. SingleUser bool `ini:"single_user"`
  114. OpenRegistration bool `ini:"open_registration"`
  115. MinUsernameLen int `ini:"min_username_len"`
  116. MaxBlogs int `ini:"max_blogs"`
  117. // Options for public instances
  118. // Federation
  119. Federation bool `ini:"federation"`
  120. PublicStats bool `ini:"public_stats"`
  121. Monetization bool `ini:"monetization"`
  122. // Access
  123. Private bool `ini:"private"`
  124. // Additional functions
  125. LocalTimeline bool `ini:"local_timeline"`
  126. UserInvites string `ini:"user_invites"`
  127. // Defaults
  128. DefaultVisibility string `ini:"default_visibility"`
  129. // Check for Updates
  130. UpdateChecks bool `ini:"update_checks"`
  131. // Disable password authentication if use only Oauth
  132. DisablePasswordAuth bool `ini:"disable_password_auth"`
  133. }
  134. // Config holds the complete configuration for running a writefreely instance
  135. Config struct {
  136. Server ServerCfg `ini:"server"`
  137. Database DatabaseCfg `ini:"database"`
  138. App AppCfg `ini:"app"`
  139. SlackOauth SlackOauthCfg `ini:"oauth.slack"`
  140. WriteAsOauth WriteAsOauthCfg `ini:"oauth.writeas"`
  141. GitlabOauth GitlabOauthCfg `ini:"oauth.gitlab"`
  142. GiteaOauth GiteaOauthCfg `ini:"oauth.gitea"`
  143. GenericOauth GenericOauthCfg `ini:"oauth.generic"`
  144. }
  145. )
  146. // New creates a new Config with sane defaults
  147. func New() *Config {
  148. c := &Config{
  149. Server: ServerCfg{
  150. Port: 8080,
  151. Bind: "localhost", /* IPV6 support when not using localhost? */
  152. },
  153. App: AppCfg{
  154. Host: "http://localhost:8080",
  155. Theme: "write",
  156. WebFonts: true,
  157. SingleUser: true,
  158. MinUsernameLen: 3,
  159. MaxBlogs: 1,
  160. Federation: true,
  161. PublicStats: true,
  162. },
  163. }
  164. c.UseMySQL(true)
  165. return c
  166. }
  167. // UseMySQL resets the Config's Database to use default values for a MySQL setup.
  168. func (cfg *Config) UseMySQL(fresh bool) {
  169. cfg.Database.Type = "mysql"
  170. if fresh {
  171. cfg.Database.Host = "localhost"
  172. cfg.Database.Port = 3306
  173. }
  174. }
  175. // UseSQLite resets the Config's Database to use default values for a SQLite setup.
  176. func (cfg *Config) UseSQLite(fresh bool) {
  177. cfg.Database.Type = "sqlite3"
  178. if fresh {
  179. cfg.Database.FileName = "writefreely.db"
  180. }
  181. }
  182. // IsSecureStandalone returns whether or not the application is running as a
  183. // standalone server with TLS enabled.
  184. func (cfg *Config) IsSecureStandalone() bool {
  185. return cfg.Server.Port == 443 && cfg.Server.TLSCertPath != "" && cfg.Server.TLSKeyPath != ""
  186. }
  187. func (ac *AppCfg) LandingPath() string {
  188. if !strings.HasPrefix(ac.Landing, "/") {
  189. return "/" + ac.Landing
  190. }
  191. return ac.Landing
  192. }
  193. func (ac AppCfg) SignupPath() string {
  194. if !ac.OpenRegistration {
  195. return ""
  196. }
  197. if ac.Chorus || ac.Private || (ac.Landing != "" && ac.Landing != "/") {
  198. return "/signup"
  199. }
  200. return "/"
  201. }
  202. // Load reads the given configuration file, then parses and returns it as a Config.
  203. func Load(fname string) (*Config, error) {
  204. if fname == "" {
  205. fname = FileName
  206. }
  207. cfg, err := ini.Load(fname)
  208. if err != nil {
  209. return nil, err
  210. }
  211. // Parse INI file
  212. uc := &Config{}
  213. err = cfg.MapTo(uc)
  214. if err != nil {
  215. return nil, err
  216. }
  217. return uc, nil
  218. }
  219. // Save writes the given Config to the given file.
  220. func Save(uc *Config, fname string) error {
  221. cfg := ini.Empty()
  222. err := ini.ReflectFrom(cfg, uc)
  223. if err != nil {
  224. return err
  225. }
  226. if fname == "" {
  227. fname = FileName
  228. }
  229. return cfg.SaveTo(fname)
  230. }