This commit is contained in:
parent
dcf8fe1f0c
commit
5f76de0a9a
4 changed files with 14 additions and 11 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -349,6 +349,12 @@ dependencies = [
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dotenv_rs"
|
||||||
|
version = "0.16.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "828401259441c2fefb04b5af2a5762cac529e17f76b87e8034b9987e541d4397"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dotenvy"
|
name = "dotenvy"
|
||||||
version = "0.15.7"
|
version = "0.15.7"
|
||||||
|
@ -778,6 +784,7 @@ dependencies = [
|
||||||
"axum-macros",
|
"axum-macros",
|
||||||
"chrono",
|
"chrono",
|
||||||
"cookie",
|
"cookie",
|
||||||
|
"dotenv_rs",
|
||||||
"figment",
|
"figment",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
|
|
@ -23,6 +23,7 @@ url = "2.4"
|
||||||
thiserror = "1.0"
|
thiserror = "1.0"
|
||||||
async-trait = "0.1"
|
async-trait = "0.1"
|
||||||
tower-http = { version = "0.4", features = ["cors"] }
|
tower-http = { version = "0.4", features = ["cors"] }
|
||||||
|
dotenv_rs = "0.16"
|
||||||
|
|
||||||
[profile.dev.package.sqlx-macros]
|
[profile.dev.package.sqlx-macros]
|
||||||
opt-level = 3
|
opt-level = 3
|
15
src/main.rs
15
src/main.rs
|
@ -15,6 +15,7 @@ use axum::{
|
||||||
},
|
},
|
||||||
middleware, Server,
|
middleware, Server,
|
||||||
};
|
};
|
||||||
|
use dotenv_rs::dotenv;
|
||||||
use figment::{
|
use figment::{
|
||||||
providers::{Env, Format, Serialized, Toml},
|
providers::{Env, Format, Serialized, Toml},
|
||||||
Figment,
|
Figment,
|
||||||
|
@ -22,10 +23,12 @@ use figment::{
|
||||||
use sqlx::postgres::PgPoolOptions;
|
use sqlx::postgres::PgPoolOptions;
|
||||||
use state::AppState;
|
use state::AppState;
|
||||||
use std::{net::SocketAddr, sync::Arc};
|
use std::{net::SocketAddr, sync::Arc};
|
||||||
use tower_http::cors::CorsLayer;
|
use tower_http::cors::{AllowOrigin, CorsLayer};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
|
dotenv().ok();
|
||||||
|
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
let config: Config = Figment::from(Serialized::defaults(Config::default()))
|
let config: Config = Figment::from(Serialized::defaults(Config::default()))
|
||||||
|
@ -34,13 +37,7 @@ async fn main() -> Result<()> {
|
||||||
.extract()?;
|
.extract()?;
|
||||||
let addr: SocketAddr = config.bind.parse()?;
|
let addr: SocketAddr = config.bind.parse()?;
|
||||||
|
|
||||||
let origins = config
|
let cors = CorsLayer::new()
|
||||||
.cors_domains
|
|
||||||
.split(',')
|
|
||||||
.map(|x| x.to_string().parse().unwrap())
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
println!("{:?}", origins);
|
|
||||||
let cors = CorsLayer::permissive()
|
|
||||||
.allow_methods([
|
.allow_methods([
|
||||||
Method::GET,
|
Method::GET,
|
||||||
Method::POST,
|
Method::POST,
|
||||||
|
@ -48,7 +45,7 @@ async fn main() -> Result<()> {
|
||||||
Method::DELETE,
|
Method::DELETE,
|
||||||
Method::OPTIONS,
|
Method::OPTIONS,
|
||||||
])
|
])
|
||||||
.allow_origin(origins)
|
.allow_origin(AllowOrigin::mirror_request())
|
||||||
.allow_credentials(true)
|
.allow_credentials(true)
|
||||||
.allow_headers([AUTHORIZATION, ACCEPT, CONTENT_TYPE])
|
.allow_headers([AUTHORIZATION, ACCEPT, CONTENT_TYPE])
|
||||||
.expose_headers([CONTENT_ENCODING]);
|
.expose_headers([CONTENT_ENCODING]);
|
||||||
|
|
|
@ -9,7 +9,6 @@ pub struct Config {
|
||||||
pub session_duration: i64, // in seconds
|
pub session_duration: i64, // in seconds
|
||||||
pub prune_interval: u64, // in seconds
|
pub prune_interval: u64, // in seconds
|
||||||
pub base_url: String,
|
pub base_url: String,
|
||||||
pub cors_domains: String, // CORS-allowed domains, separated by comma
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
@ -47,7 +46,6 @@ impl Default for Config {
|
||||||
session_duration: 3600, // 60min
|
session_duration: 3600, // 60min
|
||||||
prune_interval: 3600, // 60min
|
prune_interval: 3600, // 60min
|
||||||
base_url: "http://localhost".into(),
|
base_url: "http://localhost".into(),
|
||||||
cors_domains: "http://localhost:3000".into(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue