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.
 
 
 
 
 

191 lines
4.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. "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. Dev bool `ini:"-"`
  37. }
  38. // DatabaseCfg holds values that determine how the application connects to a datastore
  39. DatabaseCfg struct {
  40. Type string `ini:"type"`
  41. FileName string `ini:"filename"`
  42. User string `ini:"username"`
  43. Password string `ini:"password"`
  44. Database string `ini:"database"`
  45. Host string `ini:"host"`
  46. Port int `ini:"port"`
  47. }
  48. // AppCfg holds values that affect how the application functions
  49. AppCfg struct {
  50. SiteName string `ini:"site_name"`
  51. SiteDesc string `ini:"site_description"`
  52. Host string `ini:"host"`
  53. // Site appearance
  54. Theme string `ini:"theme"`
  55. Editor string `ini:"editor"`
  56. JSDisabled bool `ini:"disable_js"`
  57. WebFonts bool `ini:"webfonts"`
  58. Landing string `ini:"landing"`
  59. SimpleNav bool `ini:"simple_nav"`
  60. WFModesty bool `ini:"wf_modesty"`
  61. // Site functionality
  62. Chorus bool `ini:"chorus"`
  63. DisableDrafts bool `ini:"disable_drafts"`
  64. // Users
  65. SingleUser bool `ini:"single_user"`
  66. OpenRegistration bool `ini:"open_registration"`
  67. MinUsernameLen int `ini:"min_username_len"`
  68. MaxBlogs int `ini:"max_blogs"`
  69. // Federation
  70. Federation bool `ini:"federation"`
  71. PublicStats bool `ini:"public_stats"`
  72. // Access
  73. Private bool `ini:"private"`
  74. // Additional functions
  75. LocalTimeline bool `ini:"local_timeline"`
  76. UserInvites string `ini:"user_invites"`
  77. // Defaults
  78. DefaultVisibility string `ini:"default_visibility"`
  79. }
  80. // Config holds the complete configuration for running a writefreely instance
  81. Config struct {
  82. Server ServerCfg `ini:"server"`
  83. Database DatabaseCfg `ini:"database"`
  84. App AppCfg `ini:"app"`
  85. }
  86. )
  87. // New creates a new Config with sane defaults
  88. func New() *Config {
  89. c := &Config{
  90. Server: ServerCfg{
  91. Port: 8080,
  92. Bind: "localhost", /* IPV6 support when not using localhost? */
  93. },
  94. App: AppCfg{
  95. Host: "http://localhost:8080",
  96. Theme: "write",
  97. WebFonts: true,
  98. SingleUser: true,
  99. MinUsernameLen: 3,
  100. MaxBlogs: 1,
  101. Federation: true,
  102. PublicStats: true,
  103. },
  104. }
  105. c.UseMySQL(true)
  106. return c
  107. }
  108. // UseMySQL resets the Config's Database to use default values for a MySQL setup.
  109. func (cfg *Config) UseMySQL(fresh bool) {
  110. cfg.Database.Type = "mysql"
  111. if fresh {
  112. cfg.Database.Host = "localhost"
  113. cfg.Database.Port = 3306
  114. }
  115. }
  116. // UseSQLite resets the Config's Database to use default values for a SQLite setup.
  117. func (cfg *Config) UseSQLite(fresh bool) {
  118. cfg.Database.Type = "sqlite3"
  119. if fresh {
  120. cfg.Database.FileName = "writefreely.db"
  121. }
  122. }
  123. // IsSecureStandalone returns whether or not the application is running as a
  124. // standalone server with TLS enabled.
  125. func (cfg *Config) IsSecureStandalone() bool {
  126. return cfg.Server.Port == 443 && cfg.Server.TLSCertPath != "" && cfg.Server.TLSKeyPath != ""
  127. }
  128. func (ac *AppCfg) LandingPath() string {
  129. if !strings.HasPrefix(ac.Landing, "/") {
  130. return "/" + ac.Landing
  131. }
  132. return ac.Landing
  133. }
  134. // Load reads the given configuration file, then parses and returns it as a Config.
  135. func Load(fname string) (*Config, error) {
  136. if fname == "" {
  137. fname = FileName
  138. }
  139. cfg, err := ini.Load(fname)
  140. if err != nil {
  141. return nil, err
  142. }
  143. // Parse INI file
  144. uc := &Config{}
  145. err = cfg.MapTo(uc)
  146. if err != nil {
  147. return nil, err
  148. }
  149. return uc, nil
  150. }
  151. // Save writes the given Config to the given file.
  152. func Save(uc *Config, fname string) error {
  153. cfg := ini.Empty()
  154. err := ini.ReflectFrom(cfg, uc)
  155. if err != nil {
  156. return err
  157. }
  158. if fname == "" {
  159. fname = FileName
  160. }
  161. return cfg.SaveTo(fname)
  162. }