ft_sdk/
lib.rs

1//! This crate can only be used when compiling to wasm, and wasm is run by
2//! [www.fifthtry.com](https://www.fifthtry.com), or by [`fastn`](https://fastn.com), the command
3//! line tool to use help developers build FifthTry Apps or when self-hosting FifthTry Apps.
4#![forbid(unsafe_code)]
5#![deny(unused_extern_crates)]
6#![cfg_attr(feature = "field-extractors", feature(adt_const_params))]
7#![cfg_attr(feature = "field-extractors", feature(unsized_const_params))]
8#![cfg_attr(feature = "field-extractors", allow(incomplete_features))]
9
10extern crate self as ft_sdk;
11
12pub mod auth;
13pub mod chr;
14mod crypto;
15pub mod data;
16mod error;
17pub mod form;
18pub mod from_request;
19pub mod processor;
20mod rng;
21pub mod schema;
22pub mod session;
23pub mod utils;
24
25pub use anyhow::{anyhow, bail, ensure, Context, Error};
26pub use auth::UserId;
27pub use crypto::{DecryptionError, EncryptedString, PlainText};
28pub use error::{not_found_, server_error_, single_error, unauthorised_, SpecialError};
29#[cfg(feature = "field-extractors")]
30pub use from_request::{AppUrl, Cookie, Hidden, Optional, Query, Required};
31pub use from_request::{
32    Config, Form, FromRequest, Host, MainPackage, Path, Scheme, WasmPackage, WrappedFromRequest,
33};
34pub use ft_derive::{data, form, processor, wrapped_processor};
35#[cfg(feature = "postgres")]
36pub use ft_sys::PgConnection;
37#[cfg(feature = "sqlite")]
38pub use ft_sys::SqliteConnection;
39pub use ft_sys::{email, env, http, println, ConnectionError, UserData};
40pub use ft_sys_shared::{
41    CancelEmailError, Email, EmailAddress, EmailContent, EmailHandle, RenderedEmail, SendEmailError,
42};
43pub use rng::Rng;
44pub use session::{SessionData, SessionID};
45
46pub type FrontendData = std::collections::HashMap<String, serde_json::Value>;
47pub type FormError = std::collections::HashMap<String, String>;
48pub type Result<T> = std::result::Result<T, Error>;
49
50#[cfg(all(feature = "sqlite-default", feature = "postgres-default"))]
51compile_error!("Both sqlite and postgres features are enabled. Only one should be enabled.");
52
53#[cfg(feature = "sqlite-default")]
54pub type Connection = SqliteConnection;
55
56#[cfg(feature = "postgres-default")]
57pub type Connection = PgConnection;
58
59#[cfg(any(feature = "sqlite-default", feature = "postgres-default"))]
60pub fn default_connection() -> std::result::Result<Connection, ConnectionError> {
61    #[cfg(feature = "sqlite-default")]
62    {
63        default_sqlite()
64    }
65
66    #[cfg(feature = "postgres-default")]
67    {
68        default_pg()
69    }
70}
71
72/// Get a connection to the default postgres database.
73#[cfg(feature = "postgres")]
74pub fn default_pg() -> std::result::Result<PgConnection, ConnectionError> {
75    PgConnection::connect("default")
76}
77
78/// Get a connection to the default sqlite database.
79///
80/// Most FifthTry Apps should use this function to get the default connection.
81#[cfg(feature = "sqlite")]
82pub fn default_sqlite() -> std::result::Result<SqliteConnection, ConnectionError> {
83    let db = ft_sys::env::var("DB_FILE".to_string());
84    let db_url = db.unwrap_or_else(|| "default".to_string());
85
86    SqliteConnection::connect(db_url.as_str())
87}
88
89pub(crate) fn json<T: serde::Serialize>(
90    t: T,
91) -> std::result::Result<::http::Response<bytes::Bytes>, ft_sdk::Error> {
92    let d = match serde_json::to_string(&t) {
93        Ok(d) => d,
94        Err(e) => {
95            return Ok(::http::Response::builder()
96                .status(500)
97                .body(format!("json-error: {e:?}\n").into())?)
98        }
99    };
100
101    Ok(::http::Response::builder()
102        .status(200)
103        .header("Content-Type", "application/json")
104        .body(d.into())?)
105}