From 216d04c4b87037712f70ba558e600049a1a264ff Mon Sep 17 00:00:00 2001 From: phga Date: Wed, 23 Nov 2022 23:40:15 +0100 Subject: [PATCH] feat: Make some parts configurable via ENV --- rust_solid_cassandra/backend/src/main.rs | 22 ++++++++++++++++--- rust_solid_cassandra/backend/src/repo.rs | 17 +++++++++----- .../backend/src/repo/todo_repository.rs | 2 +- .../backend/src/repo/user_repository.rs | 2 +- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/rust_solid_cassandra/backend/src/main.rs b/rust_solid_cassandra/backend/src/main.rs index d6a51e1..798cf03 100644 --- a/rust_solid_cassandra/backend/src/main.rs +++ b/rust_solid_cassandra/backend/src/main.rs @@ -1,4 +1,8 @@ -use std::{io, sync::Arc}; +use std::{ + env, io, + net::{SocketAddrV4}, + sync::Arc, +}; use actix_identity::IdentityMiddleware; use actix_session::{config::PersistentSession, storage::CookieSessionStore, SessionMiddleware}; @@ -20,6 +24,11 @@ use repo::user_repository::UserRepository; mod routes; use routes::{todo_routes, user_routes}; +/// Server address if none is given via ENV: WEBSERVER_ADDRESS +static DEFAULT_WEBSERVER_ADDRESS: &str = "127.0.0.1"; +/// Server port if none is given via ENV: WEBSERVER_PORT +static DEFAULT_WEBSERVER_PORT: &str = "6969"; + /// Starts the webserver and initializes required repos, etc. #[actix_web::main] async fn main() -> io::Result<()> { @@ -46,7 +55,14 @@ async fn main() -> io::Result<()> { // Secret key used to init session storage let key = Key::generate(); - log::info!("Starting HTTP server: http://127.0.0.1:6969"); + // External ENV params + let socket_addr: SocketAddrV4 = format!( + "{}:{}", + env::var("WEBSERVER_ADDRESS").unwrap_or(DEFAULT_WEBSERVER_ADDRESS.to_string()), + env::var("WEBSERVER_PORT").unwrap_or(DEFAULT_WEBSERVER_PORT.to_string()) + ).parse().expect("A valid socket address. Check your ENV variables!"); + + log::info!("Starting HTTP server: http://{}:{}", socket_addr.ip(), socket_addr.port()); HttpServer::new(move || { App::new() @@ -72,7 +88,7 @@ async fn main() -> io::Result<()> { .service(routes::delete_logout) .default_service(web::to(routes::index)) }) - .bind(("127.0.0.1", 6969))? + .bind(socket_addr)? .workers(2) // number of workers per bind default ist #cpus .run() .await diff --git a/rust_solid_cassandra/backend/src/repo.rs b/rust_solid_cassandra/backend/src/repo.rs index 8c881ee..635f4af 100644 --- a/rust_solid_cassandra/backend/src/repo.rs +++ b/rust_solid_cassandra/backend/src/repo.rs @@ -1,4 +1,4 @@ -use std::{env, thread::sleep, time::Duration, error::Error}; +use std::{env, error::Error, thread::sleep, time::Duration}; use cassandra_cpp::{stmt, Cluster, Session}; @@ -9,16 +9,21 @@ pub mod user_repository; type Result = std::result::Result>; /// Keyspace name if none is given via ENV: KEYSPACE_NAME -static DEFAULT_KEYSPACE_NAME: &str = "rust_solidjs_cassandra"; +static DEFAULT_CASSANDRA_KEYSPACE_NAME: &str = "rust_solidjs_cassandra"; +/// Server address if none is given via ENV: CASSANDRA_SERVER_ADDRESS +static DEFAULT_CASSANDRA_SERVER_ADDRESS: &str = "127.0.0.1"; /// Waits for the cassandra database to become available -> then returns a session. pub fn init() -> Session { - let keyspace_name = env::var("KEYSPACE_NAME").unwrap_or(DEFAULT_KEYSPACE_NAME.to_string()); + let keyspace_name = + env::var("CASSANDRA_KEYSPACE_NAME").unwrap_or(DEFAULT_CASSANDRA_KEYSPACE_NAME.to_string()); + let cassandra_server_addr = env::var("CASSANDRA_SERVER_ADDRESS") + .unwrap_or(DEFAULT_CASSANDRA_SERVER_ADDRESS.to_string()); // Definitely set it so other modules can use it - env::set_var("KEYSPACE_NAME", &keyspace_name); + env::set_var("CASSANDRA_KEYSPACE_NAME", &keyspace_name); let mut cluster = Cluster::default(); - cluster.set_contact_points("127.0.0.1").unwrap(); // Panic if not successful + cluster.set_contact_points(&cassandra_server_addr).unwrap(); // Panic if not successful let mut count = 0; let session = loop { match cluster.connect() { @@ -27,7 +32,7 @@ pub fn init() -> Session { count += 1; log::warn!("Could not connect to database! Retrying... ({count})"); sleep(Duration::new(5, 0)); - }, + } } }; let query = stmt!(&format!( diff --git a/rust_solid_cassandra/backend/src/repo/todo_repository.rs b/rust_solid_cassandra/backend/src/repo/todo_repository.rs index aaae3bf..d790a53 100644 --- a/rust_solid_cassandra/backend/src/repo/todo_repository.rs +++ b/rust_solid_cassandra/backend/src/repo/todo_repository.rs @@ -13,7 +13,7 @@ pub struct TodoRepository { impl TodoRepository { pub fn new(session: Arc) -> TodoRepository { let keyspace_name = - env::var("KEYSPACE_NAME").expect("Value should be definitely set in init"); + env::var("CASSANDRA_KEYSPACE_NAME").expect("Value should be definitely set in init"); let table = format!("{keyspace_name}.todo"); let query = stmt!(&format!( "CREATE TABLE IF NOT EXISTS {table} ( diff --git a/rust_solid_cassandra/backend/src/repo/user_repository.rs b/rust_solid_cassandra/backend/src/repo/user_repository.rs index e3b6b7a..6ea37a4 100644 --- a/rust_solid_cassandra/backend/src/repo/user_repository.rs +++ b/rust_solid_cassandra/backend/src/repo/user_repository.rs @@ -43,7 +43,7 @@ pub struct UserRepository { impl UserRepository { pub fn new(session: Arc) -> UserRepository { let keyspace_name = - env::var("KEYSPACE_NAME").expect("Value should be definitely set in init"); + env::var("CASSANDRA_KEYSPACE_NAME").expect("Value should be definitely set in init"); let table = format!("{keyspace_name}.user"); let query = stmt!(&format!( "CREATE TABLE IF NOT EXISTS {table} (