cors stuff
This commit is contained in:
parent
568589ebcd
commit
e2027bda80
|
@ -121,6 +121,19 @@ version = "1.0.75"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
|
||||
|
||||
[[package]]
|
||||
name = "async-compression"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "62b74f44609f0f91493e3082d3734d98497e094777144380ea4db9f9905dd5b6"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"futures-core",
|
||||
"memchr",
|
||||
"pin-project-lite",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.73"
|
||||
|
@ -434,6 +447,15 @@ version = "2.2.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9cace84e55f07e7301bae1c519df89cdad8cc3cd868413d3fdbdeca9ff3db484"
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-queue"
|
||||
version = "0.3.8"
|
||||
|
@ -653,6 +675,16 @@ version = "2.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764"
|
||||
|
||||
[[package]]
|
||||
name = "flate2"
|
||||
version = "1.0.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c6c98ee8095e9d1dcbf2fcc6d95acccb90d1c81db1e44725c6a984b1dbdfb010"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "flume"
|
||||
version = "0.10.14"
|
||||
|
@ -949,6 +981,12 @@ dependencies = [
|
|||
"pin-project-lite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "http-range-header"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "add0ab9360ddbd88cfeb3bd9574a1d85cfdfa14db10b3e21d3700dbc4328758f"
|
||||
|
||||
[[package]]
|
||||
name = "httparse"
|
||||
version = "1.8.0"
|
||||
|
@ -2246,6 +2284,7 @@ dependencies = [
|
|||
"tinyrand",
|
||||
"tinyrand-std",
|
||||
"tokio",
|
||||
"tower-http",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
@ -2454,6 +2493,19 @@ dependencies = [
|
|||
"tungstenite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-util"
|
||||
version = "0.7.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "806fe8c2c87eccc8b3267cbae29ed3ab2d0bd37fca70ab622e46aaa9375ddb7d"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"futures-core",
|
||||
"futures-sink",
|
||||
"pin-project-lite",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.5.11"
|
||||
|
@ -2479,6 +2531,28 @@ dependencies = [
|
|||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tower-http"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "55ae70283aba8d2a8b411c695c437fe25b8b5e44e23e780662002fc72fb47a82"
|
||||
dependencies = [
|
||||
"async-compression",
|
||||
"bitflags 2.4.0",
|
||||
"bytes",
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"http",
|
||||
"http-body",
|
||||
"http-range-header",
|
||||
"pin-project-lite",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
"tower-layer",
|
||||
"tower-service",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tower-layer"
|
||||
version = "0.3.2"
|
||||
|
|
17
Cargo.toml
17
Cargo.toml
|
@ -32,11 +32,12 @@ tracing-subscriber = { version = "0.3", features = [
|
|||
] }
|
||||
once_cell = "1"
|
||||
sqlx = { version = "0.7", features = ["runtime-tokio", "tls-rustls", "postgres", "macros"] }
|
||||
serde = "1.0.186"
|
||||
serde_derive = "1.0.186"
|
||||
axum-jsonschema = { version = "0.6.0", features = ["aide"] }
|
||||
axum-macros = "0.3.8"
|
||||
tinyrand = "0.5.0"
|
||||
tinyrand-std = "0.5.0"
|
||||
mime_guess = "2.0.4"
|
||||
hyper = "0.14.27"
|
||||
serde = "1"
|
||||
serde_derive = "1"
|
||||
axum-jsonschema = { version = "0.6", features = ["aide"] }
|
||||
axum-macros = "0.3"
|
||||
tinyrand = "0.5"
|
||||
tinyrand-std = "0.5"
|
||||
mime_guess = "2"
|
||||
hyper = "0.14"
|
||||
tower-http = {version = "0.4", features = ["compression-gzip", "trace", "cors", "timeout"]}
|
|
@ -4,6 +4,7 @@ use serde_derive::{Deserialize, Serialize};
|
|||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
pub db_url: String,
|
||||
pub server_url: String,
|
||||
}
|
||||
|
||||
pub static CONFIG: Lazy<Config> =
|
||||
|
@ -12,7 +13,8 @@ pub static CONFIG: Lazy<Config> =
|
|||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
db_url: "postgres://starchart:hunter2@localhost/starchart".to_string(),
|
||||
db_url: "postgres://starchart:hunter2@localhost/starchart".into(),
|
||||
server_url: "127.0.0.1:7056/".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
47
src/main.rs
47
src/main.rs
|
@ -1,9 +1,15 @@
|
|||
use aide::{axum::ApiRouter, openapi::OpenApi};
|
||||
use axum::Extension;
|
||||
use axum::{extract::DefaultBodyLimit, Extension};
|
||||
use hyper::{
|
||||
header::{ACCESS_CONTROL_ALLOW_ORIGIN, CONTENT_LENGTH, DATE, SET_COOKIE, VARY},
|
||||
Method,
|
||||
};
|
||||
use std::{
|
||||
net::{Ipv4Addr, SocketAddr},
|
||||
sync::Arc,
|
||||
time::Duration,
|
||||
};
|
||||
use tower_http::{compression::CompressionLayer, cors::CorsLayer, timeout::TimeoutLayer};
|
||||
use tracing::info;
|
||||
use tracing_subscriber::fmt::SubscriberBuilder;
|
||||
|
||||
|
@ -43,7 +49,44 @@ async fn generate_server() -> Result<axum::Router, Box<dyn std::error::Error>> {
|
|||
})
|
||||
.version("0.1.0")
|
||||
})
|
||||
.layer(Extension(Arc::new(open_api))))
|
||||
.layer(Extension(Arc::new(open_api)))
|
||||
.layer(TimeoutLayer::new(Duration::from_secs(20)))
|
||||
.layer(DefaultBodyLimit::max(2000))
|
||||
.layer(
|
||||
CorsLayer::new()
|
||||
.allow_methods([Method::POST, Method::GET, Method::PUT, Method::PATCH])
|
||||
.allow_origin([
|
||||
#[cfg(debug_assertions)]
|
||||
"localhost".parse()?,
|
||||
#[cfg(debug_assertions)]
|
||||
"http://localhost:8080".parse()?,
|
||||
#[cfg(debug_assertions)]
|
||||
"http://127.0.0.1:8080".parse()?,
|
||||
crate::config::CONFIG.server_url.parse()?,
|
||||
])
|
||||
.allow_headers([
|
||||
ACCESS_CONTROL_ALLOW_ORIGIN,
|
||||
SET_COOKIE,
|
||||
VARY,
|
||||
DATE,
|
||||
CONTENT_LENGTH,
|
||||
])
|
||||
.expose_headers([
|
||||
ACCESS_CONTROL_ALLOW_ORIGIN,
|
||||
SET_COOKIE,
|
||||
VARY,
|
||||
DATE,
|
||||
CONTENT_LENGTH,
|
||||
])
|
||||
.allow_credentials(true),
|
||||
)
|
||||
.layer(
|
||||
CompressionLayer::new()
|
||||
.gzip(true)
|
||||
// we have as much cpu as we want really
|
||||
// since rust is kinda overkill anyway
|
||||
.quality(tower_http::CompressionLevel::Best),
|
||||
))
|
||||
}
|
||||
|
||||
async fn start_server() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
|
Loading…
Reference in New Issue