feat(services/autumn): cors support
This commit is contained in:
17
Cargo.lock
generated
17
Cargo.lock
generated
@@ -5323,6 +5323,7 @@ dependencies = [
|
|||||||
"strum_macros",
|
"strum_macros",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"tokio 1.35.1",
|
"tokio 1.35.1",
|
||||||
|
"tower-http",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
"ulid 1.1.3",
|
"ulid 1.1.3",
|
||||||
@@ -7122,6 +7123,22 @@ dependencies = [
|
|||||||
"tracing",
|
"tracing",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tower-http"
|
||||||
|
version = "0.5.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e9cd434a998747dd2c4276bc96ee2e0c7a2eadf3cae88e52be55a05fa9053f5"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.6.0",
|
||||||
|
"bytes 1.5.0",
|
||||||
|
"http 1.1.0",
|
||||||
|
"http-body 1.0.0",
|
||||||
|
"http-body-util",
|
||||||
|
"pin-project-lite 0.2.13",
|
||||||
|
"tower-layer",
|
||||||
|
"tower-service",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tower-layer"
|
name = "tower-layer"
|
||||||
version = "0.3.2"
|
version = "0.3.2"
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ tempfile = "3.12.0"
|
|||||||
axum-macros = "0.4.1"
|
axum-macros = "0.4.1"
|
||||||
axum_typed_multipart = "0.12.1"
|
axum_typed_multipart = "0.12.1"
|
||||||
axum = { version = "0.7.5", features = ["multipart"] }
|
axum = { version = "0.7.5", features = ["multipart"] }
|
||||||
|
tower-http = { version = "0.5.2", features = ["cors"] }
|
||||||
|
|
||||||
# OpenAPI & documentation generation
|
# OpenAPI & documentation generation
|
||||||
utoipa-scalar = { version = "0.1.0", features = ["axum"] }
|
utoipa-scalar = { version = "0.1.0", features = ["axum"] }
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use std::{
|
|||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{DefaultBodyLimit, Path, State},
|
extract::{DefaultBodyLimit, Path, State},
|
||||||
http::header,
|
http::{header, Method},
|
||||||
response::{IntoResponse, Redirect, Response},
|
response::{IntoResponse, Redirect, Response},
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
Json, Router,
|
Json, Router,
|
||||||
@@ -21,6 +21,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
use sha2::Digest;
|
use sha2::Digest;
|
||||||
use tempfile::NamedTempFile;
|
use tempfile::NamedTempFile;
|
||||||
use tokio::time::Instant;
|
use tokio::time::Instant;
|
||||||
|
use tower_http::cors::{AllowHeaders, Any, CorsLayer};
|
||||||
use utoipa::ToSchema;
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
use crate::{exif::strip_metadata, metadata::generate_metadata, mime_type::determine_mime_type};
|
use crate::{exif::strip_metadata, metadata::generate_metadata, mime_type::determine_mime_type};
|
||||||
@@ -29,16 +30,24 @@ use crate::{exif::strip_metadata, metadata::generate_metadata, mime_type::determ
|
|||||||
pub async fn router() -> Router<Database> {
|
pub async fn router() -> Router<Database> {
|
||||||
let config = config().await;
|
let config = config().await;
|
||||||
|
|
||||||
|
let cors = CorsLayer::new()
|
||||||
|
.allow_methods([Method::POST])
|
||||||
|
.allow_headers(AllowHeaders::mirror_request())
|
||||||
|
.allow_origin(Any);
|
||||||
|
|
||||||
Router::new()
|
Router::new()
|
||||||
.route("/", get(root))
|
.route("/", get(root))
|
||||||
.route(
|
.route(
|
||||||
"/:tag",
|
"/:tag",
|
||||||
post(upload_file).layer(DefaultBodyLimit::max(
|
post(upload_file)
|
||||||
config.features.limits.global.body_limit_size,
|
.options(options)
|
||||||
)),
|
.layer(DefaultBodyLimit::max(
|
||||||
|
config.features.limits.global.body_limit_size,
|
||||||
|
)),
|
||||||
)
|
)
|
||||||
.route("/:tag/:file_id", get(fetch_preview))
|
.route("/:tag/:file_id", get(fetch_preview))
|
||||||
.route("/:tag/:file_id/:file_name", get(fetch_file))
|
.route("/:tag/:file_id/:file_name", get(fetch_file))
|
||||||
|
.layer(cors)
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
@@ -86,6 +95,9 @@ async fn root() -> Json<RootResponse> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Empty handler for OPTIONS routes
|
||||||
|
async fn options() {}
|
||||||
|
|
||||||
/// Available tags to upload to
|
/// Available tags to upload to
|
||||||
#[derive(Clone, Deserialize, Debug, ToSchema, strum_macros::IntoStaticStr)]
|
#[derive(Clone, Deserialize, Debug, ToSchema, strum_macros::IntoStaticStr)]
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
|
|||||||
Reference in New Issue
Block a user