summaryrefslogtreecommitdiffstats
path: root/src/server.rs
blob: dd49a54e5e28f869fce2f5bcf47c2e953590865f (plain)
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
use activitypub_federation::config::{FederationConfig, FederationMiddleware};
use axum::{Router, routing::get};
use nanoid::nanoid;
use stack_up::Environment;
use tower_http::trace::TraceLayer;
use url::Url;

use crate::{error::AppError, server::routes::health_check, state::AppHandle};

pub mod activities;
pub mod routes;

const ALPHABET: [char; 36] = [
    '2', '3', '4', '5', '6', '7', '8', '9', '_', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
    'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '-',
];

pub fn generate_object_id(domain: &str, env: Environment) -> Result<Url, AppError> {
    let id = nanoid!(21, &ALPHABET);
    Ok(Url::parse(&format!(
        "{}://{domain}/objects/{id}",
        match env {
            Environment::Development => "http",
            Environment::Production => "https",
        },
    ))?)
}

pub fn router(state: FederationConfig<AppHandle>) -> Router {
    Router::new()
        .merge(routes::users::users_router())
        .route("/", get(health_check))
        .layer(TraceLayer::new_for_http())
        .layer(FederationMiddleware::new(state))
}

#[cfg(test)]
pub(crate) fn test_config() -> stack_up::Configuration {
    use stack_up::Configuration;

    let config_path = "sellershut.toml";

    let config = config::Config::builder()
        .add_source(config::File::new(config_path, config::FileFormat::Toml))
        .build()
        .unwrap();

    config.try_deserialize::<Configuration>().unwrap()
}