Add CORS
This commit is contained in:
parent
6be8f81fa9
commit
16be83a0de
|
@ -104,6 +104,12 @@ version = "0.22.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
|
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitflags"
|
||||||
|
version = "2.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bytes"
|
name = "bytes"
|
||||||
version = "1.7.1"
|
version = "1.7.1"
|
||||||
|
@ -306,6 +312,7 @@ dependencies = [
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tower 0.5.0",
|
"tower 0.5.0",
|
||||||
|
"tower-http",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -641,6 +648,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",
|
||||||
|
"bytes",
|
||||||
|
"http",
|
||||||
|
"http-body",
|
||||||
|
"http-body-util",
|
||||||
|
"pin-project-lite",
|
||||||
|
"tower-layer",
|
||||||
|
"tower-service",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tower-layer"
|
name = "tower-layer"
|
||||||
version = "0.3.3"
|
version = "0.3.3"
|
||||||
|
|
|
@ -11,4 +11,5 @@ serde = { version = "1.0.209", features = ["derive"] }
|
||||||
serde_json = "1.0.127"
|
serde_json = "1.0.127"
|
||||||
tokio = { version = "1.39.3", features = ["rt-multi-thread"] }
|
tokio = { version = "1.39.3", features = ["rt-multi-thread"] }
|
||||||
tower = { version = "0.5.0", features = ["limit", "buffer"] }
|
tower = { version = "0.5.0", features = ["limit", "buffer"] }
|
||||||
|
tower-http = { version = "0.5.2", features = ["cors"] }
|
||||||
tracing-subscriber = "0.3.18"
|
tracing-subscriber = "0.3.18"
|
||||||
|
|
16
src/main.rs
16
src/main.rs
|
@ -8,7 +8,7 @@ use std::{
|
||||||
use axum::{
|
use axum::{
|
||||||
error_handling::HandleErrorLayer,
|
error_handling::HandleErrorLayer,
|
||||||
extract::Path,
|
extract::Path,
|
||||||
http::{header, StatusCode},
|
http::{header, HeaderValue, Method, StatusCode},
|
||||||
response::IntoResponse,
|
response::IntoResponse,
|
||||||
routing::get,
|
routing::get,
|
||||||
BoxError, Router,
|
BoxError, Router,
|
||||||
|
@ -16,6 +16,7 @@ use axum::{
|
||||||
|
|
||||||
use base64::{engine::general_purpose::STANDARD, Engine};
|
use base64::{engine::general_purpose::STANDARD, Engine};
|
||||||
use tower::{buffer::BufferLayer, limit::rate::RateLimitLayer, ServiceBuilder};
|
use tower::{buffer::BufferLayer, limit::rate::RateLimitLayer, ServiceBuilder};
|
||||||
|
use tower_http::cors::CorsLayer;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
@ -49,6 +50,11 @@ fn app() -> Router {
|
||||||
.layer(BufferLayer::new(1024))
|
.layer(BufferLayer::new(1024))
|
||||||
.layer(RateLimitLayer::new(5, Duration::from_secs(1))),
|
.layer(RateLimitLayer::new(5, Duration::from_secs(1))),
|
||||||
)
|
)
|
||||||
|
.layer(
|
||||||
|
CorsLayer::new()
|
||||||
|
.allow_origin("*".parse::<HeaderValue>().unwrap())
|
||||||
|
.allow_methods([Method::GET]),
|
||||||
|
)
|
||||||
.route("/:command", get(command))
|
.route("/:command", get(command))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +77,13 @@ impl From<FromUtf8Error> for ManserveErr {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_man_loc(command: &str) -> Result<String, ManserveErr> {
|
fn get_man_loc(command: &str) -> Result<String, ManserveErr> {
|
||||||
let output = Command::new("man").args(["-w", command]).output()?;
|
let output = match Command::new("man").args(["-w", command]).output() {
|
||||||
|
Ok(output) => output,
|
||||||
|
Err(error) => {
|
||||||
|
println!("{error}");
|
||||||
|
return Err(error.into());
|
||||||
|
}
|
||||||
|
};
|
||||||
if !output.status.success() {
|
if !output.status.success() {
|
||||||
return Err(ManserveErr::NotFound);
|
return Err(ManserveErr::NotFound);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue