setting.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package mokuai
  2. import (
  3. "fmt"
  4. "github.com/go-ini/ini"
  5. "log"
  6. "runtime"
  7. "time"
  8. )
  9. type App struct {
  10. GinLog int
  11. Proxyapi string
  12. Proxyapi_data string
  13. }
  14. type Server struct {
  15. RunMode string
  16. HttpPort int
  17. ReadTimeout time.Duration
  18. WriteTimeout time.Duration
  19. }
  20. type Database struct {
  21. Type string
  22. Host string
  23. User string
  24. Password string
  25. DbName string
  26. RmHost string
  27. }
  28. var ServerConfig = &Server{}
  29. var AppConfig = &App{}
  30. var DatabaseConfig = &Database{}
  31. var (
  32. Cfg *ini.File
  33. )
  34. // 初始化配置文件
  35. func InitConfigFile() {
  36. // 修改此处初始化的配置文件内容
  37. var lin = "[UserCookies]\n#MT bbs Cookies (格式<有反单引号> : `cookies值`)\nMtCookies = \nMtProxy ="
  38. dirpath := File_Get_Current_Directory()
  39. if runtime.GOOS == "linux" {
  40. Dir_Create(dirpath + "/conf")
  41. File_WriteStr(dirpath+"/conf/app.ini", lin)
  42. } else if runtime.GOOS == "windows" {
  43. Dir_Create(dirpath + "\\conf")
  44. File_WriteStr(dirpath+"\\conf\\app.ini", lin)
  45. }
  46. }
  47. func IniSetup() {
  48. var err error
  49. dirpath := File_Get_Current_Directory()
  50. filePath := ""
  51. if runtime.GOOS == "linux" {
  52. _, reBool := File_IsExists(dirpath + "/conf/app.ini")
  53. if reBool == false {
  54. filePath = ""
  55. } else {
  56. filePath = dirpath + "/conf/app.ini"
  57. }
  58. } else if runtime.GOOS == "windows" {
  59. _, reBool := File_IsExists(dirpath + "\\conf\\app.ini")
  60. if reBool == false {
  61. filePath = ""
  62. } else {
  63. filePath = dirpath + "\\conf\\app.ini"
  64. }
  65. }
  66. if filePath == "" {
  67. if runtime.GOOS == "linux" {
  68. _, reBool := File_IsExists("conf/app.ini")
  69. if reBool == false {
  70. filePath = ""
  71. } else {
  72. filePath = "conf/app.ini"
  73. }
  74. } else if runtime.GOOS == "windows" {
  75. _, reBool := File_IsExists("conf\\app.ini")
  76. if reBool == false {
  77. filePath = ""
  78. } else {
  79. filePath = "conf\\app.ini"
  80. }
  81. }
  82. }
  83. if filePath == "" {
  84. InitConfigFile()
  85. log.Fatalf("fail to parse " + dirpath + "\\conf\\app.ini" + " <已创建初始化配置文件,请打开 conf/app.ini 配入信息>后 运行")
  86. return
  87. }
  88. Cfg, err = ini.Load(filePath)
  89. fmt.Println("配置文件 >> " + filePath)
  90. if err != nil {
  91. InitConfigFile()
  92. log.Fatalf("fail to parse " + dirpath + "\\conf\\app.ini" + " <已创建初始化配置文件,请打开 conf/app.ini 配入信息>后 运行")
  93. return
  94. }
  95. }
  96. func SaveConfig() {
  97. dirpath := File_Get_Current_Directory()
  98. if runtime.GOOS == "linux" {
  99. _ = Cfg.SaveTo(dirpath + "/conf/app.ini")
  100. } else if runtime.GOOS == "windows" {
  101. _ = Cfg.SaveTo(dirpath + "\\conf\\app.ini")
  102. }
  103. IniSetup()
  104. }