config.go 905 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package wrapper
  2. import (
  3. "encoding/json"
  4. "os"
  5. )
  6. type Config struct {
  7. Blog struct {
  8. Pagination struct {
  9. Index int
  10. Category int
  11. }
  12. }
  13. Shop struct {
  14. Pagination struct {
  15. Index int
  16. Category int
  17. }
  18. }
  19. }
  20. func configNew() *Config {
  21. c := &Config{}
  22. c.configDefault()
  23. return c
  24. }
  25. func (this *Config) configDefault() {
  26. this.Blog.Pagination.Index = 5
  27. this.Blog.Pagination.Category = 5
  28. this.Shop.Pagination.Index = 5
  29. this.Shop.Pagination.Category = 5
  30. }
  31. func (this *Config) configRead(file string) error {
  32. f, err := os.Open(file)
  33. if err != nil {
  34. return err
  35. }
  36. defer f.Close()
  37. dec := json.NewDecoder(f)
  38. return dec.Decode(this)
  39. }
  40. func (this *Config) configWrite(file string) error {
  41. r, err := json.Marshal(this)
  42. if err != nil {
  43. return err
  44. }
  45. f, err := os.Create(file)
  46. if err != nil {
  47. return err
  48. }
  49. defer f.Close()
  50. _, err = f.WriteString(string(r))
  51. return err
  52. }