config.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package config
  2. import (
  3. "bufio"
  4. "errors"
  5. "io"
  6. "os"
  7. "strings"
  8. "bloat/model"
  9. )
  10. type config struct {
  11. ListenAddress string
  12. ClientName string
  13. ClientScope string
  14. ClientWebsite string
  15. SingleInstance string
  16. StaticDirectory string
  17. TemplatesPath string
  18. DatabasePath string
  19. CustomCSS string
  20. PostFormats []model.PostFormat
  21. LogFile string
  22. }
  23. func (c *config) IsValid() bool {
  24. if len(c.ListenAddress) < 1 ||
  25. len(c.ClientName) < 1 ||
  26. len(c.ClientScope) < 1 ||
  27. len(c.ClientWebsite) < 1 ||
  28. len(c.StaticDirectory) < 1 ||
  29. len(c.TemplatesPath) < 1 ||
  30. len(c.DatabasePath) < 1 {
  31. return false
  32. }
  33. return true
  34. }
  35. func Parse(r io.Reader) (c *config, err error) {
  36. c = new(config)
  37. scanner := bufio.NewScanner(r)
  38. for scanner.Scan() {
  39. line := strings.TrimSpace(scanner.Text())
  40. if len(line) < 1 {
  41. continue
  42. }
  43. index := strings.IndexRune(line, '#')
  44. if index == 0 {
  45. continue
  46. }
  47. index = strings.IndexRune(line, '=')
  48. if index < 1 {
  49. return nil, errors.New("invalid config key")
  50. }
  51. key := strings.TrimSpace(line[:index])
  52. val := strings.TrimSpace(line[index+1 : len(line)])
  53. switch key {
  54. case "listen_address":
  55. c.ListenAddress = val
  56. case "client_name":
  57. c.ClientName = val
  58. case "client_scope":
  59. c.ClientScope = val
  60. case "client_website":
  61. c.ClientWebsite = val
  62. case "single_instance":
  63. c.SingleInstance = val
  64. case "static_directory":
  65. c.StaticDirectory = val
  66. case "templates_path":
  67. c.TemplatesPath = val
  68. case "database_path":
  69. c.DatabasePath = val
  70. case "custom_css":
  71. c.CustomCSS = val
  72. case "post_formats":
  73. vals := strings.Split(val, ",")
  74. var formats []model.PostFormat
  75. for _, v := range vals {
  76. pair := strings.Split(v, ":")
  77. if len(pair) != 2 {
  78. return nil, errors.New("invalid config key " + key)
  79. }
  80. n := strings.TrimSpace(pair[0])
  81. t := strings.TrimSpace(pair[1])
  82. if len(n) < 1 || len(t) < 1 {
  83. return nil, errors.New("invalid config key " + key)
  84. }
  85. formats = append(formats, model.PostFormat{
  86. Name: n,
  87. Type: t,
  88. })
  89. }
  90. c.PostFormats = formats
  91. case "log_file":
  92. c.LogFile = val
  93. default:
  94. return nil, errors.New("invalid config key " + key)
  95. }
  96. }
  97. return
  98. }
  99. func ParseFile(file string) (c *config, err error) {
  100. f, err := os.Open(file)
  101. if err != nil {
  102. return
  103. }
  104. defer f.Close()
  105. info, err := f.Stat()
  106. if err != nil {
  107. return
  108. }
  109. if info.IsDir() {
  110. return nil, errors.New("invalid config file")
  111. }
  112. return Parse(f)
  113. }