config.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. }
  29. API struct {
  30. XML struct {
  31. Enabled int
  32. Name string
  33. Company string
  34. Url string
  35. }
  36. }
  37. SMTP struct {
  38. Host string
  39. Port int
  40. Login string
  41. Password string
  42. }
  43. }
  44. func ConfigNew() *Config {
  45. c := &Config{}
  46. c.configDefault()
  47. return c
  48. }
  49. func (this *Config) configDefault() {
  50. this.Engine.MainModule = 0
  51. this.Blog.Pagination.Index = 5
  52. this.Blog.Pagination.Category = 5
  53. this.Shop.Pagination.Index = 9
  54. this.Shop.Pagination.Category = 9
  55. this.Shop.Thumbnails.Thumbnail0[0] = 100
  56. this.Shop.Thumbnails.Thumbnail0[1] = 100
  57. this.Shop.Thumbnails.Thumbnail0[2] = 0
  58. this.Shop.Thumbnails.Thumbnail1[0] = 200
  59. this.Shop.Thumbnails.Thumbnail1[1] = 200
  60. this.Shop.Thumbnails.Thumbnail1[2] = 2
  61. this.Shop.Thumbnails.Thumbnail2[0] = 250
  62. this.Shop.Thumbnails.Thumbnail2[1] = 250
  63. this.Shop.Thumbnails.Thumbnail2[2] = 2
  64. this.Shop.Thumbnails.Thumbnail3[0] = 450
  65. this.Shop.Thumbnails.Thumbnail3[1] = 450
  66. this.Shop.Thumbnails.Thumbnail3[2] = 2
  67. this.Shop.Thumbnails.ThumbnailFull[0] = 1000
  68. this.Shop.Thumbnails.ThumbnailFull[1] = 800
  69. this.Shop.Thumbnails.ThumbnailFull[2] = 1
  70. this.API.XML.Enabled = 0
  71. this.API.XML.Name = ""
  72. this.API.XML.Company = ""
  73. this.API.XML.Url = ""
  74. this.SMTP.Host = ""
  75. this.SMTP.Port = 587
  76. this.SMTP.Login = ""
  77. this.SMTP.Password = ""
  78. }
  79. func (this *Config) ConfigRead(file string) error {
  80. f, err := os.Open(file)
  81. if err != nil {
  82. return err
  83. }
  84. defer f.Close()
  85. dec := json.NewDecoder(f)
  86. return dec.Decode(this)
  87. }
  88. func (this *Config) ConfigWrite(file string) error {
  89. r, err := json.Marshal(this)
  90. if err != nil {
  91. return err
  92. }
  93. f, err := os.Create(file)
  94. if err != nil {
  95. return err
  96. }
  97. defer f.Close()
  98. _, err = f.WriteString(string(r))
  99. return err
  100. }