application.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package application
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api"
  4. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  5. "git.sxidc.com/go-framework/baize/framework/core/mqtt_api"
  6. )
  7. type App struct {
  8. // api实例
  9. apiInstance *api.Api
  10. // 基础设施实例
  11. infrastructureInstance *infrastructure.Infrastructure
  12. // 可选mqtt api
  13. mqttApiInstance *mqtt_api.MqttApi
  14. }
  15. // New 创建Application
  16. func New(api *api.Api, i *infrastructure.Infrastructure) *App {
  17. return &App{
  18. apiInstance: api,
  19. infrastructureInstance: i,
  20. }
  21. }
  22. // Start 运行应用
  23. // 参数: 无
  24. // 返回值:
  25. // - 错误
  26. func (app *App) Start() error {
  27. err := app.apiInstance.Start()
  28. if err != nil {
  29. return err
  30. }
  31. return nil
  32. }
  33. // Finish 终止应用
  34. // 参数: 无
  35. // 返回值:
  36. // - 错误
  37. func (app *App) Finish() error {
  38. err := app.apiInstance.Finish()
  39. if err != nil {
  40. return err
  41. }
  42. return nil
  43. }
  44. // Api 获取Api实例
  45. // 参数: 无
  46. // 返回值:
  47. // - Api实例
  48. func (app *App) Api() *api.Api {
  49. return app.apiInstance
  50. }
  51. // Infrastructure 获取Infrastructure实例
  52. // 参数: 无
  53. // 返回值:
  54. // - Infrastructure实例
  55. func (app *App) Infrastructure() *infrastructure.Infrastructure {
  56. return app.infrastructureInstance
  57. }
  58. // ChooseRouter 选择Router
  59. // 参数:
  60. // - routerType: 路由类型,有两种
  61. // - api.RouterRoot: 根路由
  62. // - api.RouterPrefix: 带url前缀的路由
  63. //
  64. // - version: 版本,用于获取基于上面两中路由够造的带有版本号的路由,传空字符串则选定上面两种路由本身
  65. // 返回值:
  66. // - 选择的Router
  67. func (app *App) ChooseRouter(routerType string, version string) api.Router {
  68. return app.Api().ChooseRouter(routerType, version)
  69. }
  70. func (app *App) StartMqttApi() error {
  71. if app.mqttApiInstance == nil {
  72. return nil
  73. }
  74. err := app.mqttApiInstance.Start()
  75. if err != nil {
  76. return err
  77. }
  78. return nil
  79. }
  80. func (app *App) FinishMqttApi() error {
  81. if app.mqttApiInstance != nil {
  82. app.mqttApiInstance.Finish()
  83. }
  84. err := app.apiInstance.Finish()
  85. if err != nil {
  86. return err
  87. }
  88. return nil
  89. }
  90. func (app *App) AddMqttApi(mqttApi *mqtt_api.MqttApi) {
  91. app.mqttApiInstance = mqttApi
  92. }
  93. func (app *App) MqttApi() *mqtt_api.MqttApi {
  94. return app.mqttApiInstance
  95. }