autumn-web 0.9.0

An opinionated, convention-over-configuration web framework for Rust
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
//! A `SQLite` fleet behind the sharding extractors, end to end (ADR 0019).
//!
//! The app code is the same code a Postgres-sharded app runs: `ShardedDb`,
//! `Shards` and a `#[repository(tenant_scoped, sharded)]` repository. The
//! shard set is a [`DatabaseFleet`], so each tenant (or slot) gets its own
//! `SQLite` file, opened and migrated on first use.
//!
//! Needs `test-support` for [`TestApp`]. Run explicitly:
//!
//! ```sh
//! cargo test -p autumn-web --features "sqlite,test-support" --test sqlite_fleet
//! ```
#![cfg(all(feature = "sqlite", feature = "test-support"))]

use std::path::Path as FsPath;

use autumn_web::config::{AutumnConfig, DatabaseConfig, DatabaseFleetConfig};
use autumn_web::db::fleet::DatabaseFleet;
use autumn_web::db::{RuntimeConnection, create_pool};
use autumn_web::fleet_layout::{FleetDbKey, FleetMode};
use autumn_web::migrate::{EmbeddedMigrations, embed_migrations};
use autumn_web::prelude::*;
use autumn_web::reexports::{diesel, diesel_async};
use autumn_web::sharding::{CrossShard, ShardedDb, Shards};
use autumn_web::test::TestApp;
use diesel_async::RunQueryDsl as _;
use diesel_async::pooled_connection::deadpool::Pool;

const MIGRATIONS: EmbeddedMigrations = embed_migrations!("tests/fixtures/fleet_migrations");

mod schema {
    autumn_web::reexports::diesel::table! {
        fleet_notes (id) {
            id -> Int8,
            body -> Text,
            tenant_id -> Nullable<Text>,
        }
    }
}

use schema::fleet_notes;

#[autumn_web::model]
pub struct FleetNote {
    #[id]
    pub id: i64,
    pub body: String,
    pub tenant_id: Option<String>,
}

#[autumn_web::repository(FleetNote, tenant_scoped, sharded)]
pub trait FleetNoteRepository {}

#[derive(diesel::QueryableByName)]
struct Body {
    #[diesel(sql_type = diesel::sql_types::Text)]
    body: String,
}

#[derive(diesel::QueryableByName)]
struct Count {
    #[diesel(sql_type = diesel::sql_types::BigInt)]
    n: i64,
}

/// Writes through `ShardedDb`: raw SQL on the tenant's own database.
#[post("/raw/{body}")]
async fn raw_write(mut db: ShardedDb, Path(body): Path<String>) -> AutumnResult<String> {
    diesel::sql_query("INSERT INTO fleet_notes (body, tenant_id) VALUES (?, NULL)")
        .bind::<diesel::sql_types::Text, _>(&body)
        .execute(&mut *db)
        .await?;
    Ok(db.shard().to_owned())
}

#[get("/raw")]
async fn raw_read(mut db: ShardedDb) -> AutumnResult<Json<Vec<String>>> {
    let rows: Vec<Body> = diesel::sql_query("SELECT body FROM fleet_notes ORDER BY id")
        .load(&mut *db)
        .await?;
    Ok(Json(rows.into_iter().map(|r| r.body).collect()))
}

/// Writes through the generated sharded repository.
#[post("/notes/{body}")]
async fn repo_write(
    repo: PgFleetNoteRepository,
    tenant: autumn_web::tenancy::Tenant,
    Path(body): Path<String>,
) -> AutumnResult<String> {
    let note = repo
        .save(&NewFleetNote {
            body,
            tenant_id: Some(tenant.0),
        })
        .await?;
    Ok(note.body)
}

#[get("/notes")]
async fn repo_read(repo: PgFleetNoteRepository) -> AutumnResult<Json<Vec<String>>> {
    let mut notes: Vec<String> = repo.find_all().await?.into_iter().map(|n| n.body).collect();
    notes.sort();
    Ok(Json(notes))
}

/// Admin read across every database on disk.
#[get("/admin/notes")]
async fn admin_notes(
    CrossShard(repo): CrossShard<PgFleetNoteRepository>,
) -> AutumnResult<Json<Vec<String>>> {
    let mut notes: Vec<String> = repo.find_all().await?.into_iter().map(|n| n.body).collect();
    notes.sort();
    Ok(Json(notes))
}

/// `Shards::each_shard` over the fleet: one entry per database file.
#[get("/admin/counts")]
async fn admin_counts(shards: Shards) -> AutumnResult<Json<Vec<(String, i64)>>> {
    let results = shards
        .each_shard(|shard, mut db| {
            let name = shard.name().to_owned();
            async move {
                let count: Count = diesel::sql_query("SELECT COUNT(*) AS n FROM fleet_notes")
                    .get_result(&mut *db)
                    .await?;
                Ok((name, count.n))
            }
        })
        .await;
    let mut out = Vec::new();
    for (_, result) in results {
        out.push(result?);
    }
    Ok(Json(out))
}

