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.
 
 
 
 
 

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