main.go 2.3 KB

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