main.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package main
  2. import (
  3. "Medical_ERP/common/global"
  4. _ "Medical_ERP/common/initialize"
  5. "Medical_ERP/conf"
  6. "Medical_ERP/controllers"
  7. _ "Medical_ERP/models"
  8. _ "Medical_ERP/routers"
  9. "fmt"
  10. beego "github.com/beego/beego/v2/server/web"
  11. "github.com/go-resty/resty/v2"
  12. "github.com/tidwall/gjson"
  13. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  14. _ "image/jpeg"
  15. _ "image/png"
  16. "log"
  17. "net/http"
  18. "os"
  19. )
  20. //go:generate swag init --parseDependency --parseDepth=6
  21. // @title 药品进销存管理系统
  22. // @version 1.0
  23. // @description 药品进销存管理系统
  24. // @Host 192.168.11.77:8110
  25. // @BasePath /api
  26. // @Schemes http
  27. // @securityDefinitions.apikey Bearer
  28. // @in header
  29. // @name Authorization
  30. func main() {
  31. if beego.BConfig.RunMode == "dev" {
  32. //beego.BConfig.WebConfig.DirectoryIndex = true
  33. //beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
  34. beego.SetStaticPath("/swagger", "docs")
  35. }
  36. // 错误处理
  37. beego.ErrorController(&controllers.ErrorController{})
  38. // 从 panic 中恢复
  39. beego.BConfig.RecoverPanic = true
  40. //go func() {
  41. // // 初始化服务发现
  42. // InitServiceDiscovery()
  43. //}()
  44. go InitServiceDiscovery()
  45. //models.BatchAlterMedicineTemplateTable() // 批量修改表结构
  46. //models.SyncMedicineInfoToMedicineImg()
  47. fmt.Println(pkg.Green("Server run at:"))
  48. fmt.Printf("- Local: http://127.0.0.1:%d/ \r\n", conf.Httpport)
  49. fmt.Printf("- Network: http://%s:%d/ \r\n", pkg.GetLocalHost(), conf.Httpport)
  50. fmt.Println(pkg.Green("Swagger run at:"))
  51. fmt.Printf("- Local: http://127.0.0.1:%d/swagger/index.html \r\n", conf.Httpport)
  52. fmt.Printf("- Network: http://%s:%d/swagger/index.html \r\n", pkg.GetLocalHost(), conf.Httpport)
  53. beego.Run()
  54. }
  55. // 初始化服务发现
  56. func InitServiceDiscovery() {
  57. AuthCode, _ := beego.AppConfig.String("Service_authCode")
  58. Host, _ := beego.AppConfig.String("Service_host")
  59. Name, _ := beego.AppConfig.String("Service_name")
  60. RoleApiUrl, _ := beego.AppConfig.String("Service_roleApiUrl")
  61. reqPath := "/api/service-disco"
  62. url := conf.OAuthBaseUrl + reqPath
  63. r, reqError := resty.New().R().SetBody(map[string]string{
  64. "authCode": AuthCode,
  65. "host": Host,
  66. "name": Name,
  67. "roleApiUrl": RoleApiUrl,
  68. }).Post(url)
  69. if reqError != nil {
  70. log.Fatal("初始化服务发现失败", reqError)
  71. os.Exit(-1)
  72. }
  73. code := gjson.GetBytes(r.Body(), "code").Int()
  74. id := gjson.GetBytes(r.Body(), "data").Int()
  75. if code != http.StatusOK {
  76. log.Fatal("初始化服务发现失败", string(r.Body()))
  77. os.Exit(-1)
  78. }
  79. global.ServiceId = int(id)
  80. }