2020-12-21 15:56:56 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-12-22 15:00:42 +01:00
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-12-21 15:56:56 +01:00
|
|
|
"gitlab.com/wpetit/goweb/template/html"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/fake-sms/internal/command"
|
|
|
|
"forge.cadoles.com/Cadoles/fake-sms/internal/config"
|
2021-03-01 15:18:10 +01:00
|
|
|
"forge.cadoles.com/Cadoles/fake-sms/internal/model"
|
|
|
|
"forge.cadoles.com/Cadoles/fake-sms/internal/model/powow"
|
2020-12-21 15:56:56 +01:00
|
|
|
"forge.cadoles.com/Cadoles/fake-sms/internal/query"
|
|
|
|
"forge.cadoles.com/Cadoles/fake-sms/internal/storm"
|
|
|
|
"gitlab.com/wpetit/goweb/cqrs"
|
|
|
|
"gitlab.com/wpetit/goweb/service"
|
|
|
|
"gitlab.com/wpetit/goweb/service/build"
|
|
|
|
"gitlab.com/wpetit/goweb/service/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getServiceContainer(conf *config.Config) (*service.Container, error) {
|
|
|
|
// Initialize and configure service container
|
|
|
|
ctn := service.NewContainer()
|
|
|
|
|
|
|
|
ctn.Provide(build.ServiceName, build.ServiceProvider(ProjectVersion, GitRef, BuildDate))
|
|
|
|
|
|
|
|
// Create and expose template service provider
|
|
|
|
ctn.Provide(template.ServiceName, html.ServiceProvider(
|
|
|
|
html.NewDirectoryLoader(conf.HTTP.TemplateDir),
|
2020-12-22 15:00:42 +01:00
|
|
|
html.WithHelper("toPrettyJSON", func(data interface{}) (string, error) {
|
|
|
|
json, err := json.MarshalIndent(data, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(json), nil
|
|
|
|
}),
|
2020-12-21 15:56:56 +01:00
|
|
|
))
|
|
|
|
|
|
|
|
// Create and expose config service provider
|
|
|
|
ctn.Provide(config.ServiceName, config.ServiceProvider(conf))
|
|
|
|
|
|
|
|
ctn.Provide(storm.ServiceName, storm.ServiceProvider(
|
|
|
|
storm.WithPath(conf.Data.Path),
|
2021-03-01 15:18:10 +01:00
|
|
|
storm.WithObjects(
|
|
|
|
&model.SMS{},
|
|
|
|
&powow.SMSTemplate{},
|
|
|
|
),
|
2020-12-21 15:56:56 +01:00
|
|
|
))
|
|
|
|
|
|
|
|
ctn.Provide(cqrs.ServiceName, cqrs.ServiceProvider())
|
|
|
|
|
|
|
|
bus, err := cqrs.From(ctn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bus.RegisterCommand(
|
|
|
|
cqrs.MatchCommandRequest(&command.StoreSMSRequest{}),
|
|
|
|
cqrs.CommandHandlerFunc(command.HandleStoreSMS),
|
|
|
|
)
|
|
|
|
|
|
|
|
bus.RegisterCommand(
|
|
|
|
cqrs.MatchCommandRequest(&command.ClearOutboxRequest{}),
|
|
|
|
cqrs.CommandHandlerFunc(command.HandleClearOutbox),
|
|
|
|
)
|
|
|
|
|
|
|
|
bus.RegisterCommand(
|
|
|
|
cqrs.MatchCommandRequest(&command.DeleteSMSRequest{}),
|
|
|
|
cqrs.CommandHandlerFunc(command.HandleDeleteSMS),
|
|
|
|
)
|
|
|
|
|
|
|
|
bus.RegisterQuery(
|
|
|
|
cqrs.MatchQueryRequest(&query.GetOutboxRequest{}),
|
|
|
|
cqrs.QueryHandlerFunc(query.HandleGetOutbox),
|
|
|
|
)
|
|
|
|
|
|
|
|
bus.RegisterQuery(
|
|
|
|
cqrs.MatchQueryRequest(&query.OpenSMSRequest{}),
|
|
|
|
cqrs.QueryHandlerFunc(query.HandleOpenSMS),
|
|
|
|
)
|
|
|
|
|
|
|
|
return ctn, nil
|
|
|
|
}
|