summaryrefslogtreecommitdiffstats
path: root/crates/sellershut/src/server.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/sellershut/src/server.rs')
-rw-r--r--crates/sellershut/src/server.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/crates/sellershut/src/server.rs b/crates/sellershut/src/server.rs
new file mode 100644
index 0000000..dd49a54
--- /dev/null
+++ b/crates/sellershut/src/server.rs
@@ -0,0 +1,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()
+}