config.go 2.7 KB

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