Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6f98163632 | ||
|
|
57eeb0878c |
Generated
+1
-1
@@ -84,7 +84,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "api"
|
name = "api"
|
||||||
version = "1.1.3"
|
version = "1.1.4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"bitcode",
|
"bitcode",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "api"
|
name = "api"
|
||||||
version = "1.1.3"
|
version = "1.1.4"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
+19
-5
@@ -5,7 +5,8 @@ use std::sync::Arc;
|
|||||||
use axum::{
|
use axum::{
|
||||||
Json, Router,
|
Json, Router,
|
||||||
body::{Body, Bytes},
|
body::{Body, Bytes},
|
||||||
extract::{Query, State as AxumState},
|
extract::{Query, Request, State as AxumState},
|
||||||
|
middleware::Next,
|
||||||
response::{Redirect, Response},
|
response::{Redirect, Response},
|
||||||
routing::get,
|
routing::get,
|
||||||
};
|
};
|
||||||
@@ -26,6 +27,7 @@ pub fn router() -> Router<Arc<State>> {
|
|||||||
.route("/proxy", get(proxy_with_cors))
|
.route("/proxy", get(proxy_with_cors))
|
||||||
.fallback(error_404)
|
.fallback(error_404)
|
||||||
.method_not_allowed_fallback(error_405)
|
.method_not_allowed_fallback(error_405)
|
||||||
|
.layer(axum::middleware::from_fn(cors_middleware))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn error_404() -> Error {
|
async fn error_404() -> Error {
|
||||||
@@ -42,6 +44,22 @@ async fn error_405() -> Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn cors_middleware(req: Request, next: Next) -> Response {
|
||||||
|
let mut response = next.run(req).await;
|
||||||
|
|
||||||
|
let headers = response.headers_mut();
|
||||||
|
|
||||||
|
// Insert CORS headers
|
||||||
|
headers.insert("access-control-allow-origin", "*".parse().unwrap());
|
||||||
|
headers.insert(
|
||||||
|
"access-control-allow-methods",
|
||||||
|
"GET, OPTIONS".parse().unwrap(),
|
||||||
|
);
|
||||||
|
headers.insert("access-control-allow-headers", "*".parse().unwrap());
|
||||||
|
|
||||||
|
response
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Encode, Decode)]
|
#[derive(Debug, Serialize, Encode, Decode)]
|
||||||
struct Profile {
|
struct Profile {
|
||||||
username: String,
|
username: String,
|
||||||
@@ -336,10 +354,6 @@ async fn proxy_with_cors(Query(params): Query<ProxyQuery>) -> Result<Response, E
|
|||||||
}
|
}
|
||||||
headers_mut.insert(key, value.clone());
|
headers_mut.insert(key, value.clone());
|
||||||
}
|
}
|
||||||
// Insert CORS headers
|
|
||||||
headers_mut.insert("access-control-allow-origin", "*".parse().unwrap());
|
|
||||||
headers_mut.insert("access-control-allow-methods", "GET, OPTIONS".parse().unwrap());
|
|
||||||
headers_mut.insert("access-control-allow-headers", "*".parse().unwrap());
|
|
||||||
|
|
||||||
Ok(response.body(body).unwrap())
|
Ok(response.body(body).unwrap())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user