1use anyhow::Context as _;
2use axum::Router;
3use axum::http::{HeaderValue, Method, header};
4use axum::middleware;
5use axum::response::Html;
6use platform_core::{
7 AppConfig, AppContext, LoggingEventPublisher, PostgresRuntimeConfigProvider,
8 RuntimeConfigRegistry, Shutdown, connect_pool, connect_redis, telemetry,
9};
10use platform_http::request_context_middleware;
11use std::net::SocketAddr;
12use std::path::PathBuf;
13use std::sync::Arc;
14use tower_http::cors::CorsLayer;
15use tower_http::services::{ServeDir, ServeFile};
16use tracing::info;
17
18pub mod openapi;
19
20pub use openapi::openapi_document;
21
22pub async fn run_from_env() -> anyhow::Result<()> {
23 run_from_env_with_composition(lenso_bootstrap::HostComposition::default()).await
24}
25
26pub async fn run_from_env_with_composition(
27 composition: lenso_bootstrap::HostComposition,
28) -> anyhow::Result<()> {
29 let config = AppConfig::try_from_env().context("invalid application configuration")?;
30 telemetry::init(&config.telemetry)?;
31
32 let db = connect_pool(&config.database).await?;
33 let redis = connect_redis(&config.redis).await?;
34 let mut ctx = AppContext::new(config, db, Arc::new(LoggingEventPublisher)).with_redis(redis);
35
36 let descriptors =
37 lenso_bootstrap::runtime_config_descriptors_with_composition(&ctx, &composition)
38 .context("failed to collect runtime-config descriptors")?;
39 let groups =
40 lenso_bootstrap::runtime_config_group_descriptors_with_composition(&ctx, &composition)
41 .context("failed to collect runtime-config groups")?;
42 let registry = RuntimeConfigRegistry::try_new_with_groups(descriptors, groups)
43 .context("duplicate runtime-config descriptor registered")?;
44 platform_admin::install_runtime_config_registry(registry.clone());
45 let runtime_config =
46 PostgresRuntimeConfigProvider::connect(ctx.db.clone(), Arc::new(registry), "api")
47 .await
48 .context("failed to load runtime-config snapshot")?;
49 runtime_config.spawn_listener();
50 ctx = ctx.with_runtime_config_provider(runtime_config);
51
52 let _remote_services = lenso_bootstrap::start_installed_remote_module_services(&ctx)
53 .await
54 .context("failed to start remote module services")?;
55
56 let admin_modules = lenso_bootstrap::load_admin_modules_with_composition(&ctx, &composition)
57 .await
58 .context("failed to load admin modules")?;
59 platform_admin_data::install_admin_modules(admin_modules);
60 let admin_module_metadata =
61 lenso_bootstrap::load_admin_module_metadata_with_composition(&ctx, &composition)
62 .await
63 .context("failed to load admin module metadata")?;
64 install_admin_module_metadata(admin_module_metadata);
65 let remote_http_proxy_registry = lenso_bootstrap::load_remote_http_proxy_registry(&ctx)
66 .await
67 .context("failed to load remote HTTP proxy registry")?;
68 platform_module_remote::install_remote_http_proxy_registry(remote_http_proxy_registry);
69
70 let admin_refresh_ctx = ctx.clone();
71 let admin_refresh_composition = composition.clone();
72 platform_admin_data::install_admin_module_refresh_fn(move || {
73 let ctx = admin_refresh_ctx.clone();
74 let composition = admin_refresh_composition.clone();
75 async move { lenso_bootstrap::load_admin_modules_with_composition(&ctx, &composition).await }
76 });
77 let admin_metadata_refresh_ctx = ctx.clone();
78 let admin_metadata_refresh_composition = composition.clone();
79 platform_admin_data::install_admin_module_metadata_refresh_fn(move || {
80 let ctx = admin_metadata_refresh_ctx.clone();
81 let composition = admin_metadata_refresh_composition.clone();
82 async move {
83 let metadata =
84 lenso_bootstrap::load_admin_module_metadata_with_composition(&ctx, &composition)
85 .await?;
86 install_platform_admin_catalogs(&metadata);
87 Ok(metadata)
88 }
89 });
90
91 let app = try_build_router_with_composition(ctx.clone(), &composition)
92 .context("failed to build API router")?;
93 let address: SocketAddr = format!("{}:{}", ctx.config.http.host, ctx.config.http.port)
94 .parse()
95 .context("invalid HTTP bind address")?;
96
97 info!(%address, "starting API server");
98 let listener = tokio::net::TcpListener::bind(address).await?;
99
100 let shutdown = ctx.shutdown.clone();
101 axum::serve(listener, app)
102 .with_graceful_shutdown(async move {
103 let mut shutdown_rx = shutdown.subscribe();
104 tokio::select! {
105 () = Shutdown::wait_for_signal() => {},
106 changed = shutdown_rx.changed() => {
107 let _ = changed;
108 },
109 }
110 })
111 .await?;
112
113 Ok(())
114}
115
116pub fn build_router(ctx: AppContext) -> Router {
117 try_build_router(ctx).expect("Runtime API router should build with a valid composition profile")
118}
119
120pub fn try_build_router(ctx: AppContext) -> platform_core::AppResult<Router> {
121 try_build_router_with_composition(ctx, &lenso_bootstrap::HostComposition::default())
122}
123
124pub fn try_build_router_with_composition(
125 mut ctx: AppContext,
126 composition: &lenso_bootstrap::HostComposition,
127) -> platform_core::AppResult<Router> {
128 if let Some(actor_resolver) =
129 lenso_bootstrap::auth_actor_resolver_for_context_with_composition(&ctx, composition)?
130 {
131 ctx = ctx.with_actor_resolver(actor_resolver);
132 }
133 let host_wiring = lenso_bootstrap::host_wiring_for_context_with_composition(&ctx, composition)?;
134 install_default_platform_admin_catalogs(&ctx, composition)?;
135 let (router, document) =
136 openapi::api_router_for_context_with_composition(&ctx, composition)?.split_for_parts();
137 let document = Arc::new(document);
138 let console_dist_dir = ctx.config.console.dist_dir.clone();
139 let console_index = PathBuf::from(&console_dist_dir).join("index.html");
140
141 Ok(router
142 .route("/docs", axum::routing::get(scalar_docs))
143 .route("/openapi.json", axum::routing::get(serve_openapi))
144 .nest_service(
145 "/console/extensions",
146 ServeDir::new(ctx.config.console.extensions_dir.clone()),
147 )
148 .nest_service(
149 "/console",
150 ServeDir::new(console_dist_dir).fallback(ServeFile::new(console_index)),
151 )
152 .layer(axum::Extension(document))
153 .layer(axum::Extension(host_wiring.auth_session_policy()))
154 .layer(middleware::from_fn_with_state(
155 ctx.clone(),
156 request_context_middleware,
157 ))
158 .layer(cors_layer(&ctx))
159 .with_state(ctx))
160}
161
162fn install_default_platform_admin_catalogs(
163 ctx: &AppContext,
164 composition: &lenso_bootstrap::HostComposition,
165) -> platform_core::AppResult<()> {
166 lenso_bootstrap::install_default_story_display_catalog_with_composition(ctx, composition)?;
167 platform_admin::install_default_runtime_function_declarations(
168 platform_admin::runtime_function_declarations_from_modules(
169 lenso_bootstrap::linked_runtime_function_declaration_sources_for_context_with_composition(
170 ctx,
171 composition,
172 )?,
173 ),
174 );
175 Ok(())
176}
177
178fn install_admin_module_metadata(metadata: Vec<platform_admin_data::AdminModuleMetadata>) {
179 install_platform_admin_catalogs(&metadata);
180 platform_admin_data::install_admin_module_metadata(metadata);
181}
182
183fn install_platform_admin_catalogs(metadata: &[platform_admin_data::AdminModuleMetadata]) {
184 lenso_bootstrap::install_story_display_catalog(metadata);
185 platform_admin::install_runtime_function_declarations(
186 platform_admin::runtime_function_declarations_from_modules(
187 lenso_bootstrap::runtime_function_declaration_sources_from_metadata(metadata),
188 ),
189 );
190}
191
192async fn scalar_docs() -> Html<&'static str> {
193 Html(SCALAR_DOCS_HTML)
194}
195
196async fn serve_openapi(
197 axum::Extension(document): axum::Extension<Arc<utoipa::openapi::OpenApi>>,
198) -> axum::Json<utoipa::openapi::OpenApi> {
199 axum::Json((*document).clone())
200}
201
202fn cors_layer(ctx: &AppContext) -> CorsLayer {
203 let origins: Vec<HeaderValue> = ctx
204 .config
205 .http
206 .cors_allowed_origins
207 .iter()
208 .filter_map(|origin| origin.parse().ok())
209 .collect();
210
211 CorsLayer::new()
212 .allow_origin(origins)
213 .allow_methods([
214 Method::GET,
215 Method::POST,
216 Method::PUT,
217 Method::PATCH,
218 Method::DELETE,
219 Method::OPTIONS,
220 ])
221 .allow_headers([header::ACCEPT, header::AUTHORIZATION, header::CONTENT_TYPE])
222}
223
224const SCALAR_DOCS_HTML: &str = r##"<!doctype html>
225<html lang="en">
226 <head>
227 <meta charset="utf-8" />
228 <meta name="viewport" content="width=device-width, initial-scale=1" />
229 <title>Lenso API Docs</title>
230 <script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
231 <style>
232 body {
233 margin: 0;
234 }
235 </style>
236 </head>
237 <body>
238 <div id="app"></div>
239 <script>
240 Scalar.createApiReference("#app", {
241 url: "/openapi.json",
242 theme: "default",
243 });
244 </script>
245 </body>
246</html>
247"##;