coil-runtime 0.1.1

HTTP runtime and request handling for the Coil framework.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
use super::*;
use coil_auth::{LoadedAuthModelPackage, load_auth_model_package_at};
use coil_config::{CustomerAppBootstrapManifest, CustomerAppBootstrapManifestError};
use coil_i18n::TranslationCatalog;
use std::collections::BTreeSet;
use std::env;
use std::path::Path;
use std::path::PathBuf;

/// Explicit runtime bootstrap entrypoint for ADR 96 customer-root binaries/workspaces.
///
/// This keeps the linked-customer composition path visible in code without forcing every
/// customer binary to start from the lower-level `RuntimeBuilder::new(...)` surface.
pub struct CustomerRootRuntimeBuilder<P> {
    inner: RuntimeBuilder<P>,
    config: PlatformConfig,
    app_root: Option<PathBuf>,
    enabled_modules: Vec<String>,
}

pub struct CustomerRootBootstrapInputs {
    pub app_root: PathBuf,
    pub manifest_path: PathBuf,
    pub enabled_modules: Vec<String>,
    pub config_path: PathBuf,
    pub config: PlatformConfig,
    pub auth_package_name: String,
    pub auth_package: LoadedAuthModelPackage,
    pub translation_catalogs: Vec<TranslationCatalog>,
}

pub fn customer_root_runtime<P>(
    config: PlatformConfig,
    auth_package: P,
) -> CustomerRootRuntimeBuilder<P>
where
    P: AuthModelPackage + 'static,
{
    CustomerRootRuntimeBuilder::new(config, auth_package)
}

pub fn customer_root_runtime_from_env()
-> Result<CustomerRootRuntimeBuilder<LoadedAuthModelPackage>, RuntimeBootstrapError> {
    CustomerRootRuntimeBuilder::from_env()
}

pub fn customer_root_runtime_from_paths(
    app_root: impl AsRef<Path>,
    config_path: impl AsRef<Path>,
) -> Result<CustomerRootRuntimeBuilder<LoadedAuthModelPackage>, RuntimeBootstrapError> {
    CustomerRootRuntimeBuilder::from_paths(app_root, config_path)
}

pub fn customer_root_bootstrap_inputs_from_env()
-> Result<CustomerRootBootstrapInputs, RuntimeBootstrapError> {
    CustomerRootBootstrapInputs::from_env()
}

pub fn customer_root_bootstrap_inputs_from_paths(
    app_root: impl AsRef<Path>,
    config_path: impl AsRef<Path>,
) -> Result<CustomerRootBootstrapInputs, RuntimeBootstrapError> {
    CustomerRootBootstrapInputs::from_paths(app_root, config_path)
}

