apiplant-core 0.1.0

Core types for apiplant: configuration, errors and the resource schema model
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
//! The loaded application: an app directory turned into config + resources.

use crate::config::Config;
use crate::schema::{Field, FieldAdmin, FieldType, OnDelete, Resource};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

/// Everything the server needs, assembled from an app directory.
#[derive(Debug, Clone)]
pub struct App {
    /// Root of the app directory.
    pub root: PathBuf,
    pub config: Config,
    /// All resources, keyed by name. Built-ins are included (and overridable).
    pub resources: BTreeMap<String, Resource>,
    /// Present when an `https/` directory with cert + key was found.
    pub tls: Option<TlsPaths>,
    /// Directory scanned for compiled function libraries.
    pub functions_dir: PathBuf,
}

/// Resolved TLS material.
#[derive(Debug, Clone)]
pub struct TlsPaths {
    pub cert: PathBuf,
    pub key: PathBuf,
}

impl App {
    /// Load an app directory. Missing pieces fall back to safe defaults, so the
    /// smallest valid app is an empty directory.
    pub fn load(root: impl AsRef<Path>) -> crate::Result<App> {
        let root = root.as_ref().to_path_buf();
        let config = Config::load(&root)?;

        let mut resources = BTreeMap::new();

        // 1. Seed with the built-ins.
        for (name, src) in crate::defaults::builtins() {
            resources.insert(name.to_string(), crate::defaults::parse_builtin(src));
        }

        // 2. Load user-defined models, overriding built-ins by name.
        let models_dir = root.join("models");
        if models_dir.is_dir() {
            for entry in std::fs::read_dir(&models_dir).map_err(|e| crate::Error::Io {
                path: models_dir.clone(),
                source: e,
            })? {
                let entry = entry.map_err(|e| crate::Error::Io {
                    path: models_dir.clone(),
                    source: e,
                })?;
                let path = entry.path();
                if path.extension().and_then(|e| e.to_str()) != Some("toml") {
                    continue;
                }
                let resource = Resource::load(&path)?;
                tracing::info!(resource = %resource.meta.name, "loaded model");
                resources.insert(resource.meta.name.clone(), resource);
            }
        }

        // 3. Make multitenancy automatic: every org-scoped resource carries an
        //    `organization_id` foreign key. Inject it where the author didn't
        //    declare one, so the column, its FK, and org filtering all just work.
        for resource in resources.values_mut() {
            if resource.is_org_scoped() && !resource.fields.contains_key("organization_id") {
                resource.fields.insert(
                    "organization_id".to_string(),
                    Field {
                        ty: FieldType::Reference,
                        references: Some("organization".to_string()),
                        required: true,
                        unique: false,
                        hidden: false,
                        default: None,
                        max_length: None,
                        on_delete: Some(OnDelete::Cascade),
                        // Injected and stamped by the framework — an operator
                        // should never see, let alone type, a tenant id.
                        admin: FieldAdmin {
                            visible: false,
                            ..FieldAdmin::default()
                        },
                    },
                );
            }
        }

        // 4. TLS is inferred from the presence of an `https/` directory.
        let tls = Self::detect_tls(&root);
        if tls.is_some() {
            tracing::info!("https/ directory found — serving over TLS");
        }

        Ok(App {
            functions_dir: root.join("functions"),
            root,
            config,
            resources,
            tls,
        })
    }

    /// Look for a cert + key under `https/`, tolerating common filenames.
    fn detect_tls(root: &Path) -> Option<TlsPaths> {
        let dir = root.join("https");
        if !dir.is_dir() {
            return None;
        }
        let cert = ["cert.pem", "fullchain.pem", "certificate.pem", "server.crt"]
            .iter()
            .map(|f| dir.join(f))
            .find(|p| p.exists())?;
        let key = ["key.pem", "privkey.pem", "server.key", "private.pem"]
            .iter()
            .map(|f| dir.join(f))
            .find(|p| p.exists())?;
        Some(TlsPaths { cert, key })
    }

