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.
 
 
 
 
 

184 lines
4.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. "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. Chorus bool `ini:"chorus"`
  61. // Users
  62. SingleUser bool `ini:"single_user"`
  63. OpenRegistration bool `ini:"open_registration"`
  64. MinUsernameLen int `ini:"min_username_len"`
  65. MaxBlogs int `ini:"max_blogs"`
  66. // Federation
  67. Federation bool `ini:"federation"`
  68. PublicStats bool `ini:"public_stats"`
  69. // Access
  70. Private bool `ini:"private"`
  71. // Additional functions
  72. LocalTimeline bool `ini:"local_timeline"`
  73. UserInvites string `ini:"user_invites"`
  74. }
  75. // Config holds the complete configuration for running a writefreely instance
  76. Config struct {
  77. Server ServerCfg `ini:"server"`
  78. Database DatabaseCfg `ini:"database"`
  79. App AppCfg `ini:"app"`
  80. }
  81. )
  82. // New creates a new Config with sane defaults
  83. func New() *Config {
  84. c := &Config{
  85. Server: ServerCfg{
  86. Port: 8080,
  87. Bind: "localhost", /* IPV6 support when not using localhost? */
  88. },
  89. App: AppCfg{
  90. Host: "http://localhost:8080",
  91. Theme: "write",
  92. WebFonts: true,
  93. SingleUser: true,
  94. MinUsernameLen: 3,
  95. MaxBlogs: 1,
  96. Federation: true,
  97. PublicStats: true,
  98. },
  99. }
  100. c.UseMySQL(true)
  101. return c
  102. }
  103. // UseMySQL resets the Config's Database to use default values for a MySQL setup.
  104. func (cfg *Config) UseMySQL(fresh bool) {
  105. cfg.Database.Type = "mysql"
  106. if fresh {
  107. cfg.Database.Host = "localhost"
  108. cfg.Database.Port = 3306
  109. }
  110. }
  111. // UseSQLite resets the Config's Database to use default values for a SQLite setup.
  112. func (cfg *Config) UseSQLite(fresh bool) {
  113. cfg.Database.Type = "sqlite3"
  114. if fresh {
  115. cfg.Database.FileName = "writefreely.db"
  116. }
  117. }
  118. // IsSecureStandalone returns whether or not the application is running as a
  119. // standalone server with TLS enabled.
  120. func (cfg *Config) IsSecureStandalone() bool {
  121. return cfg.Server.Port == 443 && cfg.Server.TLSCertPath != "" && cfg.Server.TLSKeyPath != ""
  122. }
  123. func (ac *AppCfg) LandingPath() string {
  124. if !strings.HasPrefix(ac.Landing, "/") {
  125. return "/" + ac.Landing
  126. }
  127. return ac.Landing
  128. }
  129. // Load reads the given configuration file, then parses and returns it as a Config.
  130. func Load(fname string) (*Config, error) {
  131. if fname == "" {
  132. fname = FileName
  133. }
  134. cfg, err := ini.Load(fname)
  135. if err != nil {
  136. return nil, err
  137. }
  138. // Parse INI file
  139. uc := &Config{}
  140. err = cfg.MapTo(uc)
  141. if err != nil {
  142. return nil, err
  143. }
  144. return uc, nil
  145. }
  146. // Save writes the given Config to the given file.
  147. func Save(uc *Config, fname string) error {
  148. cfg := ini.Empty()
  149. err := ini.ReflectFrom(cfg, uc)
  150. if err != nil {
  151. return err
  152. }
  153. if fname == "" {
  154. fname = FileName
  155. }
  156. return cfg.SaveTo(fname)
  157. }