config.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. Orders struct {
  33. RequiredFields struct {
  34. LastName int
  35. FirstName int
  36. MiddleName int
  37. MobilePhone int
  38. EmailAddress int
  39. Delivery int
  40. Comment int
  41. }
  42. NotifyEmail string
  43. Enabled int
  44. }
  45. }
  46. API struct {
  47. XML struct {
  48. Enabled int
  49. Name string
  50. Company string
  51. Url string
  52. }
  53. }
  54. SMTP struct {
  55. Host string
  56. Port int
  57. Login string
  58. Password string
  59. }
  60. }
  61. func ConfigNew() *Config {
  62. c := &Config{}
  63. c.configDefault()
  64. return c
  65. }
  66. func (this *Config) configDefault() {
  67. this.Engine.MainModule = 0
  68. this.Blog.Pagination.Index = 5
  69. this.Blog.Pagination.Category = 5
  70. this.Shop.Pagination.Index = 9
  71. this.Shop.Pagination.Category = 9
  72. this.Shop.Thumbnails.Thumbnail0[0] = 100
  73. this.Shop.Thumbnails.Thumbnail0[1] = 100
  74. this.Shop.Thumbnails.Thumbnail0[2] = 0
  75. this.Shop.Thumbnails.Thumbnail1[0] = 200
  76. this.Shop.Thumbnails.Thumbnail1[1] = 200
  77. this.Shop.Thumbnails.Thumbnail1[2] = 2
  78. this.Shop.Thumbnails.Thumbnail2[0] = 250
  79. this.Shop.Thumbnails.Thumbnail2[1] = 250
  80. this.Shop.Thumbnails.Thumbnail2[2] = 2
  81. this.Shop.Thumbnails.Thumbnail3[0] = 450
  82. this.Shop.Thumbnails.Thumbnail3[1] = 450
  83. this.Shop.Thumbnails.Thumbnail3[2] = 2
  84. this.Shop.Thumbnails.ThumbnailFull[0] = 1000
  85. this.Shop.Thumbnails.ThumbnailFull[1] = 800
  86. this.Shop.Thumbnails.ThumbnailFull[2] = 1
  87. this.Shop.Price.Format = 2
  88. this.Shop.Price.Round = 0
  89. this.Shop.Orders.RequiredFields.LastName = 1
  90. this.Shop.Orders.RequiredFields.FirstName = 1
  91. this.Shop.Orders.RequiredFields.MiddleName = 0
  92. this.Shop.Orders.RequiredFields.MobilePhone = 0
  93. this.Shop.Orders.RequiredFields.EmailAddress = 1
  94. this.Shop.Orders.RequiredFields.Delivery = 0
  95. this.Shop.Orders.RequiredFields.Comment = 0
  96. this.Shop.Orders.NotifyEmail = ""
  97. this.Shop.Orders.Enabled = 1
  98. this.API.XML.Enabled = 0
  99. this.API.XML.Name = ""
  100. this.API.XML.Company = ""
  101. this.API.XML.Url = ""
  102. this.SMTP.Host = ""
  103. this.SMTP.Port = 587
  104. this.SMTP.Login = ""
  105. this.SMTP.Password = ""
  106. }
  107. func (this *Config) ConfigRead(file string) error {
  108. f, err := os.Open(file)
  109. if err != nil {
  110. return err
  111. }
  112. defer f.Close()
  113. dec := json.NewDecoder(f)
  114. return dec.Decode(this)
  115. }
  116. func (this *Config) ConfigWrite(file string) error {
  117. r, err := json.Marshal(this)
  118. if err != nil {
  119. return err
  120. }
  121. f, err := os.Create(file)
  122. if err != nil {
  123. return err
  124. }
  125. defer f.Close()
  126. _, err = f.WriteString(string(r))
  127. return err
  128. }