109 lines
2.9 KiB
Go
109 lines
2.9 KiB
Go
package runtime
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
|
|
"git.sendico.io/tech/sxchnge/product/app/api"
|
|
amr "git.sendico.io/tech/sxchnge/product/app/health"
|
|
httptransport "git.sendico.io/tech/sxchnge/product/app/http"
|
|
"git.sendico.io/tech/sxchnge/product/app/urls"
|
|
"git.sendico.io/tech/sxchnge/shared/database"
|
|
"git.sendico.io/tech/sxchnge/shared/logging"
|
|
"git.sendico.io/tech/sxchnge/shared/service"
|
|
"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,
|
|
apiRouter,
|
|
a.config.Mw,
|
|
a.config.AuthStorage,
|
|
debug,
|
|
a.config.Notifications,
|
|
); 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
|
|
}
|