/// Provisioning a tenant from a handler.
#[post("/admin/tenants/{id}")]
async fn provision(shards: Shards, Path(id): Path<String>) -> AutumnResult<String> {
    let fleet = shards.fleet().expect("fleet-backed");
    let key = fleet.key_for(&id)?;
    let db = fleet.provision(&key).await?;
    Ok(db.key().name())
}

#[get("/admin/db/{name}")]
async fn db_by_name(shards: Shards, Path(name): Path<String>) -> AutumnResult<String> {
    let mut db = shards.db_on(&name).await?;
    let count: Count = diesel::sql_query("SELECT COUNT(*) AS n FROM fleet_notes")
        .get_result(&mut *db)
        .await?;
    Ok(count.n.to_string())
}

fn control_pool(dir: &FsPath) -> Pool<RuntimeConnection> {
    create_pool(&DatabaseConfig {
        url: Some(format!("sqlite://{}", dir.join("control.db").display())),
        ..Default::default()
    })
    .expect("control pool builds")
    .expect("url configured")
}

fn fleet(dir: &FsPath, mode: FleetMode, create_on_demand: bool) -> DatabaseFleet {
    DatabaseFleet::builder(DatabaseFleetConfig {
        mode,
        root: dir.join("fleet").display().to_string(),
        path: None,
        max_open: 4,
        pool_size: 2,
        create_on_demand: Some(create_on_demand),
        idle_close_secs: 0,
        restore_missing: false,
        ownership: None,
    })
    .migrations("app", MIGRATIONS)
    .migrations(
        "version-history",
        autumn_web::version_history::VERSION_HISTORY_MIGRATIONS,
    )
    .build()
    .expect("fleet builds")
}

fn header_tenancy() -> AutumnConfig {
    let mut config = AutumnConfig::default();
    config.profile = Some("test".into());
    config.security.csrf.enabled = false;
    config.tenancy.enabled = true;
    config.tenancy.source = "header".to_owned();
    config.tenancy.header_name = "x-tenant-id".to_owned();
    // Admin routes span tenants, so they run without one.
    config.tenancy.public_paths = vec!["/admin".to_owned()];
    config
}

fn app(dir: &FsPath, fleet: DatabaseFleet) -> autumn_web::test::TestClient {
    TestApp::new()
        .config(header_tenancy())
        .routes(routes![
            raw_write,
            raw_read,
            repo_write,
            repo_read,
            admin_notes,
            admin_counts,
            provision,
            db_by_name
        ])
        .with_db(control_pool(dir))
        .with_fleet(fleet)
        .build()
}

#[tokio::test]
async fn each_tenant_writes_to_its_own_database_file() {
    let tmp = tempfile::tempdir().unwrap();
    let fleet = fleet(tmp.path(), FleetMode::Tenant, true);
    let client = app(tmp.path(), fleet.clone());

    for (tenant, body) in [("acme", "a1"), ("acme", "a2"), ("globex", "g1")] {
        client
            .post(&format!("/raw/{body}"))
            .header("x-tenant-id", tenant)
            .send()
            .await
            .assert_status(200)
            .assert_body_eq(&format!("tenant:{tenant}"));
    }
    let acme: Vec<String> = client
        .get("/raw")
        .header("x-tenant-id", "acme")
        .send()
        .await
        .json();
    assert_eq!(acme, vec!["a1", "a2"]);
    let globex: Vec<String> = client
        .get("/raw")
        .header("x-tenant-id", "globex")
        .send()
        .await
        .json();
    assert_eq!(
        globex,
        vec!["g1"],
        "no tenant_id filter, yet no leak: separate files"
    );

    let keys = fleet.list().await.unwrap();
    assert_eq!(
        keys.iter().map(FleetDbKey::name).collect::<Vec<_>>(),
        vec!["tenant:acme", "tenant:globex"]
    );
    for key in &keys {
        assert!(fleet.path_of(key).is_file());
        assert!(
            fleet
                .path_of(key)
                .starts_with(tmp.path().join("fleet").canonicalize().unwrap())
        );
    }
    // The control database holds no tenant rows.
    assert!(!tmp.path().join("fleet").join("control.db").exists());
}

