diff --git a/docker-compose.yml b/docker-compose.yml index c03b3a1..af547ba 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: '3' +version: "3" services: client: container_name: fast-insiders-client @@ -6,7 +6,7 @@ services: build: context: . args: - API_URL: http://localhost:8000/v1/ + API_URL: http://localhost:8000/ dockerfile: ./client/Dockerfile restart: always ports: @@ -19,7 +19,7 @@ services: build: context: . args: - API_URL: http://localhost:8000/v1/ + API_URL: http://localhost:8000/ dockerfile: ./server/Dockerfile restart: always ports: @@ -44,9 +44,7 @@ services: - MYSQL_DATABASE=fast_insiders - MYSQL_USER=fiuser - volumes: fi-data: driver: local db: - diff --git a/server/Dockerfile b/server/Dockerfile index c58beec..2b27ca4 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -1,8 +1,8 @@ FROM rust:1.66-slim as build # Install build dependencies -RUN apt update \ - && apt install -y --no-install-recommends lsb-release apt-transport-https \ +RUN apt-get update \ + && apt-get install -y --no-install-recommends lsb-release apt-transport-https \ build-essential curl wget pkg-config libssl-dev # Root of the project @@ -21,20 +21,23 @@ RUN rustup default nightly ARG API_URL ENV API_URL=$API_URL +# go to src dir +WORKDIR /app/server + # Build the final binary -RUN cargo build --release --bin server +RUN cargo build --release # prepare deployment image FROM debian:stable-slim # For tls to work -RUN apt-get update && apt-get -y install ca-certificates libssl-dev && rm -rf /var/lib/apt/lists/* +RUN apt-get update && apt-get -y --no-install-recommends install ca-certificates libssl-dev && rm -rf /var/lib/apt/lists/* WORKDIR /app COPY --from=build /app/target/release/server /app/ -ENV ROCKET_ADDRESS=0.0.0.0 +ENV HOST=0.0.0.0 -CMD ./server +CMD ["./server"] diff --git a/server/src/env.rs b/server/src/env.rs index ac0d351..98fda18 100644 --- a/server/src/env.rs +++ b/server/src/env.rs @@ -1,7 +1,13 @@ +use std::net::SocketAddr; + use serde::Deserialize; #[derive(Deserialize, Debug)] pub struct Env { + #[serde(default = "host_default")] + pub host: String, + #[serde(default = "port_default")] + pub port: String, pub mysql_user: String, pub mysql_password: String, pub mysql_host: String, @@ -31,6 +37,14 @@ pub struct Env { pub get_amf_transaction_interval: u64, } +fn host_default() -> String { + "127.0.0.1".to_string() +} + +fn port_default() -> String { + "8000".to_string() +} + fn mysql_port_default() -> String { "3306".to_string() } @@ -92,6 +106,7 @@ impl Env { #[derive(Debug)] pub struct Config { + pub server_address: SocketAddr, pub database_url: String, pub max_connections: u32, pub min_connections: u32, @@ -113,7 +128,12 @@ impl Config { env.mysql_user, env.mysql_password, env.mysql_host, env.mysql_port, env.mysql_database ); + let server_address = format!("{}:{}", env.host, env.port) + .parse() + .expect("Could not parse host and port combination into a valid server address"); + let mut config = Config { + server_address, database_url, max_connections: env.max_connections, min_connections: env.min_connections, diff --git a/server/src/main.rs b/server/src/main.rs index 8407c56..acf6b65 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -16,7 +16,7 @@ use axum::{ Router, }; use sea_orm::DatabaseConnection; -use std::{net::SocketAddr, time::Duration}; +use std::time::Duration; use tokio::signal; use tower_http::{classify::ServerErrorsFailureClass, cors::CorsLayer, trace::TraceLayer}; use tracing::{info, info_span, Span}; @@ -136,7 +136,8 @@ pub async fn main() -> Result<(), Box> { // Run tasks tokio::task::spawn(async move { run_tasks(&shared_state.db).await }); - let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + let addr = CONFIG.server_address; + info!("Server will start listening on {}", addr); axum::Server::bind(&addr) .serve(app.into_make_service()) .with_graceful_shutdown(shutdown_signal())