Compare commits
No commits in common. "a3b201fa098319b4b16af7006a357a121fe4f768" and "e5d0219df8da6d83b97f5b61eee679c1cfd603e3" have entirely different histories.
a3b201fa09
...
e5d0219df8
7 changed files with 10 additions and 44 deletions
|
|
@ -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", "cors"] }
|
tower-http = { version = "0.6", features = ["trace"] }
|
||||||
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"] }
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
services:
|
services:
|
||||||
db:
|
db:
|
||||||
image: postgres:16
|
image: postgres:16
|
||||||
container_name: upvoters_db
|
container_name: uprs_db
|
||||||
restart: always
|
restart: always
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: upvoters
|
POSTGRES_USER: uprs
|
||||||
POSTGRES_PASSWORD: password123
|
POSTGRES_PASSWORD: password123
|
||||||
POSTGRES_DB: upvoters
|
POSTGRES_DB: uprs
|
||||||
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,11 +15,10 @@ services:
|
||||||
|
|
||||||
app:
|
app:
|
||||||
build: .
|
build: .
|
||||||
container_name: upvoters_app
|
container_name: uprs_app
|
||||||
restart: always
|
restart: always
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_CONNECTION_STRING: postgres://upvoters:password123@db:5432/upvoters
|
POSTGRES_CONNECTION_STRING: postgres://uprs:password123@db:5432/uprs
|
||||||
ALLOWED_ORIGINS: http://localhost:1313
|
|
||||||
ports:
|
ports:
|
||||||
- "3000:3000"
|
- "3000:3000"
|
||||||
depends_on:
|
depends_on:
|
||||||
|
|
|
||||||
13
src/env.rs
13
src/env.rs
|
|
@ -3,8 +3,6 @@ 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 {
|
||||||
|
|
@ -12,19 +10,8 @@ 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,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
18
src/lib.rs
18
src/lib.rs
|
|
@ -5,25 +5,9 @@ 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 {
|
||||||
let origins: Vec<HeaderValue> = state
|
routes::router(state).layer(TraceLayer::new_for_http())
|
||||||
.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)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,11 +12,9 @@ async fn main() {
|
||||||
.with(tracing_subscriber::fmt::layer())
|
.with(tracing_subscriber::fmt::layer())
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
let state = AppState::new().await;
|
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||||
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(state))
|
axum::serve(listener, upvoters::app(AppState::new().await))
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ pub mod db {
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
|
||||||
const DEFAULT_CONNECTION_STRING: &str =
|
const DEFAULT_CONNECTION_STRING: &str =
|
||||||
"postgres://upvoters:password123@localhost:5432/upvoters";
|
"postgres://uprs:password123@localhost:5432/uprs";
|
||||||
|
|
||||||
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")
|
||||||
|
|
|
||||||
|
|
@ -141,8 +141,6 @@ 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(),
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue