rltbl 0.1.0

Relatable (rltbl) is a tool for cleaning and connecting your data.
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
//! # rltbl/relatable
//!
//! This is [relatable](crate) (rltbl::[web](crate::web)).

use crate::{self as rltbl, core::ResultSet};
use rltbl::{
    cli::Cli,
    core::{ChangeSet, Cursor, Relatable, RelatableError},
    select::{joined_query, Format, QueryParams, Select},
    sql::{CachingStrategy, JsonRow, SqlParam},
    table::{Row, Table},
};
use std::io::Write;

use anyhow::Result;
use async_std::sync::Arc;
use axum::{
    body::Body,
    extract::{Json as ExtractJson, Path, Query, State},
    http::header,
    http::{HeaderMap, Response, StatusCode},
    response::{Html, IntoResponse, Json, Redirect},
    routing::{get, post},
    Form, Router,
};
use axum_session::{Session, SessionConfig, SessionLayer, SessionNullPool, SessionStore};
use indexmap::IndexMap;
use minijinja::context;
use serde_json::{json, to_string_pretty, to_value, Value as JsonValue};
use tokio::net::TcpListener;
use tower_service::Service;

fn forbid() -> Response<Body> {
    (StatusCode::FORBIDDEN, Html(format!("403 Forbidden"))).into_response()
}

fn get_404(error: &anyhow::Error) -> Response<Body> {
    tracing::error!("404 {error:?}");
    (
        StatusCode::NOT_FOUND,
        Html(format!("404 Not Found: {error}")),
    )
        .into_response()
}

fn get_500(error: &anyhow::Error) -> Response<Body> {
    tracing::error!("500 {error:?}");
    (
        StatusCode::INTERNAL_SERVER_ERROR,
        Html(format!("500 Internal Server Error: {error}")),
    )
        .into_response()
}

async fn get_root(State(rltbl): State<Arc<Relatable>>) -> impl IntoResponse {
    tracing::info!("request root");
    let default = "table";
    let table = rltbl
        .connection
        .query_value(
            r#"SELECT "table" FROM "table" ORDER BY _order LIMIT 1"#,
            None,
        )
        .await
        .unwrap_or(Some(json!(default)))
        .unwrap_or(json!(default));
    let table = table.as_str().unwrap_or(default);
    Redirect::permanent(format!("{}/table/{table}", rltbl.root).as_str())
}

async fn main_js() -> impl IntoResponse {
    let mut headers = HeaderMap::new();
    headers.insert(header::CONTENT_TYPE, "text/javascript".parse().unwrap());
    (headers, include_str!("resources/main.js"))
}

async fn main_css() -> impl IntoResponse {
    let mut headers = HeaderMap::new();
    headers.insert(header::CONTENT_TYPE, "text/css".parse().unwrap());
    (headers, include_str!("resources/main.css"))
}

async fn respond(rltbl: &Relatable, format: &Format, content: &JsonValue) -> Response<Body> {
    let response = match format {
        Format::Html | Format::Default => match rltbl.render("table.html", content) {
            Ok(html) => Html(html).into_response(),
            Err(error) => {
                tracing::error!("{error:?}");
                return get_500(&error);
            }
        },
        Format::PrettyJson => {
            let mut headers = HeaderMap::new();
            headers.insert(header::CONTENT_TYPE, "application/json".parse().unwrap());
            (headers, to_string_pretty(content).unwrap_or_default()).into_response()
        }
        Format::Json | Format::ValueJson => Json(content).into_response(),
        Format::Csv => get_500(
            &RelatableError::FormatError(
                "CSV format should be handled before `respond()`".to_string(),
            )
            .into(),
        ),
        Format::Tsv => get_500(
            &RelatableError::FormatError(
                "TSV format should be handled before `respond()`".to_string(),
            )
            .into(),
        ),
    };
    response
}

fn respond_csv(result: ResultSet) -> Response<Body> {
    let mut headers = HeaderMap::new();
    headers.insert(header::CONTENT_TYPE, "text/csv".parse().unwrap());
    (headers, result.to_csv()).into_response()
}