impl<P> CustomerRootRuntimeBuilder<P>
where
    P: AuthModelPackage + 'static,
{
    pub fn new(config: PlatformConfig, auth_package: P) -> Self {
        Self {
            inner: RuntimeBuilder::new(config.clone(), auth_package),
            config,
            app_root: None,
            enabled_modules: Vec::new(),
        }
    }

    /// Mount a customer workspace/app root that contains the checked-in template tree.
    ///
    /// This is the customer-root equivalent of `with_template_root(...)`.
    pub fn with_customer_root<A>(mut self, root: A) -> Self
    where
        A: Into<PathBuf>,
    {
        let root = root.into();
        self.app_root = Some(root.clone());
        self.inner = self.inner.with_template_root(root);
        self
    }

    /// Link a native customer backend plugin into the runtime plan.
    pub fn with_linked_customer_plugin<C>(mut self, plugin: C) -> Self
    where
        C: CustomerBackendPlugin,
    {
        self.inner = self.inner.register_customer_plugin(plugin);
        self
    }

    pub fn with_boxed_linked_customer_plugin(
        mut self,
        plugin: Box<dyn CustomerBackendPlugin>,
    ) -> Self {
        self.inner = self.inner.with_boxed_customer_plugin(plugin);
        self
    }

    pub fn with_module<M>(mut self, module: M) -> Self
    where
        M: PlatformModule + 'static,
    {
        self.inner = self.inner.with_module(module);
        self
    }

    pub fn register_module<M>(self, module: M) -> Self
    where
        M: PlatformModule + 'static,
    {
        self.with_module(module)
    }

    pub fn with_boxed_module(mut self, module: Box<dyn PlatformModule>) -> Self {
        self.inner = self.inner.with_boxed_module(module);
        self
    }

    pub fn register_customer_plugin<C>(self, plugin: C) -> Self
    where
        C: CustomerBackendPlugin,
    {
        self.with_linked_customer_plugin(plugin)
    }

    pub fn with_installed_extension(mut self, extension: InstalledExtension) -> Self {
        self.inner = self.inner.with_installed_extension(extension);
        self
    }

    pub fn with_template(mut self, template: coil_template::TemplateDefinition) -> Self {
        self.inner = self.inner.with_template(template);
        self
    }

    pub fn with_templates<I>(mut self, templates: I) -> Self
    where
        I: IntoIterator<Item = coil_template::TemplateDefinition>,
    {
        self.inner = self.inner.with_templates(templates);
        self
    }

    pub fn with_storage_policy_rule(mut self, rule: PathPolicyRule) -> Self {
        self.inner = self.inner.with_storage_policy_rule(rule);
        self
    }

    pub fn with_storage_policies(mut self, policies: StoragePolicySet) -> Self {
        self.inner = self.inner.with_storage_policies(policies);
        self
    }

    pub fn with_route(mut self, route: RouteDefinition) -> Self {
        self.inner = self.inner.with_route(route);
        self
    }

    pub fn with_handler(mut self, handler: HandlerDefinition) -> Self {
        self.inner = self.inner.with_handler(handler);
        self
    }

    pub fn with_feature_flag(mut self, feature_flag: FeatureFlag) -> Self {
        self.inner = self.inner.with_feature_flag(feature_flag);
        self
    }

    pub fn with_translation_catalog(mut self, catalog: TranslationCatalog) -> Self {
        self.inner = self.inner.with_translation_catalog(catalog);
        self
    }

    pub fn with_translation_catalogs<I>(mut self, catalogs: I) -> Self
    where
        I: IntoIterator<Item = TranslationCatalog>,
    {
        self.inner = self.inner.with_translation_catalogs(catalogs);
        self
    }

    pub fn with_maintenance_mode(mut self, maintenance_mode: MaintenanceMode) -> Self {
        self.inner = self.inner.with_maintenance_mode(maintenance_mode);
        self
    }

    /// Drop to the lower-level runtime builder when the customer binary needs advanced knobs that
    /// are not part of the customer-root convenience surface yet.
    pub fn into_runtime_builder(self) -> RuntimeBuilder<P> {
        self.inner
    }

    pub fn build(self) -> Result<RuntimePlan, RuntimeBuildError> {
        let enabled_modules = if self.enabled_modules.is_empty() {
            self.resolve_enabled_modules_from_customer_root()?
        } else {
            self.enabled_modules
        };
        self.inner
            .resolve_enabled_customer_modules(&enabled_modules)?
            .build()
    }

    pub fn run_from_env(self) -> Result<(), RuntimeBootstrapError> {
        self.build()?.serve_from_env(env::var("COIL_BIND").ok())
    }
}

impl CustomerRootRuntimeBuilder<LoadedAuthModelPackage> {
    pub fn from_env() -> Result<Self, RuntimeBootstrapError> {
        let app_root = env::current_dir().map_err(RuntimeBootstrapError::CurrentDirectory)?;
        let config_path = discover_default_config_path(&app_root).ok_or_else(|| {
            RuntimeBootstrapError::ConfigNotFound {
                app_root: app_root.clone(),
            }
        })?;
        Self::from_paths(app_root, config_path)
    }

