60 lines
1.7 KiB
Go
60 lines
1.7 KiB
Go
package apiimp
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/tech/sendico/notification/interface/middleware"
|
|
"github.com/tech/sendico/pkg/api/routers"
|
|
"github.com/tech/sendico/pkg/api/routers/health"
|
|
"github.com/tech/sendico/pkg/db/organization"
|
|
"github.com/tech/sendico/pkg/messaging"
|
|
notifications "github.com/tech/sendico/pkg/messaging/notifications/processor"
|
|
"github.com/tech/sendico/pkg/mlogger"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type Middleware struct {
|
|
logger mlogger.Logger
|
|
router *chi.Mux
|
|
apiEndpoint string
|
|
health routers.Health
|
|
messaging routers.Messaging
|
|
}
|
|
|
|
func (mw *Middleware) Consumer(processor notifications.EnvelopeProcessor) error {
|
|
return mw.messaging.Consumer(processor)
|
|
}
|
|
|
|
func (mw *Middleware) Producer() messaging.Producer {
|
|
return mw.messaging.Producer()
|
|
}
|
|
|
|
func (mw *Middleware) Finish() {
|
|
mw.messaging.Finish()
|
|
mw.health.Finish()
|
|
}
|
|
|
|
func (mw *Middleware) SetStatus(status health.ServiceStatus) {
|
|
mw.health.SetStatus(status)
|
|
}
|
|
|
|
func CreateMiddleware(logger mlogger.Logger, db organization.DB, router *chi.Mux, config *middleware.Config, debug bool) (*Middleware, error) {
|
|
p := &Middleware{
|
|
logger: logger.Named("middleware"),
|
|
router: router,
|
|
apiEndpoint: os.Getenv(config.EndPointEnv),
|
|
}
|
|
p.logger.Info("Set endpoint", zap.String("endpoint", p.apiEndpoint))
|
|
var err error
|
|
if p.messaging, err = routers.NewMessagingRouter(logger, &config.Messaging); err != nil {
|
|
p.logger.Error("Failed to create messaging router", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
if p.health, err = routers.NewHealthRouter(p.logger, p.router, p.apiEndpoint); err != nil {
|
|
p.logger.Error("Failed to create healthcheck router", zap.Error(err), zap.String("api_endpoint", p.apiEndpoint))
|
|
return nil, err
|
|
}
|
|
return p, nil
|
|
}
|