    /// What to call this app wherever it is named to a person — the admin
    /// dashboard's header, the API docs, the CLI.
    ///
    /// `[app] name` when the app gives itself one; the directory it lives in
    /// otherwise, which is a filing decision (`07-functions`, `backend`) rather
    /// than a name anybody should have to read. A blank name is not a name: it
    /// would render as a heading with nothing in it, so it falls back too.
    pub fn display_name(&self) -> String {
        self.config
            .app
            .name
            .as_deref()
            .map(str::trim)
            .filter(|name| !name.is_empty())
            .map(str::to_string)
            .unwrap_or_else(|| {
                self.root
                    .file_name()
                    .and_then(|name| name.to_str())
                    .unwrap_or("apiplant app")
                    .to_string()
            })
    }

    /// Title for the API docs: `[docs] title` when set, the app's name
    /// otherwise — so an app that names itself once is named everywhere.
    pub fn docs_title(&self) -> String {
        self.config
            .docs
            .title
            .as_deref()
            .map(str::trim)
            .filter(|title| !title.is_empty())
            .map(str::to_string)
            .unwrap_or_else(|| self.display_name())
    }

    /// Resource names in dependency order (referenced resources first), so a
    /// migrator can create tables without violating foreign keys.
    pub fn resources_in_dependency_order(&self) -> Vec<&Resource> {
        let mut ordered: Vec<&Resource> = Vec::new();
        let mut placed: std::collections::HashSet<&str> = std::collections::HashSet::new();

        // Simple repeated passes; resource graphs are tiny.
        let mut remaining: Vec<&Resource> = self.resources.values().collect();
        while !remaining.is_empty() {
            let mut progressed = false;
            remaining.retain(|r| {
                let deps_ready = r.fields.values().all(|f| match &f.references {
                    Some(target) => placed.contains(target.as_str()) || target == &r.meta.name,
                    None => true,
                });
                if deps_ready {
                    ordered.push(r);
                    placed.insert(r.meta.name.as_str());
                    progressed = true;
                    false
                } else {
                    true
                }
            });
            if !progressed {
                // Cyclic or dangling reference — emit the rest as-is rather than loop forever.
                ordered.append(&mut remaining);
            }
        }
        ordered
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::time::{SystemTime, UNIX_EPOCH};

    fn temp_app_dir(label: &str) -> PathBuf {
        let mut dir = std::env::temp_dir();
        let stamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        dir.push(format!(
            "apiplant-app-{label}-{}-{stamp}",
            std::process::id()
        ));
        fs::create_dir_all(dir.join("models")).unwrap();
        dir
    }

    /// One name, given once: the dashboard header and the API docs both read
    /// it, and an app that never names itself is called after its directory.
    #[test]
    fn display_name_and_docs_title_share_one_source() {
        let dir = temp_app_dir("naming");
        let directory = dir.file_name().unwrap().to_str().unwrap().to_string();

        let unnamed = App::load(&dir).unwrap();
        assert_eq!(unnamed.display_name(), directory);
        assert_eq!(unnamed.docs_title(), directory);

        fs::write(dir.join("main.toml"), "[app]\nname = \"Acme Logistics\"\n").unwrap();
        let named = App::load(&dir).unwrap();
        assert_eq!(named.display_name(), "Acme Logistics");
        assert_eq!(named.docs_title(), "Acme Logistics");

        // A blank name is not a name — it would render as an empty heading.
        fs::write(dir.join("main.toml"), "[app]\nname = \"   \"\n").unwrap();
        assert_eq!(App::load(&dir).unwrap().display_name(), directory);

        // `[docs] title` still wins for the docs alone, for an app whose API is
        // published under a different name than the app answers to.
        fs::write(
            dir.join("main.toml"),
            "[app]\nname = \"Acme Logistics\"\n\n[docs]\ntitle = \"Acme Freight API\"\n",
        )
        .unwrap();
        let split = App::load(&dir).unwrap();
        assert_eq!(split.display_name(), "Acme Logistics");
        assert_eq!(split.docs_title(), "Acme Freight API");

        fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn empty_app_loads_builtins_and_no_tls() {
        let dir = temp_app_dir("empty");
        let app = App::load(&dir).unwrap();

        assert!(app.resources.contains_key("organization"));
        assert!(app.resources.contains_key("membership"));
        assert!(app.resources.contains_key("user"));
        assert!(app.resources.contains_key("api_key"));
        assert!(app.resources.contains_key("oauth_connection"));
        assert!(app.tls.is_none());
        assert_eq!(app.functions_dir, dir.join("functions"));

        fs::remove_dir_all(dir).unwrap();
    }

    #[test]
    fn org_scoped_resources_get_organization_id_injected() {
        let dir = temp_app_dir("org-scope");
        fs::write(
            dir.join("models/post.toml"),
            r#"
[resource]
name = "post"

[fields.title]
type = "string"
required = true
"#,
        )
        .unwrap();
        fs::write(
            dir.join("models/plan.toml"),
            r#"
[resource]
name = "plan"
scope = "global"

[fields.name]
type = "string"
"#,
        )
        .unwrap();

        let app = App::load(&dir).unwrap();
        let post = app.resources.get("post").unwrap();
        let plan = app.resources.get("plan").unwrap();

        let org = post.fields.get("organization_id").unwrap();
        assert_eq!(org.ty, FieldType::Reference);
        assert_eq!(org.references.as_deref(), Some("organization"));
        assert!(org.required);
        assert_eq!(org.on_delete, Some(OnDelete::Cascade));
        assert!(!plan.fields.contains_key("organization_id"));

        fs::remove_dir_all(dir).unwrap();
    }

    #[test]
    fn same_named_model_replaces_builtin_resource() {
        let dir = temp_app_dir("override-user");
        fs::write(
            dir.join("models/users.toml"),
            r#"
[resource]
name = "user"
scope = "global"

[auth]
identity_field = "username"
password_field = "password_hash"

[fields.username]
type = "string"
required = true

[fields.password_hash]
type = "string"
hidden = true
"#,
        )
        .unwrap();

        let app = App::load(&dir).unwrap();
        let user = app.resources.get("user").unwrap();

        assert!(user.fields.contains_key("username"));
        assert!(!user.fields.contains_key("email"));
        assert_eq!(user.auth.as_ref().unwrap().identity_field, "username");

        fs::remove_dir_all(dir).unwrap();
    }

    #[test]
    fn tls_detection_accepts_common_cert_and_key_names() {
        let dir = temp_app_dir("tls");
        fs::create_dir_all(dir.join("https")).unwrap();
        fs::write(dir.join("https/fullchain.pem"), "cert").unwrap();
        fs::write(dir.join("https/privkey.pem"), "key").unwrap();

        let app = App::load(&dir).unwrap();
        let tls = app.tls.unwrap();
        assert_eq!(tls.cert, dir.join("https/fullchain.pem"));
        assert_eq!(tls.key, dir.join("https/privkey.pem"));

        fs::remove_dir_all(dir).unwrap();
    }

    #[test]
    fn dependency_order_places_parents_before_children() {
        let dir = temp_app_dir("deps");
        fs::write(
            dir.join("models/post.toml"),
            r#"
[resource]
name = "post"

[fields.owner_id]
type = "reference"
references = "user"

[fields.title]
type = "string"
"#,
        )
        .unwrap();
        fs::write(
            dir.join("models/comment.toml"),
            r#"
[resource]
name = "comment"

[fields.post_id]
type = "reference"
references = "post"

[fields.owner_id]
type = "reference"
references = "user"

[fields.body]
type = "text"
"#,
        )
        .unwrap();

        let app = App::load(&dir).unwrap();
        let order: Vec<_> = app
            .resources_in_dependency_order()
            .into_iter()
            .map(|r| r.meta.name.as_str())
            .collect();

        let user_idx = order.iter().position(|name| *name == "user").unwrap();
        let post_idx = order.iter().position(|name| *name == "post").unwrap();
        let comment_idx = order.iter().position(|name| *name == "comment").unwrap();

        assert!(user_idx < post_idx);
        assert!(post_idx < comment_idx);

        fs::remove_dir_all(dir).unwrap();
    }
}