application.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package application
  2. import (
  3. "baize-playground/server/application/service"
  4. "baize-playground/server/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. }
  19. func DestroyApp() {
  20. if appInstance == nil {
  21. return
  22. }
  23. baize.DestroyApplication(appInstance)
  24. appInstance = nil
  25. }
  26. func Start() error {
  27. // 初始化服务
  28. for _, svc := range service.RegisteredServices {
  29. err := svc.Init(appInstance)
  30. if err != nil {
  31. return err
  32. }
  33. }
  34. err := appInstance.Start()
  35. if err != nil {
  36. return err
  37. }
  38. return nil
  39. }
  40. func Finish() error {
  41. err := appInstance.Finish()
  42. if err != nil {
  43. return err
  44. }
  45. // 销毁服务
  46. for _, svc := range service.RegisteredServices {
  47. err := svc.Destroy()
  48. if err != nil {
  49. return err
  50. }
  51. }
  52. return nil
  53. }
  54. func ServerHttpForTest(w http.ResponseWriter, req *http.Request) {
  55. appInstance.Api().RootRouter().(*api.RootRouter).ServerHttp(w, req)
  56. }