package router import ( "os" "git.sendico.io/tech/sxchnge/product/auth/config" rauthorized "git.sendico.io/tech/sxchnge/product/auth/http/authorized" rpublic "git.sendico.io/tech/sxchnge/product/auth/http/public" "git.sendico.io/tech/sxchnge/product/auth/response" "git.sendico.io/tech/sxchnge/product/customers/store" "git.sendico.io/tech/sxchnge/product/sessions/store" api "git.sendico.io/tech/sxchnge/shared/http" "git.sendico.io/tech/sxchnge/shared/logging" "git.sendico.io/tech/sxchnge/shared/ratelimit/store" "git.sendico.io/tech/sxchnge/shared/service" "github.com/go-chi/chi/v5" ) type Dispatcher struct { logger mlogger.Logger public APIRouter protected ProtectedAPIRouter } func (d *Dispatcher) Handler(service mservice.Type, endpoint string, method api.HTTPMethod, handler sresponse.HandlerFunc) { d.public.InstallHandler(service, endpoint, method, handler) } func (d *Dispatcher) AccountHandler(service mservice.Type, endpoint string, method api.HTTPMethod, handler sresponse.AccountHandlerFunc) { d.protected.AccountHandler(service, endpoint, method, handler) } func (d *Dispatcher) PendingAccountHandler(service mservice.Type, endpoint string, method api.HTTPMethod, handler sresponse.PendingAccountHandlerFunc) { d.protected.PendingAccountHandler(service, endpoint, method, handler) } func NewDispatcher( logger mlogger.Logger, router chi.Router, db account.DB, rtdb refreshtokens.DB, attempts ratelimits.Store, config *middleware.Config, loginRateLimits middleware.LoginRateLimits, flows ...rpublic.Flows, ) *Dispatcher { d := &Dispatcher{ logger: logger.Named("api_dispatcher"), } d.logger.Debug("Installing endpoints middleware...") endpoint := os.Getenv(config.EndPointEnv) signature := middleware.SignatureConf(config) router.Group(func(r chi.Router) { d.public = rpublic.NewRouter(d.logger, endpoint, db, rtdb, attempts, r, &config.Token, &signature, loginRateLimits, flows...) }) router.Group(func(r chi.Router) { d.protected = rauthorized.NewRouter(d.logger, endpoint, r, db, rtdb, &config.Token, &signature) }) return d }