application.go 1.3 KB

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