    pub fn from_paths(
        app_root: impl AsRef<Path>,
        config_path: impl AsRef<Path>,
    ) -> Result<Self, RuntimeBootstrapError> {
        let inputs = CustomerRootBootstrapInputs::from_paths(app_root, config_path)?;
        Ok(Self::from_bootstrap_inputs(inputs))
    }

    pub fn from_bootstrap_inputs(inputs: CustomerRootBootstrapInputs) -> Self {
        let mut builder = Self::new(inputs.config, inputs.auth_package)
            .with_customer_root(inputs.app_root)
            .with_translation_catalogs(inputs.translation_catalogs);
        builder.enabled_modules = inputs.enabled_modules;
        builder
    }
}

impl<P> CustomerRootRuntimeBuilder<P>
where
    P: AuthModelPackage + 'static,
{
    fn resolve_enabled_modules_from_customer_root(&self) -> Result<Vec<String>, RuntimeBuildError> {
        let Some(app_root) = &self.app_root else {
            return Err(RuntimeBuildError::CustomerRootNotConfigured);
        };
        let manifest_path = app_root.join("app.toml");
        let manifest = load_customer_root_manifest(&manifest_path)
            .map_err(|error| customer_root_manifest_error_into_build(&manifest_path, error))?;
        manifest
            .validate_runtime_config_alignment(&self.config)
            .map_err(|error| customer_root_manifest_error_into_build(&manifest_path, error))?;
        Ok(manifest.enabled_modules().to_vec())
    }
}

impl CustomerRootBootstrapInputs {
    pub fn from_env() -> Result<Self, RuntimeBootstrapError> {
        let app_root = env::current_dir().map_err(RuntimeBootstrapError::CurrentDirectory)?;
        let config_path = discover_default_config_path(&app_root).ok_or_else(|| {
            RuntimeBootstrapError::ConfigNotFound {
                app_root: app_root.clone(),
            }
        })?;
        Self::from_paths(app_root, config_path)
    }

    pub fn from_paths(
        app_root: impl AsRef<Path>,
        config_path: impl AsRef<Path>,
    ) -> Result<Self, RuntimeBootstrapError> {
        let app_root = app_root.as_ref().to_path_buf();
        let config_path = resolve_path(&app_root, config_path.as_ref());
        let manifest_path = app_root.join("app.toml");
        let config = PlatformConfig::from_file(&config_path).map_err(|error| {
            RuntimeBootstrapError::ConfigLoad {
                path: config_path.clone(),
                reason: error.to_string(),
            }
        })?;
        let manifest = load_customer_root_manifest(&manifest_path)
            .map_err(|error| customer_root_manifest_error_into_bootstrap(&manifest_path, error))?;
        manifest
            .validate_runtime_config_alignment(&config)
            .map_err(|error| customer_root_manifest_error_into_bootstrap(&manifest_path, error))?;
        let translation_catalogs = load_customer_translation_catalogs(&app_root, &manifest)
            .map_err(|reason| RuntimeBootstrapError::ManifestLoad {
                path: manifest_path.clone(),
                reason,
            })?;
        let auth_package_name = config.auth.package.clone();
        let auth_package =
            load_auth_model_package_at(&auth_package_name, &app_root).map_err(|error| {
                RuntimeBootstrapError::AuthPackageLoad {
                    package: auth_package_name.clone(),
                    app_root: app_root.clone(),
                    reason: error.to_string(),
                }
            })?;

        Ok(Self {
            app_root,
            manifest_path,
            enabled_modules: manifest.enabled_modules().to_vec(),
            config_path,
            config,
            auth_package_name,
            auth_package,
            translation_catalogs,
        })
    }
}

fn resolve_path(app_root: &Path, path: &Path) -> PathBuf {
    if path.is_absolute() {
        path.to_path_buf()
    } else {
        app_root.join(path)
    }
}

