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.
 
 
 
 
 

255 lines
6.8 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. // Disable password authentication if use only Oauth
  120. DisablePasswordAuth bool `ini:"disable_password_auth"`
  121. }
  122. // Config holds the complete configuration for running a writefreely instance
  123. Config struct {
  124. Server ServerCfg `ini:"server"`
  125. Database DatabaseCfg `ini:"database"`
  126. App AppCfg `ini:"app"`
  127. SlackOauth SlackOauthCfg `ini:"oauth.slack"`
  128. WriteAsOauth WriteAsOauthCfg `ini:"oauth.writeas"`
  129. GitlabOauth GitlabOauthCfg `ini:"oauth.gitlab"`
  130. GenericOauth GenericOauthCfg `ini:"oauth.generic"`
  131. }
  132. )
  133. // New creates a new Config with sane defaults
  134. func New() *Config {
  135. c := &Config{
  136. Server: ServerCfg{
  137. Port: 8080,
  138. Bind: "localhost", /* IPV6 support when not using localhost? */
  139. },
  140. App: AppCfg{
  141. Host: "http://localhost:8080",
  142. Theme: "write",
  143. WebFonts: true,
  144. SingleUser: true,
  145. MinUsernameLen: 3,
  146. MaxBlogs: 1,
  147. Federation: true,
  148. PublicStats: true,
  149. },
  150. }
  151. c.UseMySQL(true)
  152. return c
  153. }
  154. // UseMySQL resets the Config's Database to use default values for a MySQL setup.
  155. func (cfg *Config) UseMySQL(fresh bool) {
  156. cfg.Database.Type = "mysql"
  157. if fresh {
  158. cfg.Database.Host = "localhost"
  159. cfg.Database.Port = 3306
  160. }
  161. }
  162. // UseSQLite resets the Config's Database to use default values for a SQLite setup.
  163. func (cfg *Config) UseSQLite(fresh bool) {
  164. cfg.Database.Type = "sqlite3"
  165. if fresh {
  166. cfg.Database.FileName = "writefreely.db"
  167. }
  168. }
  169. // IsSecureStandalone returns whether or not the application is running as a
  170. // standalone server with TLS enabled.
  171. func (cfg *Config) IsSecureStandalone() bool {
  172. return cfg.Server.Port == 443 && cfg.Server.TLSCertPath != "" && cfg.Server.TLSKeyPath != ""
  173. }
  174. func (ac *AppCfg) LandingPath() string {
  175. if !strings.HasPrefix(ac.Landing, "/") {
  176. return "/" + ac.Landing
  177. }
  178. return ac.Landing
  179. }
  180. func (ac AppCfg) SignupPath() string {
  181. if !ac.OpenRegistration {
  182. return ""
  183. }
  184. if ac.Chorus || ac.Private || (ac.Landing != "" && ac.Landing != "/") {
  185. return "/signup"
  186. }
  187. return "/"
  188. }
  189. // Load reads the given configuration file, then parses and returns it as a Config.
  190. func Load(fname string) (*Config, error) {
  191. if fname == "" {
  192. fname = FileName
  193. }
  194. cfg, err := ini.Load(fname)
  195. if err != nil {
  196. return nil, err
  197. }
  198. // Parse INI file
  199. uc := &Config{}
  200. err = cfg.MapTo(uc)
  201. if err != nil {
  202. return nil, err
  203. }
  204. return uc, nil
  205. }
  206. // Save writes the given Config to the given file.
  207. func Save(uc *Config, fname string) error {
  208. cfg := ini.Empty()
  209. err := ini.ReflectFrom(cfg, uc)
  210. if err != nil {
  211. return err
  212. }
  213. if fname == "" {
  214. fname = FileName
  215. }
  216. return cfg.SaveTo(fname)
  217. }