1use std::collections::{HashMap, HashSet};
11use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe};
12use std::path::PathBuf;
13
14use axum::Router;
15use laterite_auth::{AuthConfig, AuthService};
16use laterite_core::config::{self, AppMeta, BackendConfig, DatabaseConfig, ServerConfig};
17use laterite_core::{CapabilitySet, Db, Module, ModuleRegistry, Registry, Translator};
18use serde::Deserialize;
19
20use crate::settings::SettingsItem;
21use crate::{builtin_modules, normalize_path, router, AdminConfig, Permission, Resource};
22
23pub const DEFAULT_ENV_PREFIX: &str = "LAT";
27
28fn resolve_env_prefix(code: Option<String>, declared: Option<String>) -> String {
31 code.or(declared)
32 .unwrap_or_else(|| DEFAULT_ENV_PREFIX.to_string())
33}
34
35#[cfg(test)]
36mod env_prefix_tests {
37 use super::resolve_env_prefix;
38
39 #[test]
40 fn code_override_then_declaration_then_default() {
41 assert_eq!(resolve_env_prefix(None, None), "LAT");
42 assert_eq!(resolve_env_prefix(None, Some("ACME".into())), "ACME");
43 assert_eq!(
44 resolve_env_prefix(Some("CODE".into()), Some("ACME".into())),
45 "CODE"
46 );
47 }
48}
49
50#[derive(Debug, Clone, Deserialize)]
53pub struct AppConfig {
54 #[serde(default)]
55 pub app: AppMeta,
56 pub server: ServerConfig,
57 pub database: DatabaseConfig,
58 #[serde(default)]
59 pub auth: AuthConfig,
60 #[serde(default)]
61 pub backend: BackendConfig,
62 #[serde(default)]
63 pub plugins: PluginsConfig,
64}
65
66#[derive(Debug, Clone, Deserialize, Default)]
70pub struct PluginsConfig {
71 #[serde(default)]
73 pub disabled: Vec<String>,
74}
75
76pub struct BootstrapCtx {
79 db: Db,
80 capabilities: CapabilitySet,
81 translator: Translator,
82}
83
84impl BootstrapCtx {
85 pub fn db(&self) -> Db {
87 self.db.clone()
88 }
89
90 pub fn capabilities(&self) -> &CapabilitySet {
93 &self.capabilities
94 }
95
96 pub fn translator(&self) -> &Translator {
99 &self.translator
100 }
101}
102
103type ExtendFn = Box<dyn FnOnce(Router, &BootstrapCtx) -> Router>;
104
105pub struct Bootstrap {
107 config_dir: PathBuf,
108 env_prefix: Option<String>,
109 app_modules: Vec<Box<dyn Module>>,
110 plugin_modules: Vec<Box<dyn Module>>,
111 extend: Option<ExtendFn>,
112}
113
114impl Bootstrap {
115 pub fn new(config_dir: impl Into<PathBuf>) -> Self {
120 Self {
121 config_dir: config_dir.into(),
122 env_prefix: None,
123 app_modules: Vec::new(),
124 plugin_modules: Vec::new(),
125 extend: None,
126 }
127 }
128
129 pub fn env_prefix(mut self, prefix: impl Into<String>) -> Self {
132 self.env_prefix = Some(prefix.into());
133 self
134 }
135
136 pub fn module(mut self, module: impl Module) -> Self {
141 self.app_modules.push(Box::new(module));
142 self
143 }
144
145 pub fn modules(mut self, modules: Vec<Box<dyn Module>>) -> Self {
150 self.plugin_modules.extend(modules);
151 self
152 }
153
154 pub fn extend(mut self, f: impl FnOnce(Router, &BootstrapCtx) -> Router + 'static) -> Self {
157 self.extend = Some(Box::new(f));
158 self
159 }
160
161 pub async fn serve(self) -> anyhow::Result<()> {
165 let _ = tracing_subscriber::fmt()
167 .with_env_filter(
168 tracing_subscriber::EnvFilter::try_from_default_env()
169 .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info,sqlx=warn")),
170 )
171 .try_init();
172
173 let env_prefix = resolve_env_prefix(
174 self.env_prefix.clone(),
175 config::declared_env_prefix(&self.config_dir)?,
176 );
177 let config: AppConfig = config::load(&self.config_dir, &env_prefix)?;
178 laterite_core::config::set_debug(config.app.debug);
180
181 let db = laterite_core::db::connect(&config.database).await?;
182
183 let plugin_ids: HashSet<String> = self
187 .plugin_modules
188 .iter()
189 .map(|m| m.id().to_string())
190 .collect();
191
192 laterite_core::migration::run(
197 &db.pool,
198 db.backend,
199 &[crate::plugins::PluginsModule.migrations()],
200 )
201 .await?;
202 crate::plugins::record_roster(&db, plugin_ids.iter().map(String::as_str)).await?;
203
204 let fingerprint = crate::plugins::binary_fingerprint();
210 let reconcile = crate::plugins::reconcile_boot(&db, &fingerprint).await?;
211 for id in &reconcile.crashed {
212 eprintln!(
213 "Plugin '{id}' quarantined: {}.",
214 crate::plugins::CRASH_REASON
215 );
216 }
217 for id in &reconcile.cleared {
218 println!("Plugin '{id}' quarantine lifted: the binary changed since it failed.");
219 }
220
221 let db_disabled = crate::plugins::disabled_ids(&db).await?;
222 let quarantined = crate::plugins::quarantined(&db).await?;
223
224 let mut registry = ModuleRegistry::new();
225 for module in builtin_modules() {
226 registry.register_boxed(module);
227 }
228 for module in self.app_modules {
229 registry.register_boxed(module);
230 }
231 for module in self.plugin_modules {
232 registry.register_boxed(module);
233 }
234 let capabilities =
235 laterite_core::capabilities::check(&db.pool, db.backend, ®istry).await?;
236
237 let ordered = registry.ordered()?;
241
242 let builtins: HashSet<String> = builtin_modules()
247 .iter()
248 .map(|m| m.id().to_string())
249 .collect();
250 let mut seed: HashMap<String, String> = HashMap::new();
251 for id in config.plugins.disabled.iter().chain(db_disabled.iter()) {
252 if builtins.contains(id) {
253 eprintln!("Ignoring request to disable built-in module '{id}'.");
254 } else {
255 seed.insert(id.clone(), "disabled".to_string());
256 }
257 }
258 for (id, reason) in &quarantined {
261 if !builtins.contains(id) {
262 seed.insert(id.clone(), format!("quarantined: {reason}"));
263 }
264 }
265 let skipped = skip_plan(&ordered, &seed);
266 report_plugins(&ordered, &skipped);
267 let active: Vec<&dyn Module> = ordered
268 .iter()
269 .copied()
270 .filter(|m| !skipped.contains_key(m.id().as_str()))
271 .collect();
272
273 let mut contributions = Registry::new();
278 let mut failed: HashMap<String, String> = HashMap::new();
279 for module in &active {
280 let mid = module.id();
281 let id = mid.as_str();
282 let eligible = plugin_ids.contains(id);
283
284 if let Some(dep) = module
285 .requires()
286 .iter()
287 .find(|r| failed.contains_key(r.as_str()))
288 {
289 let reason = format!("requires {}, which failed to load", dep.as_str());
290 println!("Plugin '{id}' off: {reason}");
291 failed.insert(id.to_string(), reason);
292 continue;
293 }
294
295 if eligible {
300 crate::plugins::mark_loading(&db, id, &fingerprint).await?;
301 }
302
303 match laterite_core::migration::run(&db.pool, db.backend, &[module.migrations()]).await
304 {
305 Ok(()) => {}
306 Err(e) if !eligible => return Err(e.into()),
307 Err(e) => {
308 let reason = format!("migration failed: {e}");
309 eprintln!("Plugin '{id}' quarantined: {reason}");
310 crate::plugins::quarantine(&db, id, &reason, &fingerprint).await?;
311 failed.insert(id.to_string(), reason);
312 continue;
313 }
314 }
315
316 contributions.set_owner(mid);
317 let registered = catch_unwind(AssertUnwindSafe(|| module.register(&mut contributions)));
318 match registered {
319 Ok(()) => {
322 if eligible {
323 crate::plugins::clear_loading(&db, id).await?;
324 }
325 }
326 Err(panic) => {
327 contributions.purge_owner(mid);
328 if !eligible {
329 resume_unwind(panic);
330 }
331 let reason = "register panicked".to_string();
332 eprintln!("Plugin '{id}' quarantined: {reason}");
333 crate::plugins::quarantine(&db, id, &reason, &fingerprint).await?;
334 failed.insert(id.to_string(), reason);
335 }
336 }
337 }
338 let bases: HashMap<String, Option<&'static str>> = active
342 .iter()
343 .map(|m| (m.id().to_string(), m.admin_base()))
344 .collect();
345 let mut resources = Vec::new();
346 for (owner, mut resource) in contributions.take_owned::<Resource>() {
347 let base = bases.get(owner.as_str()).copied().flatten();
348 crate::rebase_resource(
349 &crate::routemap::resolve(owner, &resource.base_path, base, &config.backend.paths),
350 &mut resource,
351 );
352 resources.push((owner, resource));
353 }
354 let mut screens = Vec::new();
355 for (owner, mut reg) in contributions.take_owned::<crate::routes::ScreenReg>() {
356 let base = bases.get(owner.as_str()).copied().flatten();
357 reg.base_path =
358 crate::routemap::resolve(owner, ®.base_path, base, &config.backend.paths);
359 screens.push((owner, reg));
360 }
361
362 let public = contributions.take_owned::<crate::routes::PublicRouteReg>();
363
364 let mut claims: Vec<(String, String)> = crate::builtin_resources()
367 .iter()
368 .map(|r| (r.base_path.clone(), "the framework".to_string()))
369 .collect();
370 claims.extend(
371 resources
372 .iter()
373 .map(|(owner, r)| (r.base_path.clone(), owner.to_string())),
374 );
375 claims.extend(
376 screens
377 .iter()
378 .map(|(owner, s)| (s.base_path.clone(), owner.to_string())),
379 );
380 crate::routemap::check_collisions(&claims);
381 let resources: Vec<Resource> = resources.into_iter().map(|(_, r)| r).collect();
382 let screens: Vec<crate::routes::ScreenReg> = screens.into_iter().map(|(_, s)| s).collect();
383 crate::routemap::check_public(
386 &public
387 .iter()
388 .map(|(owner, r)| (r.path.clone(), owner.to_string()))
389 .collect::<Vec<_>>(),
390 &crate::normalize_path(&config.backend.path),
391 );
392 let public: Vec<crate::routes::PublicRouteReg> =
393 public.into_iter().map(|(_, r)| r).collect();
394 let settings = contributions.take::<SettingsItem>();
395 let permissions = contributions.take::<Permission>();
396 let picker_sources = contributions.take::<crate::picker::PickerSourceReg>();
397 let persisters = contributions.take::<crate::persist::PersisterReg>();
398 let listeners = contributions.take::<laterite_core::ModelListenerReg>();
399
400 let auth = AuthService::new(db.clone(), config.auth.clone());
401 let origin = config::base_url(config.app.url.as_deref(), &config.server.listen);
402 let admin_config = AdminConfig {
403 secure_cookie: config.backend.secure_cookie,
404 timezone: config.backend.timezone.clone(),
405 locale: config.app.locale.clone(),
406 app_name: config.app.name.clone(),
407 path: config.backend.path.clone(),
408 origin: origin.clone(),
409 };
410 let mut catalogs = laterite_core::CatalogStore::builder();
414 for module in active
415 .iter()
416 .filter(|m| !failed.contains_key(m.id().as_str()))
417 {
418 for (locale, po) in module.catalogs() {
419 catalogs = catalogs.po(locale, po).map_err(|e| {
420 laterite_core::CoreError::Config(format!(
421 "module {}: {locale} catalog: {e}",
422 module.id().as_str()
423 ))
424 })?;
425 }
426 }
427
428 let mut app = router(
429 auth,
430 db.clone(),
431 resources,
432 settings,
433 permissions,
434 picker_sources,
435 persisters,
436 listeners,
437 screens,
438 public,
439 admin_config,
440 std::sync::Arc::new(catalogs.build()),
441 );
442
443 if let Some(extend) = self.extend {
444 let ctx = BootstrapCtx {
445 db,
446 capabilities,
447 translator: Translator::new(&config.app.locale),
448 };
449 app = extend(app, &ctx);
450 }
451
452 let listener = bind(&config.server.listen).await?;
453 let admin_path = normalize_path(&config.backend.path);
454 println!("{} on {origin}{admin_path}", config.app.name);
455 axum::serve(listener, app).await?;
456 Ok(())
457 }
458}
459
460async fn bind(listen: &str) -> anyhow::Result<tokio::net::TcpListener> {
463 use listenfd::ListenFd;
464 match ListenFd::from_env().take_tcp_listener(0)? {
465 Some(std_listener) => {
466 std_listener.set_nonblocking(true)?;
467 Ok(tokio::net::TcpListener::from_std(std_listener)?)
468 }
469 None => Ok(tokio::net::TcpListener::bind(listen).await?),
470 }
471}
472
473fn skip_plan(ordered: &[&dyn Module], seed: &HashMap<String, String>) -> HashMap<String, String> {
479 let mut skipped: HashMap<String, String> = HashMap::new();
480 for module in ordered {
481 let id = module.id().as_str();
482 if let Some(reason) = seed.get(id) {
483 skipped.insert(id.to_string(), reason.clone());
484 continue;
485 }
486 if let Some(dep) = module
487 .requires()
488 .iter()
489 .find(|r| skipped.contains_key(r.as_str()))
490 {
491 skipped.insert(
492 id.to_string(),
493 format!("requires {}, which is off", dep.as_str()),
494 );
495 }
496 }
497 skipped
498}
499
500fn report_plugins(ordered: &[&dyn Module], skipped: &HashMap<String, String>) {
503 if skipped.is_empty() {
504 return;
505 }
506 for module in ordered {
507 if let Some(reason) = skipped.get(module.id().as_str()) {
508 println!("Plugin '{}' off: {reason}", module.id());
509 }
510 }
511}
512
513#[cfg(test)]
514mod tests {
515 use super::*;
516 use laterite_core::ModuleId;
517
518 #[test]
519 fn env_prefix_is_unset_until_overridden_in_code() {
520 assert_eq!(Bootstrap::new("config").env_prefix, None);
521 assert_eq!(
522 Bootstrap::new("config")
523 .env_prefix("ACME")
524 .env_prefix
525 .as_deref(),
526 Some("ACME")
527 );
528 }
529
530 #[test]
531 fn app_config_loads_named_sections_and_falls_back_to_defaults() {
532 let dir = tempfile::tempdir().unwrap();
533 std::fs::write(
534 dir.path().join("default.toml"),
535 "[app]\nname = \"Acme\"\n\n[server]\nlisten = \"0.0.0.0:9000\"\n\n\
536 [database]\nurl = \"postgres://x\"\n",
537 )
538 .unwrap();
539 let cfg: AppConfig = config::load(dir.path(), "LATERITE_BOOTSTRAP_TEST").unwrap();
540 assert_eq!(cfg.app.name, "Acme");
541 assert_eq!(cfg.server.listen, "0.0.0.0:9000");
542 assert_eq!(cfg.database.url, "postgres://x");
543 assert_eq!(cfg.backend.path, "/admin");
544 assert!(!cfg.backend.secure_cookie);
545 assert!(cfg.app.url.is_none());
546 }
547
548 struct M(&'static str, &'static [ModuleId]);
549 impl Module for M {
550 fn id(&self) -> ModuleId {
551 ModuleId::new(self.0)
552 }
553 fn requires(&self) -> &'static [ModuleId] {
554 self.1
555 }
556 }
557
558 #[test]
559 fn skip_plan_disables_config_ids_and_cascades_to_dependents() {
560 const NONE: [ModuleId; 0] = [];
561 const REQ_A: [ModuleId; 1] = [ModuleId::new("acme.a")];
562 const REQ_B: [ModuleId; 1] = [ModuleId::new("acme.b")];
563 let (a, b, c, d) = (
566 M("acme.a", &NONE),
567 M("acme.b", &REQ_A),
568 M("acme.c", &REQ_B),
569 M("acme.d", &NONE),
570 );
571 let ordered: Vec<&dyn Module> = vec![&a, &b, &c, &d];
572
573 let seed: HashMap<String, String> = [("acme.a".to_string(), "disabled".to_string())]
574 .into_iter()
575 .collect();
576 let skipped = skip_plan(&ordered, &seed);
577
578 assert_eq!(skipped.get("acme.a").map(String::as_str), Some("disabled"));
580 assert!(skipped["acme.b"].contains("requires acme.a"));
581 assert!(skipped["acme.c"].contains("requires acme.b"));
582 assert!(!skipped.contains_key("acme.d"));
583 }
584}