application.go 1.1 KB

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