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.
 
 
 
 
 

252 line
6.7 KiB

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