Configure CORS

This commit is contained in:
Alex Selimov 2026-03-19 23:37:03 -04:00
parent e5d0219df8
commit 44c9f0e705
4 changed files with 32 additions and 7 deletions

View file

@ -20,7 +20,7 @@ sqlx = { version = "0.8.6", features = [
] } ] }
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
tower = "0.5" tower = "0.5"
tower-http = { version = "0.6", features = ["trace"] } tower-http = { version = "0.6", features = ["trace", "cors"] }
tracing = "0.1" tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] } tracing-subscriber = { version = "0.3", features = ["env-filter"] }
uuid = { version = "1.22.0", features = ["v4"] } uuid = { version = "1.22.0", features = ["v4"] }

View file

@ -1,12 +1,12 @@
services: services:
db: db:
image: postgres:16 image: postgres:16
container_name: uprs_db container_name: upvoters_db
restart: always restart: always
environment: environment:
POSTGRES_USER: uprs POSTGRES_USER: upvoters
POSTGRES_PASSWORD: password123 POSTGRES_PASSWORD: password123
POSTGRES_DB: uprs POSTGRES_DB: upvoters
volumes: volumes:
- pgdata:/var/lib/postgresql/data - pgdata:/var/lib/postgresql/data
- ./db/migrations:/docker-entrypoint-initdb.d # run initial schema - ./db/migrations:/docker-entrypoint-initdb.d # run initial schema
@ -15,10 +15,11 @@ services:
app: app:
build: . build: .
container_name: uprs_app container_name: upvoters_app
restart: always restart: always
environment: environment:
POSTGRES_CONNECTION_STRING: postgres://uprs:password123@db:5432/uprs POSTGRES_CONNECTION_STRING: postgres://upvoters:password123@db:5432/upvoters
ALLOWED_ORIGINS: http://localhost:1313
ports: ports:
- "3000:3000" - "3000:3000"
depends_on: depends_on:

View file

@ -3,6 +3,7 @@ use std::env;
#[derive(Clone)] #[derive(Clone)]
pub struct Env { pub struct Env {
pub postgres_connection_string: String, pub postgres_connection_string: String,
pub allowed_origins: Vec<String>,
} }
impl Env { impl Env {
@ -10,8 +11,15 @@ impl Env {
let postgres_connection_string = env::var("POSTGRES_CONNECTION_STRING") let postgres_connection_string = env::var("POSTGRES_CONNECTION_STRING")
.expect("Missing POSTGRES_CONNECTION_STRING as an environment variable"); .expect("Missing POSTGRES_CONNECTION_STRING as an environment variable");
let allowed_origins = env::var("ALLOWED_ORIGINS")
.expect("Missing ALLOWED_ORIGINS as an environment variable")
.split(',')
.map(|s| s.trim().to_string())
.collect();
Env { Env {
postgres_connection_string, postgres_connection_string,
allowed_origins,
} }
} }
} }

View file

@ -5,9 +5,25 @@ pub mod test_helpers;
pub mod votes; pub mod votes;
use axum::Router; use axum::Router;
use axum::http::{HeaderValue, Method, header};
use state::AppState; use state::AppState;
use tower_http::cors::CorsLayer;
use tower_http::trace::TraceLayer; use tower_http::trace::TraceLayer;
pub fn app(state: AppState) -> Router { pub fn app(state: AppState) -> Router {
routes::router(state).layer(TraceLayer::new_for_http()) let origins: Vec<HeaderValue> = state
.env
.allowed_origins
.iter()
.map(|o| o.parse().expect("Invalid origin in ALLOWED_ORIGINS"))
.collect();
let cors = CorsLayer::new()
.allow_origin(origins)
.allow_methods([Method::GET, Method::POST, Method::DELETE])
.allow_headers([header::CONTENT_TYPE])
.allow_credentials(true);
routes::router(state)
.layer(TraceLayer::new_for_http())
.layer(cors)
} }