feat: Make some parts configurable via ENV

main
phga 2 years ago
parent 5d92bc6b2b
commit 216d04c4b8
Signed by: phga
GPG Key ID: 5249548AA705F019

@ -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

@ -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<T> = std::result::Result<T, Box<dyn Error>>;
/// 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!(

@ -13,7 +13,7 @@ pub struct TodoRepository {
impl TodoRepository {
pub fn new(session: Arc<Session>) -> 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} (

@ -43,7 +43,7 @@ pub struct UserRepository {
impl UserRepository {
pub fn new(session: Arc<Session>) -> 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} (

Loading…
Cancel
Save