package runtime import ( "context" "os" "git.sendico.io/tech/sxchnge/bff/edge/shared/interface/api" httptransport "git.sendico.io/tech/sxchnge/bff/edge/shared/internal/transport/http" amr "git.sendico.io/tech/sxchnge/bff/pkg/api/routers" "git.sendico.io/tech/sxchnge/bff/pkg/db" "git.sendico.io/tech/sxchnge/bff/pkg/domainprovider" "git.sendico.io/tech/sxchnge/bff/pkg/mlogger" "git.sendico.io/tech/sxchnge/bff/pkg/mservice" "github.com/go-chi/chi/v5" "go.uber.org/zap" ) func CreateAPI( lifecycleCtx context.Context, logger mlogger.Logger, config *api.Config, profileConfig api.ConfigProvider, db db.Factory, rootRouter *chi.Mux, apiRouter *chi.Mux, debug bool, runtime api.RuntimeConfig, ) (mservice.MicroService, error) { p := &APIImp{ lifecycleCtx: lifecycleCtx, logger: logger.Named("api"), config: config, profile: profileConfig, db: db, runtime: runtime, } if err := p.installRuntimeBasics(logger, db, rootRouter, apiRouter, debug); err != nil { return nil, err } if err := p.bootstrapConfiguredRuntime(config, db); err != nil { return nil, err } p.logger.Info("Installing microservices...", zap.String("runtime", runtime.RuntimeName())) if err := p.installServices(); err != nil { p.logger.Error("Failed to install a microservice", zap.Error(err)) return nil, err } p.logger.Info("Microservices installation complete", zap.Int("microservices", len(p.services))) return p, nil } func (a *APIImp) installRuntimeBasics( logger mlogger.Logger, db db.Factory, rootRouter *chi.Mux, apiRouter *chi.Mux, debug bool, ) error { var err error if a.domain, err = domainprovider.CreateDomainProvider( a.logger, a.config.Mw.DomainEnv, a.config.Mw.APIProtocolEnv, a.config.Mw.EndPointEnv, ); err != nil { a.logger.Error("Failed to initialize domain provider") return err } a.logger.Info("Domain provider installed") apiEndpoint := os.Getenv(a.config.Mw.EndPointEnv) if a.health, err = amr.NewHealthRouter(a.logger, rootRouter, apiEndpoint); err != nil { a.logger.Error("Failed to create healthcheck router", zap.Error(err), zap.String("api_endpoint", apiEndpoint)) return err } if a.mw, err = httptransport.CreateMiddleware( logger, db, a.db.Permissions().Enforcer(), apiRouter, a.config.Mw, a.config.AuthStorage, a.runtime.EnableWebSocket, debug, ); err != nil { a.logger.Error("Failed to create middleware", zap.Error(err)) return err } a.logger.Info("Middleware installed", zap.Bool("debug_mode", debug)) return nil } func (a *APIImp) bootstrapConfiguredRuntime(_ *api.Config, _ db.Factory) error { if a.runtime.Bootstrap != nil { components, err := a.runtime.Bootstrap(a) if err != nil { a.logger.Error("Failed to bootstrap runtime profile", zap.Error(err), zap.String("runtime", a.runtime.RuntimeName())) return err } a.components = components a.logger.Info("Runtime profile bootstrap complete", zap.String("runtime", a.runtime.RuntimeName()), zap.Int("components", len(components))) } return nil }