fn respond_tsv(result: ResultSet) -> Response<Body> {
    let mut headers = HeaderMap::new();
    headers.insert(
        header::CONTENT_TYPE,
        "text/tab-separated-values".parse().unwrap(),
    );
    (headers, result.to_tsv()).into_response()
}

fn get_username(session: Session<SessionNullPool>) -> String {
    let username = std::env::var("RLTBL_USER").unwrap_or_default();
    if username != "" {
        return username;
    }
    session.get("username").unwrap_or_default()
}

async fn get_table(
    State(rltbl): State<Arc<Relatable>>,
    Path(path): Path<String>,
    Query(query_params): Query<QueryParams>,
    session: Session<SessionNullPool>,
) -> Response<Body> {
    // tracing::info!("get_table({rltbl:?}, {path}, {query_params:?})");

    let username = get_username(session);
    if username.trim() != "" {
        init_user(&rltbl, &username).await;
    }
    let select = Select::from_path_and_query(&path, &query_params, &rltbl).await;
    let format = match Format::try_from(&path) {
        Ok(format) => format,
        Err(error) => return get_404(&error),
    };
    let result = match rltbl.fetch(&select).await {
        Ok(result) => result,
        Err(error) => return get_500(&error),
    };
    match format {
        Format::Csv => return respond_csv(result),
        Format::Tsv => return respond_tsv(result),
        _ => (),
    }
    let site = rltbl.get_site(&username).await;
    let content = json!({
        "site": site,
        "page": select.to_page(&rltbl.root, "table", &vec![]).unwrap_or_default(),
        "result": result
    });
    respond(&rltbl, &format, &content).await
}

async fn post_table(
    State(rltbl): State<Arc<Relatable>>,
    Path(path): Path<String>,
    _session: Session<SessionNullPool>,
    ExtractJson(changeset): ExtractJson<ChangeSet>,
) -> Response<Body> {
    tracing::info!("post_table([rltbl], {path}, {changeset:?})");
    if rltbl.readonly {
        return forbid().into();
    }

    let table = changeset.table.clone();
    if path != table {
        return get_500(
            &RelatableError::InputError(format!(
                "Changeset table '{table}' does not match URL path {path}"
            ))
            .into(),
        );
    }

    // WARN: We need to check that the user matches!
    // let user = changeset.user.clone();
    // let username = get_username(session);
    // if username != user {
    //     return get_500(
    //         &RelatableError::InputError(format!(
    //             "Changeset user '{user}' does not match session username {username}"
    //         ))
    //         .into(),
    //     );
    // }

    match rltbl.set_values(&changeset).await {
        Ok(_) => "POST successful".into_response(),
        Err(error) => get_500(&error),
    }
}

