123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package application
- import (
- "git.sxidc.com/go-framework/baize/framework/core/api"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
- "git.sxidc.com/go-framework/baize/framework/core/mqtt_api"
- )
- type App struct {
- // api实例
- apiInstance *api.Api
- // 基础设施实例
- infrastructureInstance *infrastructure.Infrastructure
- // 可选mqtt api
- mqttApiInstance *mqtt_api.MqttApi
- }
- // New 创建Application
- func New(api *api.Api, i *infrastructure.Infrastructure) *App {
- return &App{
- apiInstance: api,
- infrastructureInstance: i,
- }
- }
- // Start 运行应用
- // 参数: 无
- // 返回值:
- // - 错误
- func (app *App) Start() error {
- err := app.apiInstance.Start()
- if err != nil {
- return err
- }
- return nil
- }
- // Finish 终止应用
- // 参数: 无
- // 返回值:
- // - 错误
- func (app *App) Finish() error {
- err := app.apiInstance.Finish()
- if err != nil {
- return err
- }
- return nil
- }
- // Api 获取Api实例
- // 参数: 无
- // 返回值:
- // - Api实例
- func (app *App) Api() *api.Api {
- return app.apiInstance
- }
- // Infrastructure 获取Infrastructure实例
- // 参数: 无
- // 返回值:
- // - Infrastructure实例
- func (app *App) Infrastructure() *infrastructure.Infrastructure {
- return app.infrastructureInstance
- }
- // ChooseRouter 选择Router
- // 参数:
- // - routerType: 路由类型,有两种
- // - api.RouterRoot: 根路由
- // - api.RouterPrefix: 带url前缀的路由
- //
- // - version: 版本,用于获取基于上面两中路由够造的带有版本号的路由,传空字符串则选定上面两种路由本身
- // 返回值:
- // - 选择的Router
- func (app *App) ChooseRouter(routerType string, version string) api.Router {
- return app.Api().ChooseRouter(routerType, version)
- }
- func (app *App) StartMqttApi() error {
- if app.mqttApiInstance == nil {
- return nil
- }
- err := app.mqttApiInstance.Start()
- if err != nil {
- return err
- }
- return nil
- }
- func (app *App) FinishMqttApi() error {
- if app.mqttApiInstance != nil {
- app.mqttApiInstance.Finish()
- }
- err := app.apiInstance.Finish()
- if err != nil {
- return err
- }
- return nil
- }
- func (app *App) AddMqttApi(mqttApi *mqtt_api.MqttApi) {
- app.mqttApiInstance = mqttApi
- }
- func (app *App) MqttApi() *mqtt_api.MqttApi {
- return app.mqttApiInstance
- }
|