baize.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package baize
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api"
  4. "git.sxidc.com/go-framework/baize/framework/core/application"
  5. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  6. "git.sxidc.com/go-framework/baize/framework/core/mqtt_api"
  7. )
  8. func NewApplication(conf application.Config) *application.App {
  9. // 创建API
  10. apiConfig := conf.ApiConfig
  11. apiInstance := api.New(api.WithUrlPrefix(apiConfig.UrlPrefix),
  12. api.WithPort(apiConfig.Port),
  13. api.WithLogSkipPaths(apiConfig.LogSkipPaths...))
  14. // 创建基础设施
  15. infrastructureConfig := new(infrastructure.Config)
  16. infrastructureConfig.DatabaseConfig = conf.InfrastructureConfig.Database
  17. infrastructureConfig.CacheConfig = conf.InfrastructureConfig.Cache
  18. infrastructureInstance := infrastructure.NewInfrastructure(*infrastructureConfig)
  19. app := application.New(apiInstance, infrastructureInstance)
  20. // 添加MqttApi
  21. if conf.MqttApiConfig != nil {
  22. mqttConfig := conf.MqttApiConfig
  23. mqttApi, err := mqtt_api.New(mqtt_api.WithTopicPrefix(mqttConfig.TopicPrefix),
  24. mqtt_api.WithLogSkipPaths(mqttConfig.LogSkipPaths...),
  25. mqtt_api.WithMqttOptions(&mqtt_api.MqttClientOptions{
  26. UserName: mqttConfig.MqttConfig.UserName,
  27. Password: mqttConfig.MqttConfig.Password,
  28. Address: mqttConfig.MqttConfig.Address,
  29. ClientID: mqttConfig.MqttConfig.ClientID,
  30. KeepAliveSec: mqttConfig.MqttConfig.KeepAliveSec,
  31. PingTimeoutSec: mqttConfig.MqttConfig.PingTimeoutSec,
  32. WriteTimeoutSec: mqttConfig.MqttConfig.WriteTimeoutSec,
  33. }))
  34. if err != nil {
  35. panic(err)
  36. }
  37. app.AddMqttApi(mqttApi)
  38. }
  39. return app
  40. }
  41. func DestroyApplication(app *application.App) {
  42. if app == nil {
  43. return
  44. }
  45. mqttApi := app.MqttApi()
  46. if mqttApi != nil {
  47. mqtt_api.Destroy(mqttApi)
  48. }
  49. infrastructure.DestroyInfrastructure(app.Infrastructure())
  50. }