#[tokio::test]
async fn the_generated_sharded_repository_routes_to_the_tenant_database() {
    let tmp = tempfile::tempdir().unwrap();
    let fleet = fleet(tmp.path(), FleetMode::Tenant, true);
    let client = app(tmp.path(), fleet);

    for (tenant, body) in [("acme", "plan"), ("globex", "launch"), ("acme", "ship")] {
        client
            .post(&format!("/notes/{body}"))
            .header("x-tenant-id", tenant)
            .send()
            .await
            .assert_status(200);
    }
    let acme: Vec<String> = client
        .get("/notes")
        .header("x-tenant-id", "acme")
        .send()
        .await
        .json();
    assert_eq!(acme, vec!["plan", "ship"]);

    // CrossShard fans out over every database on disk.
    let all: Vec<String> = client.get("/admin/notes").send().await.json();
    assert_eq!(all, vec!["launch", "plan", "ship"]);

    let counts: Vec<(String, i64)> = client.get("/admin/counts").send().await.json();
    assert_eq!(
        counts,
        vec![
            ("tenant:acme".to_owned(), 2),
            ("tenant:globex".to_owned(), 1)
        ]
    );
    client
        .get("/admin/db/tenant:globex")
        .send()
        .await
        .assert_status(200)
        .assert_body_eq("1");
}

#[tokio::test]
async fn tenant_ids_that_cannot_name_a_file_are_rejected_with_400() {
    let tmp = tempfile::tempdir().unwrap();
    let client = app(tmp.path(), fleet(tmp.path(), FleetMode::Tenant, true));
    for bad in ["..", "Acme", "a.b", "con"] {
        client
            .get("/raw")
            .header("x-tenant-id", bad)
            .send()
            .await
            .assert_status(400);
    }
    assert!(
        std::fs::read_dir(tmp.path().join("fleet"))
            .unwrap()
            .next()
            .is_none(),
        "nothing was created for a refused id"
    );
}

#[tokio::test]
async fn an_unprovisioned_tenant_gets_404_until_provisioned() {
    let tmp = tempfile::tempdir().unwrap();
    let client = app(tmp.path(), fleet(tmp.path(), FleetMode::Tenant, false));
    client
        .get("/raw")
        .header("x-tenant-id", "initech")
        .send()
        .await
        .assert_status(404);
    client
        .post("/admin/tenants/initech")
        .send()
        .await
        .assert_status(200)
        .assert_body_eq("tenant:initech");
    client
        .post("/admin/tenants/initech")
        .send()
        .await
        .assert_status(409);
    client
        .get("/raw")
        .header("x-tenant-id", "initech")
        .send()
        .await
        .assert_status(200)
        .assert_body_eq("[]");
    client
        .get("/admin/db/tenant:nobody")
        .send()
        .await
        .assert_status(404);
}

#[tokio::test]
async fn a_slot_fleet_shares_one_file_per_slot_and_isolates_by_tenant_id() {
    let tmp = tempfile::tempdir().unwrap();
    let fleet = fleet(tmp.path(), FleetMode::Slot, true);
    let client = app(tmp.path(), fleet.clone());

    for (tenant, body) in [("acme", "a"), ("globex", "g")] {
        let shard = client
            .post(&format!("/notes/{body}"))
            .header("x-tenant-id", tenant)
            .send()
            .await
            .assert_status(200)
            .text();
        assert_eq!(shard, body);
    }
    // tenant_scoped keeps tenants apart even when they share a slot file.
    let acme: Vec<String> = client
        .get("/notes")
        .header("x-tenant-id", "acme")
        .send()
        .await
        .json();
    assert_eq!(acme, vec!["a"]);

    let slot = |t: &str| autumn_web::sharding::slot_for_key(t.into()).0;
    let mut expected: Vec<FleetDbKey> = ["acme", "globex"]
        .iter()
        .map(|t| FleetDbKey::slot(slot(t)).unwrap())
        .collect();
    expected.sort();
    expected.dedup();
    assert_eq!(fleet.list().await.unwrap(), expected);
    let name = client
        .post("/raw/x")
        .header("x-tenant-id", "acme")
        .send()
        .await
        .text();
    assert_eq!(name, format!("slot:{:05}", slot("acme")));
}

#[tokio::test]
async fn the_fleet_health_indicator_reports_the_counters() {
    let tmp = tempfile::tempdir().unwrap();
    let mut config = header_tenancy();
    config.health.detailed = true;
    let client = TestApp::new()
        .config(config)
        .routes(routes![raw_write])
        .with_db(control_pool(tmp.path()))
        .with_fleet(fleet(tmp.path(), FleetMode::Tenant, true))
        .build();
    client
        .post("/raw/x")
        .header("x-tenant-id", "acme")
        .send()
        .await
        .assert_status(200);
    let health: serde_json::Value = client.get("/actuator/health").send().await.json();
    let fleet = &health["components"]["db:fleet"];
    assert_eq!(fleet["status"], "UP", "{health}");
    assert_eq!(fleet["details"]["mode"], "tenant");
    assert_eq!(fleet["details"]["open"], 1);
    assert_eq!(fleet["details"]["created_total"], 1);
}