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.
 
 
 
 
 

217 lines
5.5 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. 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. // Check for Updates
  97. UpdateChecks bool `ini:"update_checks"`
  98. }
  99. // Config holds the complete configuration for running a writefreely instance
  100. Config struct {
  101. Server ServerCfg `ini:"server"`
  102. Database DatabaseCfg `ini:"database"`
  103. App AppCfg `ini:"app"`
  104. SlackOauth SlackOauthCfg `ini:"oauth.slack"`
  105. WriteAsOauth WriteAsOauthCfg `ini:"oauth.writeas"`
  106. }
  107. )
  108. // New creates a new Config with sane defaults
  109. func New() *Config {
  110. c := &Config{
  111. Server: ServerCfg{
  112. Port: 8080,
  113. Bind: "localhost", /* IPV6 support when not using localhost? */
  114. },
  115. App: AppCfg{
  116. Host: "http://localhost:8080",
  117. Theme: "write",
  118. WebFonts: true,
  119. SingleUser: true,
  120. MinUsernameLen: 3,
  121. MaxBlogs: 1,
  122. Federation: true,
  123. PublicStats: true,
  124. },
  125. }
  126. c.UseMySQL(true)
  127. return c
  128. }
  129. // UseMySQL resets the Config's Database to use default values for a MySQL setup.
  130. func (cfg *Config) UseMySQL(fresh bool) {
  131. cfg.Database.Type = "mysql"
  132. if fresh {
  133. cfg.Database.Host = "localhost"
  134. cfg.Database.Port = 3306
  135. }
  136. }
  137. // UseSQLite resets the Config's Database to use default values for a SQLite setup.
  138. func (cfg *Config) UseSQLite(fresh bool) {
  139. cfg.Database.Type = "sqlite3"
  140. if fresh {
  141. cfg.Database.FileName = "writefreely.db"
  142. }
  143. }
  144. // IsSecureStandalone returns whether or not the application is running as a
  145. // standalone server with TLS enabled.
  146. func (cfg *Config) IsSecureStandalone() bool {
  147. return cfg.Server.Port == 443 && cfg.Server.TLSCertPath != "" && cfg.Server.TLSKeyPath != ""
  148. }
  149. func (ac *AppCfg) LandingPath() string {
  150. if !strings.HasPrefix(ac.Landing, "/") {
  151. return "/" + ac.Landing
  152. }
  153. return ac.Landing
  154. }
  155. // Load reads the given configuration file, then parses and returns it as a Config.
  156. func Load(fname string) (*Config, error) {
  157. if fname == "" {
  158. fname = FileName
  159. }
  160. cfg, err := ini.Load(fname)
  161. if err != nil {
  162. return nil, err
  163. }
  164. // Parse INI file
  165. uc := &Config{}
  166. err = cfg.MapTo(uc)
  167. if err != nil {
  168. return nil, err
  169. }
  170. return uc, nil
  171. }
  172. // Save writes the given Config to the given file.
  173. func Save(uc *Config, fname string) error {
  174. cfg := ini.Empty()
  175. err := ini.ReflectFrom(cfg, uc)
  176. if err != nil {
  177. return err
  178. }
  179. if fname == "" {
  180. fname = FileName
  181. }
  182. return cfg.SaveTo(fname)
  183. }