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.
 
 
 
 
 

147 lines
3.1 KiB

  1. /*
  2. * Copyright © 2018 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
  11. import (
  12. "gopkg.in/ini.v1"
  13. )
  14. const (
  15. FileName = "config.ini"
  16. )
  17. type (
  18. ServerCfg struct {
  19. HiddenHost string `ini:"hidden_host"`
  20. Port int `ini:"port"`
  21. Bind string `ini:"bind"`
  22. TLSCertPath string `ini:"tls_cert_path"`
  23. TLSKeyPath string `ini:"tls_key_path"`
  24. Dev bool `ini:"-"`
  25. }
  26. DatabaseCfg struct {
  27. Type string `ini:"type"`
  28. FileName string `ini:"filename"`
  29. User string `ini:"username"`
  30. Password string `ini:"password"`
  31. Database string `ini:"database"`
  32. Host string `ini:"host"`
  33. Port int `ini:"port"`
  34. }
  35. AppCfg struct {
  36. SiteName string `ini:"site_name"`
  37. SiteDesc string `ini:"site_description"`
  38. Host string `ini:"host"`
  39. // Site appearance
  40. Theme string `ini:"theme"`
  41. JSDisabled bool `ini:"disable_js"`
  42. WebFonts bool `ini:"webfonts"`
  43. // Users
  44. SingleUser bool `ini:"single_user"`
  45. OpenRegistration bool `ini:"open_registration"`
  46. MinUsernameLen int `ini:"min_username_len"`
  47. MaxBlogs int `ini:"max_blogs"`
  48. // Federation
  49. Federation bool `ini:"federation"`
  50. PublicStats bool `ini:"public_stats"`
  51. Private bool `ini:"private"`
  52. // Additional functions
  53. LocalTimeline bool `ini:"local_timeline"`
  54. }
  55. Config struct {
  56. Server ServerCfg `ini:"server"`
  57. Database DatabaseCfg `ini:"database"`
  58. App AppCfg `ini:"app"`
  59. }
  60. )
  61. func New() *Config {
  62. c := &Config{
  63. Server: ServerCfg{
  64. Port: 8080,
  65. Bind: "localhost", /* IPV6 support when not using localhost? */
  66. },
  67. App: AppCfg{
  68. Host: "http://localhost:8080",
  69. Theme: "write",
  70. WebFonts: true,
  71. SingleUser: true,
  72. MinUsernameLen: 3,
  73. MaxBlogs: 1,
  74. Federation: true,
  75. PublicStats: true,
  76. },
  77. }
  78. c.UseMySQL(true)
  79. return c
  80. }
  81. // UseMySQL resets the Config's Database to use default values for a MySQL setup.
  82. func (cfg *Config) UseMySQL(fresh bool) {
  83. cfg.Database.Type = "mysql"
  84. if fresh {
  85. cfg.Database.Host = "localhost"
  86. cfg.Database.Port = 3306
  87. }
  88. }
  89. // UseSQLite resets the Config's Database to use default values for a SQLite setup.
  90. func (cfg *Config) UseSQLite(fresh bool) {
  91. cfg.Database.Type = "sqlite3"
  92. if fresh {
  93. cfg.Database.FileName = "writefreely.db"
  94. }
  95. }
  96. func (cfg *Config) IsSecureStandalone() bool {
  97. return cfg.Server.Port == 443 && cfg.Server.TLSCertPath != "" && cfg.Server.TLSKeyPath != ""
  98. }
  99. func Load(fname string) (*Config, error) {
  100. if fname == "" {
  101. fname = FileName
  102. }
  103. cfg, err := ini.Load(fname)
  104. if err != nil {
  105. return nil, err
  106. }
  107. // Parse INI file
  108. uc := &Config{}
  109. err = cfg.MapTo(uc)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return uc, nil
  114. }
  115. func Save(uc *Config, fname string) error {
  116. cfg := ini.Empty()
  117. err := ini.ReflectFrom(cfg, uc)
  118. if err != nil {
  119. return err
  120. }
  121. if fname == "" {
  122. fname = FileName
  123. }
  124. return cfg.SaveTo(fname)
  125. }