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.
 
 
 
 
 

113 lines
2.2 KiB

  1. package config
  2. import (
  3. "gopkg.in/ini.v1"
  4. )
  5. const (
  6. FileName = "config.ini"
  7. )
  8. type (
  9. ServerCfg struct {
  10. HiddenHost string `ini:"hidden_host"`
  11. Port int `ini:"port"`
  12. Bind string `ini:"bind"`
  13. TLSCertPath string `ini:"tls_cert_path"`
  14. TLSKeyPath string `ini:"tls_key_path"`
  15. Dev bool `ini:"-"`
  16. }
  17. DatabaseCfg struct {
  18. Type string `ini:"type"`
  19. User string `ini:"username"`
  20. Password string `ini:"password"`
  21. Database string `ini:"database"`
  22. Host string `ini:"host"`
  23. Port int `ini:"port"`
  24. }
  25. AppCfg struct {
  26. SiteName string `ini:"site_name"`
  27. SiteDesc string `ini:"site_description"`
  28. Host string `ini:"host"`
  29. // Site appearance
  30. Theme string `ini:"theme"`
  31. JSDisabled bool `ini:"disable_js"`
  32. WebFonts bool `ini:"webfonts"`
  33. // Users
  34. SingleUser bool `ini:"single_user"`
  35. OpenRegistration bool `ini:"open_registration"`
  36. MinUsernameLen int `ini:"min_username_len"`
  37. MaxBlogs int `ini:"max_blogs"`
  38. // Federation
  39. Federation bool `ini:"federation"`
  40. PublicStats bool `ini:"public_stats"`
  41. Private bool `ini:"private"`
  42. }
  43. Config struct {
  44. Server ServerCfg `ini:"server"`
  45. Database DatabaseCfg `ini:"database"`
  46. App AppCfg `ini:"app"`
  47. }
  48. )
  49. func New() *Config {
  50. return &Config{
  51. Server: ServerCfg{
  52. Port: 8080,
  53. Bind: "localhost", /* IPV6 support when not using localhost? */
  54. },
  55. Database: DatabaseCfg{
  56. Type: "mysql",
  57. Host: "localhost",
  58. Port: 3306,
  59. },
  60. App: AppCfg{
  61. Host: "http://localhost:8080",
  62. Theme: "write",
  63. WebFonts: true,
  64. SingleUser: true,
  65. MinUsernameLen: 3,
  66. MaxBlogs: 1,
  67. Federation: true,
  68. PublicStats: true,
  69. },
  70. }
  71. }
  72. func (cfg *Config) IsSecureStandalone() bool {
  73. return cfg.Server.Port == 443 && cfg.Server.TLSCertPath != "" && cfg.Server.TLSKeyPath != ""
  74. }
  75. func Load() (*Config, error) {
  76. cfg, err := ini.Load(FileName)
  77. if err != nil {
  78. return nil, err
  79. }
  80. // Parse INI file
  81. uc := &Config{}
  82. err = cfg.MapTo(uc)
  83. if err != nil {
  84. return nil, err
  85. }
  86. return uc, nil
  87. }
  88. func Save(uc *Config) error {
  89. cfg := ini.Empty()
  90. err := ini.ReflectFrom(cfg, uc)
  91. if err != nil {
  92. return err
  93. }
  94. return cfg.SaveTo(FileName)
  95. }