config.go 1.5 KB

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