mod utils;
use {
::axum::{routing::*, *},
moka::future::Cache,
std::time::*,
tokio::{net::*, *},
tower_http::trace::*,
tower_http_cache_plus::{
cache::{implementation::moka::*, *},
*,
},
};
const CACHE_SIZE: u64 = 1024 * 1024;
const CACHE_DURATION: Duration = Duration::from_secs(10);
const MAX_BODY_SIZE: usize = 1024;
#[main]
async fn main() {
utils::init_tracing();
let cache = Cache::<CommonCacheKey, _, _>::builder()
.name("http")
.for_http_response()
.max_capacity(CACHE_SIZE)
.time_to_live(CACHE_DURATION)
.eviction_listener(|key, _value, cause| {
tracing::debug!("evict ({:?}): {}", cause, key);
})
.build();
let cache = MokaCacheImplementation::new(cache);
let router = Router::default()
.route("/", get(("Hello, world!\n",)))
.layer(
CachingLayer::default()
.cache(cache.clone())
.max_cacheable_body_size(MAX_BODY_SIZE)
.keep_identity_encoding(false),
)
.layer(TraceLayer::new_for_http());
let listener = TcpListener::bind("[::]:8080")
.await
.expect("TcpListener::bind");
tracing::info!("bound to: {:?}", listener.local_addr());
serve(listener, router).await.expect("axum::serve");
}