fn discover_default_config_path(app_root: &Path) -> Option<PathBuf> {
    env::var("COIL_CONFIG")
        .ok()
        .map(PathBuf::from)
        .map(|path| resolve_path(app_root, &path))
        .filter(|path| path.is_file())
        .or_else(|| {
            [
                PathBuf::from("platform.toml"),
                PathBuf::from("platform.dev.toml"),
                PathBuf::from("config/platform.toml"),
                PathBuf::from("coil.toml"),
                PathBuf::from("config/coil.toml"),
            ]
            .into_iter()
            .map(|path| app_root.join(path))
            .find(|path| path.is_file())
        })
}

fn load_customer_root_manifest(
    manifest_path: &Path,
) -> Result<CustomerAppBootstrapManifest, CustomerAppBootstrapManifestError> {
    CustomerAppBootstrapManifest::from_file(manifest_path)
}

fn customer_root_manifest_error_into_bootstrap(
    manifest_path: &Path,
    error: CustomerAppBootstrapManifestError,
) -> RuntimeBootstrapError {
    match error {
        CustomerAppBootstrapManifestError::Read { path, reason }
        | CustomerAppBootstrapManifestError::Parse { path, reason } => {
            RuntimeBootstrapError::ManifestLoad { path, reason }
        }
        other => RuntimeBootstrapError::ManifestLoad {
            path: manifest_path.to_path_buf(),
            reason: other.to_string(),
        },
    }
}

fn customer_root_manifest_error_into_build(
    manifest_path: &Path,
    error: CustomerAppBootstrapManifestError,
) -> RuntimeBuildError {
    match error {
        CustomerAppBootstrapManifestError::Read { path, reason }
        | CustomerAppBootstrapManifestError::Parse { path, reason } => {
            RuntimeBuildError::CustomerManifestLoad { path, reason }
        }
        other => RuntimeBuildError::CustomerManifestLoad {
            path: manifest_path.to_path_buf(),
            reason: other.to_string(),
        },
    }
}

fn load_customer_translation_catalogs(
    app_root: &Path,
    manifest: &CustomerAppBootstrapManifest,
) -> Result<Vec<TranslationCatalog>, String> {
    let supported_locales = manifest.supported_locales();
    let mut seen_locales = BTreeSet::new();
    manifest
        .translation_catalogs()
        .iter()
        .map(|catalog| {
            if !supported_locales.iter().any(|locale| locale == catalog.locale()) {
                return Err(format!(
                    "translation catalog locale `{}` is not declared in the customer app supported locale set",
                    catalog.locale()
                ));
            }
            if !seen_locales.insert(catalog.locale().to_string()) {
                return Err(format!(
                    "translation catalog for locale `{}` is declared more than once",
                    catalog.locale()
                ));
            }
            let path = app_root.join(catalog.path());
            TranslationCatalog::from_toml_file(
                coil_i18n::LocaleTag::new(catalog.locale()).map_err(|error| error.to_string())?,
                &path,
            )
            .map_err(|error| {
                format!(
                    "failed to load translation catalog `{}` for locale `{}`: {error}",
                    catalog.path(),
                    catalog.locale()
                )
            })
        })
        .collect()
}

pub(crate) fn resolve_enabled_customer_modules(
    enabled_modules: &[String],
    modules: Vec<Box<dyn PlatformModule>>,
) -> Result<Vec<Box<dyn PlatformModule>>, RuntimeBuildError> {
    let enabled = enabled_modules.iter().cloned().collect::<BTreeSet<_>>();
    let mut linked = BTreeSet::new();
    let mut selected = Vec::new();
    for module in modules {
        let name = module.manifest().name;
        linked.insert(name.clone());
        if enabled.contains(&name) {
            selected.push(module);
        }
    }
    let missing = enabled
        .into_iter()
        .filter(|module| !linked.contains(module))
        .collect::<Vec<_>>();
    if !missing.is_empty() {
        return Err(RuntimeBuildError::CustomerManifestMissingLinkedModules { modules: missing });
    }
    Ok(selected)
}