config.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package config
  2. import (
  3. "encoding/json"
  4. "os"
  5. )
  6. type Config struct {
  7. Engine struct {
  8. MainModule int
  9. }
  10. Blog struct {
  11. Pagination struct {
  12. Index int
  13. Category int
  14. }
  15. }
  16. Shop struct {
  17. Pagination struct {
  18. Index int
  19. Category int
  20. }
  21. Thumbnails struct {
  22. Thumbnail0 [3]int
  23. Thumbnail1 [3]int
  24. Thumbnail2 [3]int
  25. Thumbnail3 [3]int
  26. ThumbnailFull [3]int
  27. }
  28. Price struct {
  29. Format int
  30. Round int
  31. }
  32. }
  33. API struct {
  34. XML struct {
  35. Enabled int
  36. Name string
  37. Company string
  38. Url string
  39. }
  40. }
  41. SMTP struct {
  42. Host string
  43. Port int
  44. Login string
  45. Password string
  46. }
  47. }
  48. func ConfigNew() *Config {
  49. c := &Config{}
  50. c.configDefault()
  51. return c
  52. }
  53. func (this *Config) configDefault() {
  54. this.Engine.MainModule = 0
  55. this.Blog.Pagination.Index = 5
  56. this.Blog.Pagination.Category = 5
  57. this.Shop.Pagination.Index = 9
  58. this.Shop.Pagination.Category = 9
  59. this.Shop.Thumbnails.Thumbnail0[0] = 100
  60. this.Shop.Thumbnails.Thumbnail0[1] = 100
  61. this.Shop.Thumbnails.Thumbnail0[2] = 0
  62. this.Shop.Thumbnails.Thumbnail1[0] = 200
  63. this.Shop.Thumbnails.Thumbnail1[1] = 200
  64. this.Shop.Thumbnails.Thumbnail1[2] = 2
  65. this.Shop.Thumbnails.Thumbnail2[0] = 250
  66. this.Shop.Thumbnails.Thumbnail2[1] = 250
  67. this.Shop.Thumbnails.Thumbnail2[2] = 2
  68. this.Shop.Thumbnails.Thumbnail3[0] = 450
  69. this.Shop.Thumbnails.Thumbnail3[1] = 450
  70. this.Shop.Thumbnails.Thumbnail3[2] = 2
  71. this.Shop.Thumbnails.ThumbnailFull[0] = 1000
  72. this.Shop.Thumbnails.ThumbnailFull[1] = 800
  73. this.Shop.Thumbnails.ThumbnailFull[2] = 1
  74. this.Shop.Price.Format = 2
  75. this.Shop.Price.Round = 0
  76. this.API.XML.Enabled = 0
  77. this.API.XML.Name = ""
  78. this.API.XML.Company = ""
  79. this.API.XML.Url = ""
  80. this.SMTP.Host = ""
  81. this.SMTP.Port = 587
  82. this.SMTP.Login = ""
  83. this.SMTP.Password = ""
  84. }
  85. func (this *Config) ConfigRead(file string) error {
  86. f, err := os.Open(file)
  87. if err != nil {
  88. return err
  89. }
  90. defer f.Close()
  91. dec := json.NewDecoder(f)
  92. return dec.Decode(this)
  93. }
  94. func (this *Config) ConfigWrite(file string) error {
  95. r, err := json.Marshal(this)
  96. if err != nil {
  97. return err
  98. }
  99. f, err := os.Create(file)
  100. if err != nil {
  101. return err
  102. }
  103. defer f.Close()
  104. _, err = f.WriteString(string(r))
  105. return err
  106. }