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.
 
 
 
 
 

98 lines
1.7 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. Port int `ini:"port"`
  11. }
  12. DatabaseCfg struct {
  13. Type string `ini:"type"`
  14. User string `ini:"username"`
  15. Password string `ini:"password"`
  16. Database string `ini:"database"`
  17. Host string `ini:"host"`
  18. Port int `ini:"port"`
  19. }
  20. AppCfg struct {
  21. SiteName string `ini:"site_name"`
  22. Host string `ini:"host"`
  23. // Site appearance
  24. Theme string `ini:"theme"`
  25. JSDisabled bool `ini:"disable_js"`
  26. WebFonts bool `ini:"webfonts"`
  27. // Users
  28. SingleUser bool `ini:"single_user"`
  29. OpenRegistration bool `ini:"open_registration"`
  30. MinUsernameLen int `ini:"min_username_len"`
  31. // Federation
  32. Federation bool `ini:"federation"`
  33. PublicStats bool `ini:"public_stats"`
  34. Private bool `ini:"private"`
  35. }
  36. Config struct {
  37. Server ServerCfg `ini:"server"`
  38. Database DatabaseCfg `ini:"database"`
  39. App AppCfg `ini:"app"`
  40. }
  41. )
  42. func New() *Config {
  43. return &Config{
  44. Server: ServerCfg{
  45. Port: 8080,
  46. },
  47. Database: DatabaseCfg{
  48. Type: "mysql",
  49. Host: "localhost",
  50. Port: 3306,
  51. },
  52. App: AppCfg{
  53. Host: "http://localhost:8080",
  54. Theme: "write",
  55. WebFonts: true,
  56. SingleUser: true,
  57. MinUsernameLen: 3,
  58. Federation: true,
  59. PublicStats: true,
  60. },
  61. }
  62. }
  63. func Load() (*Config, error) {
  64. cfg, err := ini.Load(FileName)
  65. if err != nil {
  66. return nil, err
  67. }
  68. // Parse INI file
  69. uc := &Config{}
  70. err = cfg.MapTo(uc)
  71. if err != nil {
  72. return nil, err
  73. }
  74. return uc, nil
  75. }
  76. func Save(uc *Config) error {
  77. cfg := ini.Empty()
  78. err := ini.ReflectFrom(cfg, uc)
  79. if err != nil {
  80. return err
  81. }
  82. return cfg.SaveTo(FileName)
  83. }