refactor: simplify actix app config
This commit is contained in:
parent
c33cece59e
commit
bf2e91a2c5
|
@ -424,7 +424,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "datatrash"
|
name = "datatrash"
|
||||||
version = "2.2.1"
|
version = "2.2.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-files",
|
"actix-files",
|
||||||
"actix-governor",
|
"actix-governor",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "datatrash"
|
name = "datatrash"
|
||||||
version = "2.2.1"
|
version = "2.2.2"
|
||||||
authors = ["neri"]
|
authors = ["neri"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
|
26
src/main.rs
26
src/main.rs
|
@ -13,7 +13,7 @@ use actix_files::Files;
|
||||||
use actix_governor::{Governor, GovernorConfigBuilder};
|
use actix_governor::{Governor, GovernorConfigBuilder};
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
http::header::{HeaderName, HeaderValue, CONTENT_SECURITY_POLICY, X_CONTENT_TYPE_OPTIONS},
|
http::header::{HeaderName, HeaderValue, CONTENT_SECURITY_POLICY, X_CONTENT_TYPE_OPTIONS},
|
||||||
middleware::{self, DefaultHeaders, Logger},
|
middleware::{self, Condition, DefaultHeaders, Logger},
|
||||||
web::{self, Data},
|
web::{self, Data},
|
||||||
App, Error, HttpResponse, HttpServer,
|
App, Error, HttpResponse, HttpServer,
|
||||||
};
|
};
|
||||||
|
@ -68,7 +68,7 @@ async fn main() -> std::io::Result<()> {
|
||||||
|
|
||||||
let http_server = HttpServer::new({
|
let http_server = HttpServer::new({
|
||||||
move || {
|
move || {
|
||||||
let app = App::new()
|
App::new()
|
||||||
.wrap(Logger::new(r#"%{r}a "%r" =%s %bbytes %Tsec"#))
|
.wrap(Logger::new(r#"%{r}a "%r" =%s %bbytes %Tsec"#))
|
||||||
.wrap(
|
.wrap(
|
||||||
DefaultHeaders::new()
|
DefaultHeaders::new()
|
||||||
|
@ -76,6 +76,7 @@ async fn main() -> std::io::Result<()> {
|
||||||
.add((X_CONTENT_TYPE_OPTIONS, HeaderValue::from_static("nosniff"))),
|
.add((X_CONTENT_TYPE_OPTIONS, HeaderValue::from_static("nosniff"))),
|
||||||
)
|
)
|
||||||
.wrap(middleware::Compress::default())
|
.wrap(middleware::Compress::default())
|
||||||
|
.wrap(middleware::NormalizePath::trim())
|
||||||
.app_data(db.clone())
|
.app_data(db.clone())
|
||||||
.app_data(expiry_watch_sender.clone())
|
.app_data(expiry_watch_sender.clone())
|
||||||
.app_data(config.clone())
|
.app_data(config.clone())
|
||||||
|
@ -86,27 +87,18 @@ async fn main() -> std::io::Result<()> {
|
||||||
.route(web::get().to(upload::uploaded)),
|
.route(web::get().to(upload::uploaded)),
|
||||||
)
|
)
|
||||||
.service(Files::new("/static", "static").disable_content_disposition())
|
.service(Files::new("/static", "static").disable_content_disposition())
|
||||||
.default_service(web::route().to(not_found));
|
.default_service(web::route().to(not_found))
|
||||||
if config.enable_rate_limit {
|
.service(
|
||||||
app.service(
|
|
||||||
web::resource([
|
web::resource([
|
||||||
"/{id:[a-z0-9]{5}}",
|
"/{id:[a-z0-9]{5}}",
|
||||||
"/{id:[a-z0-9]{5}}/",
|
|
||||||
"/{id:[a-z0-9]{5}}/{name}",
|
"/{id:[a-z0-9]{5}}/{name}",
|
||||||
])
|
])
|
||||||
.wrap(Governor::new(&governor_conf))
|
.wrap(Condition::new(
|
||||||
|
config.enable_rate_limit,
|
||||||
|
Governor::new(&governor_conf),
|
||||||
|
))
|
||||||
.route(web::get().to(download::download)),
|
.route(web::get().to(download::download)),
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
app.service(
|
|
||||||
web::resource([
|
|
||||||
"/{id:[a-z0-9]{5}}",
|
|
||||||
"/{id:[a-z0-9]{5}}/",
|
|
||||||
"/{id:[a-z0-9]{5}}/{name}",
|
|
||||||
])
|
|
||||||
.route(web::get().to(download::download)),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.bind(bind_address)?
|
.bind(bind_address)?
|
||||||
|
|
Loading…
Reference in New Issue