Compare commits

..

2 commits

7 changed files with 44 additions and 10 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,8 @@ 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>,
pub port: String,
} }
impl Env { impl Env {
@ -10,8 +12,19 @@ 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();
let port = env::var("PORT")
.unwrap_or_else(|_| "3000".to_string());
Env { Env {
postgres_connection_string, postgres_connection_string,
allowed_origins,
port,
} }
} }
} }

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)
} }

View file

@ -12,9 +12,11 @@ async fn main() {
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); let state = AppState::new().await;
let port = &state.env.port;
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{port}")).await.unwrap();
tracing::info!("listening on {}", listener.local_addr().unwrap()); tracing::info!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, upvoters::app(AppState::new().await)) axum::serve(listener, upvoters::app(state))
.await .await
.unwrap(); .unwrap();
} }

View file

@ -3,7 +3,7 @@ pub mod db {
use sqlx::PgPool; use sqlx::PgPool;
const DEFAULT_CONNECTION_STRING: &str = const DEFAULT_CONNECTION_STRING: &str =
"postgres://uprs:password123@localhost:5432/uprs"; "postgres://upvoters:password123@localhost:5432/upvoters";
pub async fn test_pool() -> PgPool { pub async fn test_pool() -> PgPool {
let conn = std::env::var("POSTGRES_CONNECTION_STRING") let conn = std::env::var("POSTGRES_CONNECTION_STRING")

View file

@ -141,6 +141,8 @@ mod tests {
db, db,
env: Env { env: Env {
postgres_connection_string: String::new(), postgres_connection_string: String::new(),
allowed_origins: vec![],
port: "3000".to_string(),
}, },
} }
} }