config.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. API struct {
  20. XML struct {
  21. Enabled int
  22. Name string
  23. Company string
  24. Url string
  25. }
  26. }
  27. }
  28. func configNew() *Config {
  29. c := &Config{}
  30. c.configDefault()
  31. return c
  32. }
  33. func (this *Config) configDefault() {
  34. this.Blog.Pagination.Index = 5
  35. this.Blog.Pagination.Category = 5
  36. this.Shop.Pagination.Index = 9
  37. this.Shop.Pagination.Category = 9
  38. this.API.XML.Enabled = 0
  39. this.API.XML.Name = ""
  40. this.API.XML.Company = ""
  41. this.API.XML.Url = ""
  42. }
  43. func (this *Config) configRead(file string) error {
  44. f, err := os.Open(file)
  45. if err != nil {
  46. return err
  47. }
  48. defer f.Close()
  49. dec := json.NewDecoder(f)
  50. return dec.Decode(this)
  51. }
  52. func (this *Config) configWrite(file string) error {
  53. r, err := json.Marshal(this)
  54. if err != nil {
  55. return err
  56. }
  57. f, err := os.Create(file)
  58. if err != nil {
  59. return err
  60. }
  61. defer f.Close()
  62. _, err = f.WriteString(string(r))
  63. return err
  64. }