12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package baize
- import (
- "git.sxidc.com/go-framework/baize/framework/core/api"
- "git.sxidc.com/go-framework/baize/framework/core/application"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
- "git.sxidc.com/go-framework/baize/framework/core/mqtt_api"
- )
- func NewApplication(conf application.Config) *application.App {
- // 创建API
- apiConfig := conf.ApiConfig
- apiInstance := api.New(api.WithUrlPrefix(apiConfig.UrlPrefix),
- api.WithPort(apiConfig.Port),
- api.WithLogSkipPaths(apiConfig.LogSkipPaths...))
- // 创建基础设施
- infrastructureConfig := new(infrastructure.Config)
- infrastructureConfig.DatabaseConfig = conf.InfrastructureConfig.Database
- infrastructureConfig.CacheConfig = conf.InfrastructureConfig.Cache
- infrastructureInstance := infrastructure.NewInfrastructure(*infrastructureConfig)
- app := application.New(apiInstance, infrastructureInstance)
- // 添加MqttApi
- if conf.MqttApiConfig != nil {
- mqttConfig := conf.MqttApiConfig
- mqttApi, err := mqtt_api.New(mqtt_api.WithTopicPrefix(mqttConfig.TopicPrefix),
- mqtt_api.WithLogSkipPaths(mqttConfig.LogSkipPaths...),
- mqtt_api.WithMqttOptions(&mqtt_api.MqttClientOptions{
- UserName: mqttConfig.MqttConfig.UserName,
- Password: mqttConfig.MqttConfig.Password,
- Address: mqttConfig.MqttConfig.Address,
- ClientID: mqttConfig.MqttConfig.ClientID,
- KeepAliveSec: mqttConfig.MqttConfig.KeepAliveSec,
- PingTimeoutSec: mqttConfig.MqttConfig.PingTimeoutSec,
- WriteTimeoutSec: mqttConfig.MqttConfig.WriteTimeoutSec,
- }))
- if err != nil {
- panic(err)
- }
- app.AddMqttApi(mqttApi)
- }
- return app
- }
- func DestroyApplication(app *application.App) {
- if app == nil {
- return
- }
- mqttApi := app.MqttApi()
- if mqttApi != nil {
- mqtt_api.Destroy(mqttApi)
- }
- infrastructure.DestroyInfrastructure(app.Infrastructure())
- }
|