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.
 
 
 
 
 

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