A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

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