application.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package application
  2. import (
  3. "erpims/application/service"
  4. "erpims/config"
  5. "erpims/oss"
  6. "git.sxidc.com/go-framework/baize"
  7. "git.sxidc.com/go-framework/baize/framework/core/api"
  8. "git.sxidc.com/go-framework/baize/framework/core/application"
  9. "git.sxidc.com/service-supports/fslog"
  10. "net/http"
  11. )
  12. var appInstance *application.App
  13. func NewApp() {
  14. if appInstance != nil {
  15. return
  16. }
  17. appInstance = baize.NewApplication(config.GetConfig().ApplicationConfig)
  18. //注册OSS
  19. ossCfg := config.GetConfig().OssConfig
  20. err := oss.Init(ossCfg.Endpoint, ossCfg.KeyID, ossCfg.KeySecret, ossCfg.Prefix)
  21. if err != nil {
  22. fslog.Error(err)
  23. panic(err)
  24. }
  25. // 注册Router
  26. appInstance.Api().PrefixRouter().RegisterVersionedRouter("v1")
  27. }
  28. func DestroyApp() {
  29. if appInstance == nil {
  30. return
  31. }
  32. baize.DestroyApplication(appInstance)
  33. appInstance = nil
  34. }
  35. func Start() error {
  36. // 初始化服务
  37. for _, svc := range service.RegisteredServices {
  38. err := svc.Init(appInstance)
  39. if err != nil {
  40. return err
  41. }
  42. }
  43. err := appInstance.Start()
  44. if err != nil {
  45. return err
  46. }
  47. return nil
  48. }
  49. func Finish() error {
  50. err := appInstance.Finish()
  51. if err != nil {
  52. return err
  53. }
  54. // 销毁服务
  55. for _, svc := range service.RegisteredServices {
  56. err := svc.Destroy()
  57. if err != nil {
  58. return err
  59. }
  60. }
  61. return nil
  62. }
  63. func ServerHttpForTest(w http.ResponseWriter, req *http.Request) {
  64. appInstance.Api().RootRouter().(*api.RootRouter).ServerHttp(w, req)
  65. }