added rate limiting layer to activate, added author nested router
This commit is contained in:
parent
65e058b3c1
commit
c508f78043
@ -55,25 +55,25 @@ async fn main() {
|
|||||||
)
|
)
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string()) != "development" {
|
// if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string()) != "development" {
|
||||||
println!("we're not in development, starting up the rate limiter");
|
println!("we're not in development, starting up the rate limiter");
|
||||||
let governor_conf = Arc::new(
|
let governor_conf = Arc::new(
|
||||||
GovernorConfigBuilder::default()
|
GovernorConfigBuilder::default()
|
||||||
.per_second(2)
|
.per_second(2)
|
||||||
.burst_size(5)
|
.burst_size(5)
|
||||||
.finish()
|
.finish()
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let governor_limiter = governor_conf.limiter().clone();
|
let governor_limiter = governor_conf.limiter().clone();
|
||||||
let interval = Duration::from_secs(60);
|
let interval = Duration::from_secs(60);
|
||||||
// a separate background task to clean up
|
// a separate background task to clean up
|
||||||
std::thread::spawn(move || loop {
|
std::thread::spawn(move || loop {
|
||||||
std::thread::sleep(interval);
|
std::thread::sleep(interval);
|
||||||
tracing::info!("rate limiting storage size: {}", governor_limiter.len());
|
tracing::info!("rate limiting storage size: {}", governor_limiter.len());
|
||||||
governor_limiter.retain_recent();
|
governor_limiter.retain_recent();
|
||||||
});
|
});
|
||||||
}
|
// }
|
||||||
|
|
||||||
// grabbing the database url from our env variables
|
// grabbing the database url from our env variables
|
||||||
let db_connection_str = std::env::var("DATABASE_URL")
|
let db_connection_str = std::env::var("DATABASE_URL")
|
||||||
@ -93,15 +93,22 @@ async fn main() {
|
|||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.nest("/", routes::root::RootRoute::routes())
|
.nest("/", routes::root::RootRoute::routes())
|
||||||
.nest("/posts", routes::posts::PostsRoute::routes(&app_state))
|
.nest("/posts", routes::posts::PostsRoute::routes(&app_state))
|
||||||
|
.nest(
|
||||||
|
"/comments",
|
||||||
|
routes::comments::CommentsRoute::routes(&app_state),
|
||||||
|
)
|
||||||
|
.nest(
|
||||||
|
"/authors",
|
||||||
|
routes::authors::AuthorsRoute::routes(&app_state),
|
||||||
|
)
|
||||||
.layer(
|
.layer(
|
||||||
TraceLayer::new_for_http()
|
TraceLayer::new_for_http()
|
||||||
.make_span_with(trace::DefaultMakeSpan::new().level(tracing::Level::INFO))
|
.make_span_with(trace::DefaultMakeSpan::new().level(tracing::Level::INFO))
|
||||||
.on_response(trace::DefaultOnResponse::new().level(tracing::Level::INFO)),
|
.on_response(trace::DefaultOnResponse::new().level(tracing::Level::INFO)),
|
||||||
)
|
)
|
||||||
.nest(
|
.layer(GovernorLayer {
|
||||||
"/comments",
|
config: governor_conf,
|
||||||
routes::comments::CommentsRoute::routes(&app_state),
|
});
|
||||||
);
|
|
||||||
|
|
||||||
// run it with hyper
|
// run it with hyper
|
||||||
let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||||
|
Loading…
Reference in New Issue
Block a user