config.go 2.8 KB

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