config.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. Thumbnails struct {
  19. Thumbnail0 [3]int
  20. Thumbnail1 [3]int
  21. Thumbnail2 [3]int
  22. Thumbnail3 [3]int
  23. ThumbnailFull [3]int
  24. }
  25. }
  26. API struct {
  27. XML struct {
  28. Enabled int
  29. Name string
  30. Company string
  31. Url string
  32. }
  33. }
  34. }
  35. func configNew() *Config {
  36. c := &Config{}
  37. c.configDefault()
  38. return c
  39. }
  40. func (this *Config) configDefault() {
  41. this.Blog.Pagination.Index = 5
  42. this.Blog.Pagination.Category = 5
  43. this.Shop.Pagination.Index = 9
  44. this.Shop.Pagination.Category = 9
  45. this.Shop.Thumbnails.Thumbnail0[0] = 100
  46. this.Shop.Thumbnails.Thumbnail0[1] = 100
  47. this.Shop.Thumbnails.Thumbnail0[2] = 0
  48. this.Shop.Thumbnails.Thumbnail1[0] = 200
  49. this.Shop.Thumbnails.Thumbnail1[1] = 200
  50. this.Shop.Thumbnails.Thumbnail1[2] = 0
  51. this.Shop.Thumbnails.Thumbnail2[0] = 250
  52. this.Shop.Thumbnails.Thumbnail2[1] = 250
  53. this.Shop.Thumbnails.Thumbnail2[2] = 0
  54. this.Shop.Thumbnails.Thumbnail3[0] = 450
  55. this.Shop.Thumbnails.Thumbnail3[1] = 450
  56. this.Shop.Thumbnails.Thumbnail3[2] = 0
  57. this.Shop.Thumbnails.ThumbnailFull[0] = 1000
  58. this.Shop.Thumbnails.ThumbnailFull[1] = 800
  59. this.Shop.Thumbnails.ThumbnailFull[2] = 1
  60. this.API.XML.Enabled = 0
  61. this.API.XML.Name = ""
  62. this.API.XML.Company = ""
  63. this.API.XML.Url = ""
  64. }
  65. func (this *Config) configRead(file string) error {
  66. f, err := os.Open(file)
  67. if err != nil {
  68. return err
  69. }
  70. defer f.Close()
  71. dec := json.NewDecoder(f)
  72. return dec.Decode(this)
  73. }
  74. func (this *Config) configWrite(file string) error {
  75. r, err := json.Marshal(this)
  76. if err != nil {
  77. return err
  78. }
  79. f, err := os.Create(file)
  80. if err != nil {
  81. return err
  82. }
  83. defer f.Close()
  84. _, err = f.WriteString(string(r))
  85. return err
  86. }