Skip to content

Commit

Permalink
feat(services/autumn): cors support
Browse files Browse the repository at this point in the history
  • Loading branch information
insertish committed Sep 29, 2024
1 parent a3c5b1b commit 916f47e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/services/autumn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ tempfile = "3.12.0"
axum-macros = "0.4.1"
axum_typed_multipart = "0.12.1"
axum = { version = "0.7.5", features = ["multipart"] }
tower-http = { version = "0.5.2", features = ["cors"] }

# OpenAPI & documentation generation
utoipa-scalar = { version = "0.1.0", features = ["axum"] }
Expand Down
20 changes: 16 additions & 4 deletions crates/services/autumn/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use axum::{
extract::{DefaultBodyLimit, Path, State},
http::header,
http::{header, Method},
response::{IntoResponse, Redirect, Response},
routing::{get, post},
Json, Router,
Expand All @@ -21,6 +21,7 @@ use serde::{Deserialize, Serialize};
use sha2::Digest;
use tempfile::NamedTempFile;
use tokio::time::Instant;
use tower_http::cors::{AllowHeaders, Any, CorsLayer};
use utoipa::ToSchema;

use crate::{exif::strip_metadata, metadata::generate_metadata, mime_type::determine_mime_type};
Expand All @@ -29,16 +30,24 @@ use crate::{exif::strip_metadata, metadata::generate_metadata, mime_type::determ
pub async fn router() -> Router<Database> {
let config = config().await;

let cors = CorsLayer::new()
.allow_methods([Method::POST])
.allow_headers(AllowHeaders::mirror_request())
.allow_origin(Any);

Router::new()
.route("/", get(root))
.route(
"/:tag",
post(upload_file).layer(DefaultBodyLimit::max(
config.features.limits.global.body_limit_size,
)),
post(upload_file)
.options(options)
.layer(DefaultBodyLimit::max(
config.features.limits.global.body_limit_size,
)),
)
.route("/:tag/:file_id", get(fetch_preview))
.route("/:tag/:file_id/:file_name", get(fetch_file))
.layer(cors)
}

lazy_static! {
Expand Down Expand Up @@ -86,6 +95,9 @@ async fn root() -> Json<RootResponse> {
})
}

/// Empty handler for OPTIONS routes
async fn options() {}

/// Available tags to upload to
#[derive(Clone, Deserialize, Debug, ToSchema, strum_macros::IntoStaticStr)]
#[allow(non_camel_case_types)]
Expand Down

0 comments on commit 916f47e

Please sign in to comment.