Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cache server exposed via module #10

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions affinidi-did-resolver-cache-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
133 changes: 4 additions & 129 deletions affinidi-did-resolver-cache-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -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::<SocketAddr>())
.await
.unwrap();

Ok(())
return start().await;
}
131 changes: 131 additions & 0 deletions affinidi-did-resolver-cache-server/src/server.rs
Original file line number Diff line number Diff line change
@@ -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::<SocketAddr>())
.await
.unwrap();

Ok(())
}
Loading