From 2dd49f2ed65756af6e51049d285356622a3b9b9f Mon Sep 17 00:00:00 2001 From: Robert Kwolek Date: Tue, 10 Sep 2024 09:51:23 +0200 Subject: [PATCH] feat: cache server exposed via module --- affinidi-did-resolver-cache-server/src/lib.rs | 1 + .../src/main.rs | 133 +----------------- .../src/server.rs | 131 +++++++++++++++++ 3 files changed, 136 insertions(+), 129 deletions(-) create mode 100644 affinidi-did-resolver-cache-server/src/server.rs diff --git a/affinidi-did-resolver-cache-server/src/lib.rs b/affinidi-did-resolver-cache-server/src/lib.rs index 608d750..e6a185e 100644 --- a/affinidi-did-resolver-cache-server/src/lib.rs +++ b/affinidi-did-resolver-cache-server/src/lib.rs @@ -15,6 +15,7 @@ pub(crate) mod common; pub mod config; pub mod errors; pub mod handlers; +pub mod server; pub mod session; pub mod statistics; diff --git a/affinidi-did-resolver-cache-server/src/main.rs b/affinidi-did-resolver-cache-server/src/main.rs index 1f4cf02..5c804f9 100644 --- a/affinidi-did-resolver-cache-server/src/main.rs +++ b/affinidi-did-resolver-cache-server/src/main.rs @@ -1,135 +1,10 @@ -use affinidi_did_resolver_cache_sdk::{ - config::ClientConfigBuilder, errors::DIDCacheError, DIDCacheClient, -}; -use affinidi_did_resolver_cache_server::{ - config::init, - handlers::{application_routes, health_checker_handler}, - statistics::{statistics, Statistics}, - SharedData, -}; -use axum::{routing::get, Router}; -use http::Method; -use std::{env, net::SocketAddr, sync::Arc}; -use tokio::sync::Mutex; -use tower_http::{ - cors::CorsLayer, - trace::{self, TraceLayer}, -}; -use tracing::{event, Level}; -use tracing_subscriber::{filter, layer::SubscriberExt, reload, util::SubscriberInitExt}; +use affinidi_did_resolver_cache_sdk::{errors::DIDCacheError}; +use affinidi_did_resolver_cache_server::{server::start}; -/// Main entry point for the `affinidi-did-resolver-cache` binary. +/// Main entry point for the `affinidi-did-resolver-cache-server` binary. /// /// This binary runs as a DID Cache resolver service with built in caching #[tokio::main] async fn main() -> Result<(), DIDCacheError> { - // setup logging/tracing framework - let filter = filter::LevelFilter::INFO; // This can be changed in the config file! - let (filter, reload_handle) = reload::Layer::new(filter); - let ansi = env::var("LOCAL").is_ok(); - tracing_subscriber::registry() - .with(filter) - .with(tracing_subscriber::fmt::layer().with_ansi(ansi)) - .init(); - - if ansi { - event!(Level::INFO, ""); - event!( - Level::INFO, - r#" db 888888888888 888b 88 88888888ba, 88 88888888ba, ,ad8888ba, 88 "# - ); - event!( - Level::INFO, - r#" d88b 88 8888b 88 88 `"8b 88 88 `"8b d8"' `"8b 88 "# - ); - event!( - Level::INFO, - r#" d8'`8b 88 88 `8b 88 88 `8b 88 88 `8b d8' 88 "# - ); - event!( - Level::INFO, - r#" d8' `8b 88 88 `8b 88 88 88 88 88 88 88 ,adPPYYba, ,adPPYba, 88,dPPYba, ,adPPYba, "# - ); - event!( - Level::INFO, - r#" d8YaaaaY8b 88 88 `8b 88 88 88 88 88 88 88 "" `Y8 a8" "" 88P' "8a a8P_____88 "# - ); - event!( - Level::INFO, - r#" d8""""""""8b 88 88 `8b 88 88 8P 88 88 8P Y8, ,adPPPPP88 8b 88 88 8PP""""""" "# - ); - event!( - Level::INFO, - r#" d8' `8b 88 88 `8888 88 .a8P 88 88 .a8P Y8a. .a8P 88, ,88 "8a, ,aa 88 88 "8b, ,aa "# - ); - event!( - Level::INFO, - r#" d8' `8b 88 88 `888 88888888Y"' 88 88888888Y"' `"Y8888Y"' `"8bbdP"Y8 `"Ybbd8"' 88 88 `"Ybbd8"' "# - ); - event!(Level::INFO, ""); - } - - event!(Level::INFO, "[Loading Affinidi DID Cache configuration]"); - - let config = init(Some(reload_handle)).expect("Couldn't initialize DID Cache!"); - - // Use the affinidi-did-resolver-cache-sdk in local mode - let cache_config = ClientConfigBuilder::default() - .with_cache_capacity(config.cache_capacity_count) - .with_cache_ttl(config.cache_expire) - .build(); - - let resolver = DIDCacheClient::new(cache_config).await?; - - // Create the shared application State - let shared_state = SharedData { - service_start_timestamp: chrono::Utc::now(), - stats: Arc::new(Mutex::new(Statistics::default())), - resolver, - }; - - // Start the statistics thread - let _stats = shared_state.stats.clone(); - let _cache = shared_state.resolver.get_cache(); - tokio::spawn(async move { - statistics(config.statistics_interval, &_stats, _cache) - .await - .expect("Error starting statistics thread"); - }); - - // build our application routes - let app: Router = application_routes(&shared_state); - - // Add middleware to all routes - let app = Router::new() - .merge(app) - .layer( - CorsLayer::new() - .allow_origin(tower_http::cors::Any) - .allow_headers([http::header::CONTENT_TYPE]) - .allow_methods([ - Method::GET, - Method::POST, - Method::PUT, - Method::DELETE, - Method::PATCH, - ]), - ) - .layer( - TraceLayer::new_for_http() - .make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO)) - .on_response(trace::DefaultOnResponse::new().level(Level::INFO)), - ) - // Add the healthcheck route after the tracing so we don't fill up logs with healthchecks - .route( - "/did/healthchecker", - get(health_checker_handler).with_state(shared_state), - ); - - axum_server::bind(config.listen_address.parse().unwrap()) - .serve(app.into_make_service_with_connect_info::()) - .await - .unwrap(); - - Ok(()) + return start().await; } diff --git a/affinidi-did-resolver-cache-server/src/server.rs b/affinidi-did-resolver-cache-server/src/server.rs new file mode 100644 index 0000000..4a49bd8 --- /dev/null +++ b/affinidi-did-resolver-cache-server/src/server.rs @@ -0,0 +1,131 @@ +use affinidi_did_resolver_cache_sdk::{ + config::ClientConfigBuilder, errors::DIDCacheError, DIDCacheClient, +}; +use crate::{ + config::init, + handlers::{application_routes, health_checker_handler}, + statistics::{statistics, Statistics}, + SharedData, +}; +use axum::{routing::get, Router}; +use http::Method; +use std::{env, net::SocketAddr, sync::Arc}; +use tokio::sync::Mutex; +use tower_http::{ + cors::CorsLayer, + trace::{self, TraceLayer}, +}; +use tracing::{event, Level}; +use tracing_subscriber::{filter, layer::SubscriberExt, reload, util::SubscriberInitExt}; + +pub async fn start() -> Result<(), DIDCacheError> { + // setup logging/tracing framework + let filter = filter::LevelFilter::INFO; // This can be changed in the config file! + let (filter, reload_handle) = reload::Layer::new(filter); + let ansi = env::var("LOCAL").is_ok(); + tracing_subscriber::registry() + .with(filter) + .with(tracing_subscriber::fmt::layer().with_ansi(ansi)) + .init(); + + if ansi { + event!(Level::INFO, ""); + event!( + Level::INFO, + r#" db 888888888888 888b 88 88888888ba, 88 88888888ba, ,ad8888ba, 88 "# + ); + event!( + Level::INFO, + r#" d88b 88 8888b 88 88 `"8b 88 88 `"8b d8"' `"8b 88 "# + ); + event!( + Level::INFO, + r#" d8'`8b 88 88 `8b 88 88 `8b 88 88 `8b d8' 88 "# + ); + event!( + Level::INFO, + r#" d8' `8b 88 88 `8b 88 88 88 88 88 88 88 ,adPPYYba, ,adPPYba, 88,dPPYba, ,adPPYba, "# + ); + event!( + Level::INFO, + r#" d8YaaaaY8b 88 88 `8b 88 88 88 88 88 88 88 "" `Y8 a8" "" 88P' "8a a8P_____88 "# + ); + event!( + Level::INFO, + r#" d8""""""""8b 88 88 `8b 88 88 8P 88 88 8P Y8, ,adPPPPP88 8b 88 88 8PP""""""" "# + ); + event!( + Level::INFO, + r#" d8' `8b 88 88 `8888 88 .a8P 88 88 .a8P Y8a. .a8P 88, ,88 "8a, ,aa 88 88 "8b, ,aa "# + ); + event!( + Level::INFO, + r#" d8' `8b 88 88 `888 88888888Y"' 88 88888888Y"' `"Y8888Y"' `"8bbdP"Y8 `"Ybbd8"' 88 88 `"Ybbd8"' "# + ); + event!(Level::INFO, ""); + } + + event!(Level::INFO, "[Loading Affinidi DID Cache configuration]"); + + let config = init(Some(reload_handle)).expect("Couldn't initialize DID Cache!"); + + // Use the affinidi-did-resolver-cache-sdk in local mode + let cache_config = ClientConfigBuilder::default() + .with_cache_capacity(config.cache_capacity_count) + .with_cache_ttl(config.cache_expire) + .build(); + + let resolver = DIDCacheClient::new(cache_config).await?; + + // Create the shared application State + let shared_state = SharedData { + service_start_timestamp: chrono::Utc::now(), + stats: Arc::new(Mutex::new(Statistics::default())), + resolver, + }; + + // Start the statistics thread + let _stats = shared_state.stats.clone(); + let _cache = shared_state.resolver.get_cache(); + tokio::spawn(async move { + statistics(config.statistics_interval, &_stats, _cache) + .await + .expect("Error starting statistics thread"); + }); + + // build our application routes + let app: Router = application_routes(&shared_state); + + // Add middleware to all routes + let app = Router::new() + .merge(app) + .layer( + CorsLayer::new() + .allow_origin(tower_http::cors::Any) + .allow_headers([http::header::CONTENT_TYPE]) + .allow_methods([ + Method::GET, + Method::POST, + Method::PUT, + Method::DELETE, + Method::PATCH, + ]), + ) + .layer( + TraceLayer::new_for_http() + .make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO)) + .on_response(trace::DefaultOnResponse::new().level(Level::INFO)), + ) + // Add the healthcheck route after the tracing so we don't fill up logs with healthchecks + .route( + "/did/healthchecker", + get(health_checker_handler).with_state(shared_state), + ); + + axum_server::bind(config.listen_address.parse().unwrap()) + .serve(app.into_make_service_with_connect_info::()) + .await + .unwrap(); + + Ok(()) +}