async fn get_tableset(
    State(rltbl): State<Arc<Relatable>>,
    Path((tableset_name, path)): Path<(String, String)>,
    Query(query_params): Query<QueryParams>,
    session: Session<SessionNullPool>,
) -> Response<Body> {
    tracing::trace!("get_tableset({rltbl:?}, {tableset_name}, {path}, {query_params:?})");
    let format = match Format::try_from(&path) {
        Ok(format) => format,
        Err(error) => return get_404(&error),
    };

    let select = Select::from_path_and_query(&path, &query_params, &rltbl).await;
    // tracing::info!("SELECT {select:?}",);

    if matches!(format, Format::ValueJson) {
        let sel = match joined_query(&rltbl, &tableset_name, &select).await {
            Ok(select) => select,
            Err(error) => match error.downcast_ref() {
                Some(RelatableError::ConfigError(e)) => match e.as_str() {
                    "empty tableset" => return Json(json!(0)).into_response(),
                    _ => return get_500(&error),
                },
                _ => {
                    return get_500(&error);
                }
            },
        };
        let value = match rltbl.count(&sel).await {
            Ok(count) => count,
            Err(error) => return get_500(&error),
        };
        return Json(value).into_response();
    }

    let mut result = match joined_query(&rltbl, &tableset_name, &select).await {
        Ok(sel) => match rltbl.fetch(&sel).await {
            Ok(result) => result,
            Err(error) => return get_500(&error),
        },
        Err(error) => match error.downcast_ref() {
            Some(RelatableError::ConfigError(e)) => match e.as_str() {
                "empty tableset" => {
                    let mut table = Table::get_table(select.table_name.as_str(), &rltbl)
                        .await
                        .unwrap();
                    table.ensure_default_view_created(&rltbl).await.unwrap();
                    let (columns, _) = Table::collect_column_info(&table.name, &rltbl)
                        .await
                        .unwrap();
                    ResultSet {
                        select: select.clone(),
                        table,
                        columns,
                        ..Default::default()
                    }
                }
                _ => return get_500(&error),
            },
            _ => {
                return get_500(&error);
            }
        },
    };
    result.select = select.clone();
    match format {
        Format::Csv => return respond_csv(result),
        Format::Tsv => return respond_tsv(result),
        _ => (),
    }

    let username = get_username(session);
    if username.trim() != "" {
        init_user(&rltbl, &username).await;
    }
    // tracing::info!("USERNAME {username}");

    let site = rltbl.get_site(&username).await;

    let sql = format!(r#"SELECT * FROM "tableset" WHERE tableset = '{tableset_name}'"#);
    let json_rows = match rltbl.connection.query(&sql, None).await {
        Ok(rows) => rows,
        Err(error) => return get_500(&error),
    };

    // tracing::info!("TAB {json_rows:?}");
    let mut tabset = vec![];
    for json_row in json_rows {
        let table = json_row.get_string("right_table").unwrap();
        if tabset.contains(&table) {
            continue;
        }
        tabset.push(table.clone());
    }

    let content = json!({
       "site": site,
       "page": select.to_page(&rltbl.root, &format!("tableset/{tableset_name}"), &tabset).unwrap_or_default(),
       "result": result
    });
    respond(&rltbl, &format, &content).await
}

async fn init_user(rltbl: &Relatable, username: &str) -> () {
    let color = random_color::RandomColor::new().to_hex();
    let db_kind = rltbl.connection.kind();
    let statement = format!(
        r#"SELECT 1 FROM "user" WHERE "name" = {sql_param}"#,
        sql_param = SqlParam::new(&db_kind).next()
    );
    let params = json!([username]);
    if let None = rltbl
        .connection
        .query_value(&statement, Some(&params))
        .await
        .expect("Error getting user")
    {
        let statement = format!(
            r#"INSERT INTO user("name", "color") VALUES ({sql_params})"#,
            sql_params = SqlParam::new(&db_kind).get_as_list(2)
        );
        let params = json!([username, color]);
        rltbl
            .connection
            .query(&statement, Some(&params))
            .await
            .expect("Update user");
    }
}

async fn post_sign_in(
    State(rltbl): State<Arc<Relatable>>,
    session: Session<SessionNullPool>,
    Form(form): Form<IndexMap<String, String>>,
) -> Response<Body> {
    tracing::info!("post_login({form:?})");
    let username = String::new();
    let username = form.get("username").unwrap_or(&username);
    session.set("username", username);
    init_user(&rltbl, &username).await;

    match form.get("redirect") {
        Some(url) => Redirect::to(url).into_response(),
        None => Html(format!(
            r#"<p>Logged in as {username}</p>
            <form method="post">
            <input name="username" value="{username}"/>
            <input type="submit"/>
            </form>"#
        ))
        .into_response(),
    }
}

async fn post_sign_out(session: Session<SessionNullPool>) -> Response<Body> {
    tracing::debug!("post_logout()");
    session.set("username", "");

    Html(format!(
        r#"<p>Logged out</p>
        <form method="post">
        <input name="username" value=""/>
        <input type="submit"/>
        </form>"#
    ))
    .into_response()
}

async fn post_cursor(
    State(rltbl): State<Arc<Relatable>>,
    session: Session<SessionNullPool>,
    ExtractJson(cursor): ExtractJson<Cursor>,
) -> Response<Body> {
    // tracing::info!("post_cursor({cursor:?})");
    let username = get_username(session);
    tracing::debug!("post_cursor({cursor:?}, {username})");
    // TODO: sanitize the cursor JSON.
    let mut sql_param = SqlParam::new(&rltbl.connection.kind());
    let statement = format!(
        r#"UPDATE user
           SET "cursor" = {sql_param_1},
               "datetime" = CURRENT_TIMESTAMP
           WHERE "name" = {sql_param_2}"#,
        sql_param_1 = sql_param.next(),
        sql_param_2 = sql_param.next(),
    );
    let cursor = to_value(cursor).unwrap_or_default();
    let params = json!([cursor, username]);
    match rltbl.connection.query(&statement, Some(&params)).await {
        Ok(_) => "Cursor updated".into_response(),
        Err(_) => "Cursor update failed".into_response(),
    }
}

async fn get_row_menu(
    State(rltbl): State<Arc<Relatable>>,
    session: Session<SessionNullPool>,
    Path((table_name, row_id)): Path<(String, u64)>,
) -> Response<Body> {
    tracing::info!("get_row_menu({table_name}, {row_id})");
    let username = get_username(session);
    let site = rltbl.get_site(&username).await;
    let table = match Table::get_table(&table_name, &rltbl).await {
        Ok(table) => table,
        Err(error) => return get_404(&error),
    };
    let row: Row = match rltbl
        .connection
        .query_one(
            &format!(
                r#"SELECT * FROM "{}" WHERE _id = {sql_param}"#,
                table.view,
                sql_param = SqlParam::new(&rltbl.connection.kind()).next()
            ),
            Some(&json!([row_id])),
        )
        .await
    {
        Ok(row) => match row {
            Some(row) => row.into(),
            None => {
                return get_404(
                    &RelatableError::MissingError(format!(
                        "No row in '{table_name}' with id {row_id}"
                    ))
                    .into(),
                )
            }
        },
        Err(error) => return get_500(&error),
    };
    match rltbl.render("row_menu.html", context! {site, table, row}) {
        Ok(html) => Html(html).into_response(),
        Err(error) => return get_500(&error),
    }
}

async fn get_column_menu(
    State(rltbl): State<Arc<Relatable>>,
    session: Session<SessionNullPool>,
    Path((table_name, column)): Path<(String, String)>,
    Query(query_params): Query<QueryParams>,
) -> Response<Body> {
    tracing::info!("get_column_menu({table_name}, {column})");
    let username = get_username(session);
    let select = Select::from_path_and_query(&table_name, &query_params, &rltbl).await;
    let mut operator = String::new();
    let mut value = json!("");
    let mut order = String::new();
    for filter in select.filters {
        let (_t, c, o, v) = filter.parts();
        tracing::warn!("FILTER {filter:?} {o}");
        if c == column {
            operator = o;
            value = v;
        }
    }
    for (c, o) in select.order_by {
        if c == column {
            order = format!("{o:?}");
        }
    }
    let site = rltbl.get_site(&username).await;
    match rltbl.render(
        "column_menu.html",
        context! {site, table_name, column, operator, value, order},
    ) {
        Ok(html) => Html(html).into_response(),
        Err(error) => {
            tracing::error!("{error:?}");
            return get_500(&error);
        }
    }
}
async fn get_cell_menu(
    State(rltbl): State<Arc<Relatable>>,
    session: Session<SessionNullPool>,
    Path((table_name, row_id, column)): Path<(String, u64, String)>,
) -> Response<Body> {
    tracing::info!("get_cell_menu({table_name}, {row_id}, {column})");
    let username = get_username(session);
    let site = rltbl.get_site(&username).await;
    let table = match Table::get_table(&table_name, &rltbl).await {
        Ok(table) => table,
        Err(error) => return get_404(&error),
    };
    let row: Row = match rltbl
        .connection
        .query_one(
            &format!(
                r#"SELECT * FROM "{}" WHERE _id = {sql_param}"#,
                table.view,
                sql_param = SqlParam::new(&rltbl.connection.kind()).next()
            ),
            Some(&json!([row_id])),
        )
        .await
    {
        Ok(row) => match row {
            Some(row) => row.into(),
            None => {
                return get_404(
                    &RelatableError::MissingError(format!(
                        "No row in '{table_name}' with id {row_id}"
                    ))
                    .into(),
                )
            }
        },
        Err(error) => return get_500(&error),
    };
    let cell = row.cells.get(&column);
    match rltbl.render("cell_menu.html", context! {site, table, row, column, cell}) {
        Ok(html) => Html(html).into_response(),
        Err(error) => {
            tracing::error!("{error:?}");
            return get_500(&error);
        }
    }
}

async fn get_cell_options(
    State(rltbl): State<Arc<Relatable>>,
    Path((table, row_id, column)): Path<(String, u64, String)>,
    Query(query_params): Query<QueryParams>,
) -> Response<Body> {
    tracing::info!("get_cell_option({table}, {row_id}, {column}, {query_params:?})");
    let input = match query_params.get("input") {
        Some(input) => input,
        None => &String::new(),
    };
    let statement = format!(
        r#"SELECT DISTINCT "{column}" AS "value" FROM "{table}"
           WHERE "{column}" LIKE '%{input}%' AND "{column}" != ''
           LIMIT 20"#
    );
    let values: Vec<JsonValue> = rltbl
        .connection
        .query(&statement, None)
        .await
        .expect("Get column values")
        .iter()
        .map(|row| {
            let value = row.get_string("value").expect("No 'value' in row");
            json!({
                    "value": value,
                    "label": value,
            })
        })
        .collect();
    Json(json!(values)).into_response()
}

async fn previous_row_id(rltbl: &Relatable, table: &str, row_id: &u64) -> u64 {
    let sql = format!(
        r#"SELECT "_id", MAX("_order") FROM "{table}"
        WHERE "_order" < (SELECT "_order" FROM "{table}" WHERE _id = {sql_param})"#,
        sql_param = SqlParam::new(&rltbl.connection.kind()).next()
    );
    let after_id = rltbl
        .connection
        .query_value(&sql, Some(&json!([row_id])))
        .await;
    after_id
        .unwrap_or(Some(json!(0)))
        .unwrap_or(json!(0))
        .as_u64()
        .unwrap_or_default() as u64
}

async fn add_row_before(
    State(rltbl): State<Arc<Relatable>>,
    session: Session<SessionNullPool>,
    Path((table, row_id)): Path<(String, u64)>,
) -> Response<Body> {
    tracing::info!("add_row_before({table}, {row_id})");
    let username = get_username(session);
    let after_id = previous_row_id(&rltbl, &table, &row_id).await;
    return add_row(&rltbl, &username, &table, Some(after_id)).await;
}

async fn add_row_after(
    State(rltbl): State<Arc<Relatable>>,
    session: Session<SessionNullPool>,
    Path((table, row_id)): Path<(String, u64)>,
) -> Response<Body> {
    tracing::info!("add_row_after({table}, {row_id})");
    let username = get_username(session);
    return add_row(&rltbl, &username, &table, Some(row_id)).await;
}

async fn add_row_end(
    State(rltbl): State<Arc<Relatable>>,
    session: Session<SessionNullPool>,
    Path(table): Path<String>,
) -> Response<Body> {
    tracing::info!("add_row_end({table})");
    let username = get_username(session);
    return add_row(&rltbl, &username, &table, None).await;
}

async fn add_row(
    rltbl: &Relatable,
    username: &str,
    table: &str,
    after_id: Option<u64>,
) -> Response<Body> {
    if rltbl.readonly {
        return forbid().into();
    }
    let columns = match rltbl.fetch_columns(&table).await {
        Ok(columns) => columns,
        Err(error) => return get_500(&error),
    };
    let json_row: JsonRow = JsonRow {
        content: columns
            .iter()
            .map(|c| (c.name.clone(), json!(String::new())))
            .collect(),
    };
    match rltbl.add_row(&table, &username, after_id, &json_row).await {
        Ok(row) => {
            // tracing::info!("Added row {row:?}");
            let offset = rltbl
                .connection
                .query_value(
                    &format!(
                        r#"SELECT COUNT() FROM "{table}" WHERE _order <= {sql_param}"#,
                        sql_param = SqlParam::new(&rltbl.connection.kind()).next()
                    ),
                    Some(&json!([row.order])),
                )
                .await;
            let offset: u64 = offset
                .unwrap_or(Some(json!(0)))
                .unwrap_or(json!(0))
                .as_u64()
                .unwrap_or_default();
            let url = format!("{}/table/{table}?offset={offset}", rltbl.root);
            return Redirect::temporary(url.as_str()).into_response();
        }
        Err(error) => return get_500(&error),
    }
}

async fn delete_row(
    State(rltbl): State<Arc<Relatable>>,
    session: Session<SessionNullPool>,
    Path((table, row_id)): Path<(String, u64)>,
) -> Response<Body> {
    tracing::info!("add_row_after({table}, {row_id})");
    if rltbl.readonly {
        return forbid().into();
    }

    let username = get_username(session);
    let prev = previous_row_id(&rltbl, &table, &row_id).await;
    match rltbl.delete_row(&table, &username, row_id).await {
        Ok(_) => {
            let offset = rltbl
                .connection
                .query_value(
                    &format!(
                        r#"SELECT COUNT() FROM "{table}"
                           WHERE _order <= (SELECT _order FROM "{table}" WHERE _id = {sql_param})"#,
                        sql_param = SqlParam::new(&rltbl.connection.kind()).next()
                    ),
                    Some(&json!([prev])),
                )
                .await;
            let offset: u64 = offset
                .unwrap_or(Some(json!(0)))
                .unwrap_or(json!(0))
                .as_u64()
                .unwrap_or_default();
            let url = format!("{}/table/{table}?offset={offset}", rltbl.root);
            Redirect::temporary(url.as_str()).into_response()
        }
        Err(error) => return get_500(&error),
    }
}

pub async fn build_app(shared_state: Arc<Relatable>) -> Router {
    let session_config = SessionConfig::default();
    let session_store = SessionStore::<SessionNullPool>::new(None, session_config)
        .await
        .unwrap();
    Router::new()
        .route("/", get(get_root))
        .route("/static/main.js", get(main_js))
        .route("/static/main.css", get(main_css))
        .route("/sign-in", post(post_sign_in))
        .route("/sign-out", post(post_sign_out))
        .route("/cursor", post(post_cursor))
        .route("/table/{*path}", get(get_table).post(post_table))
        .route("/tableset/{tableset_name}/{*path}", get(get_tableset))
        .route("/row-menu/{table_name}/{row_id}", get(get_row_menu))
        .route("/column-menu/{table_name}/{column}", get(get_column_menu))
        .route(
            "/cell-menu/{table_name}/{row_id}/{column}",
            get(get_cell_menu),
        )
        .route(
            "/cell-options/{table}/{row_id}/{column}",
            get(get_cell_options),
        )
        .route("/add-row/{table}", get(add_row_end))
        .route("/add-row-before/{table}/{row_id}", get(add_row_before))
        .route("/add-row-after/{table}/{row_id}", get(add_row_after))
        .route("/delete-row/{table}/{row_id}", get(delete_row))
        .layer(SessionLayer::new(session_store))
        .with_state(shared_state)
}

#[tokio::main]
pub async fn app(rltbl: Relatable, host: &str, port: &u16, timeout: &usize) -> Result<String> {
    let shared_state = Arc::new(rltbl);

    let app = build_app(shared_state).await;

    // Create a `TcpListener` using tokio.
    let addr = format!("{host}:{port}");
    let listener = TcpListener::bind(&addr).await.expect("valid TCP address");
    println!(
        "Running Relatable server at http://{}",
        listener.local_addr()?
    );

    // Run the server with graceful shutdown
    if *timeout == 0 {
        println!("Press Control-C to quit.");
        axum::serve(listener, app)
            .with_graceful_shutdown(shutdown_on_signal())
            .await
            .unwrap();
    } else {
        println!("Running server for {timeout}s.");
        axum::serve(listener, app)
            .with_graceful_shutdown(shutdown_on_timeout(*timeout))
            .await
            .unwrap();
    }

    Ok("Stopping Relatable server...".into())
}

pub async fn serve(cli: &Cli, host: &str, port: &u16, timeout: &usize) -> Result<()> {
    tracing::debug!("serve({host}, {port})");
    let rltbl = Relatable::connect(None, &cli.caching).await?;
    app(rltbl, host, port, timeout)?;
    Ok(())
}

async fn shutdown_on_timeout(timeout: usize) {
    std::thread::sleep(std::time::Duration::from_secs(timeout as u64));

    #[cfg(unix)]
    let terminate = async {
        tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
            .expect("failed to install signal handler")
            .recv()
            .await;
    };

    #[cfg(not(unix))]
    let terminate = std::future::pending::<()>();

    let _ = terminate;
}

// From https://github.com/tokio-rs/axum/blob/main/examples/graceful-shutdown/src/main.rs
async fn shutdown_on_signal() {
    let ctrl_c = async {
        tokio::signal::ctrl_c()
            .await
            .expect("failed to install Ctrl+C handler");
    };

    #[cfg(unix)]
    let terminate = async {
        tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
            .expect("failed to install signal handler")
            .recv()
            .await;
    };

    #[cfg(not(unix))]
    let terminate = std::future::pending::<()>();

    tokio::select! {
        _ = ctrl_c => {},
        _ = terminate => {},
    }
}

// Read CGI variables from the environment,
// and read STDIN in the case of POST,
// then handle the request,
// and send the HTTP response to STDOUT.
pub async fn serve_cgi() {
    let request_method = std::env::var("REQUEST_METHOD").unwrap_or("GET".to_string());
    let path_info = std::env::var("PATH_INFO").unwrap_or("/".to_string());
    let query_string = std::env::var("QUERY_STRING").unwrap_or_default();
    let query_string = query_string.trim();
    let uri = if query_string == "" {
        path_info
    } else {
        format!("{path_info}?{query_string}")
    };
    let body = if request_method == "POST" {
        std::io::stdin()
            .lines()
            .map(|l| l.unwrap())
            .collect::<Vec<_>>()
            .join("\n")
    } else {
        String::new()
    };

    let request = axum::http::Request::builder()
        .method(request_method.as_str())
        .uri(uri)
        .header("Accept", std::env::var("HTTP_ACCEPT").unwrap_or_default())
        .header(
            "Content-Type",
            std::env::var("CONTENT_TYPE").unwrap_or_default(),
        )
        .header(
            "Content-Length",
            std::env::var("CONTENT_LENGTH").unwrap_or_default(),
        )
        .body(body)
        .unwrap();
    tracing::debug!("REQUEST {request:?}");

    let rltbl = Relatable::connect(None, &CachingStrategy::Trigger)
        .await
        .expect("Database connection");
    let shared_state = Arc::new(rltbl);
    let mut router = build_app(shared_state).await;
    let response = router.call(request).await;
    tracing::debug!("RESPONSE {response:?}");

    let result = serialize_response(response.unwrap()).await;
    std::io::stdout()
        .write_all(&result)
        .expect("Write to STDOUT");
}

// From https://github.com/amandasaurus/rust-cgi/blob/main/src/lib.rs
// Turn a Response into an HTTP response as bytes.
async fn serialize_response(response: Response<Body>) -> Vec<u8> {
    let mut output = String::new();
    output.push_str("Status: ");
    output.push_str(response.status().as_str());
    if let Some(reason) = response.status().canonical_reason() {
        output.push_str(" ");
        output.push_str(reason);
    }
    output.push_str("\n");

    {
        let headers = response.headers();
        let mut keys: Vec<&http::header::HeaderName> = headers.keys().collect();
        keys.sort_by_key(|h| h.as_str());
        for key in keys {
            output.push_str(key.as_str());
            output.push_str(": ");
            output.push_str(headers.get(key).unwrap().to_str().unwrap());
            output.push_str("\n");
        }
    }

    output.push_str("\n");

    let mut output = output.into_bytes();

    let (_, body) = response.into_parts();
    let bytes = axum::body::to_bytes(body, usize::MAX)
        .await
        .expect("Read from response body");
    output.append(&mut bytes.to_vec());

    output
}