From c8d43028634fa10636d893e749217e075aad7d16 Mon Sep 17 00:00:00 2001 From: Sphereso Date: Tue, 1 Apr 2025 11:25:27 +0200 Subject: [PATCH] added 'serve' feature --- .env.example | 9 +++++++- .vscode/settings.json | 4 ++++ crates/backend/Cargo.toml | 3 +++ crates/backend/src/main.rs | 43 ++++++++++++++++++++++++++++---------- 4 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.env.example b/.env.example index a9091ca..fe63267 100644 --- a/.env.example +++ b/.env.example @@ -1,7 +1,14 @@ +# Postgres section DB_NAME= DB_USER= DB_PASSWORD= DB_HOST= DB_PORT= -DATABASE_URL=postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:{DB_PORT}/${DB_NAME} \ No newline at end of file +DATABASE_URL=postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:{DB_PORT}/${DB_NAME} + +# Redis section +REDIS_HOST= +REDIS_PORT= +SECRET_KEY= + diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..949809d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "rust-analyzer.check.features": "all", + "rust-analyzer.cargo.features": "all" +} \ No newline at end of file diff --git a/crates/backend/Cargo.toml b/crates/backend/Cargo.toml index b36e09b..310a889 100644 --- a/crates/backend/Cargo.toml +++ b/crates/backend/Cargo.toml @@ -19,3 +19,6 @@ serde = {version = "1", features = ["derive"]} sea-orm ={ version = "1.1", features = ["sqlx-postgres", "runtime-tokio-rustls", "macros"]} dotenvy = "0.15" + +[features] +serve = [] diff --git a/crates/backend/src/main.rs b/crates/backend/src/main.rs index 4e203ec..d57ae7c 100644 --- a/crates/backend/src/main.rs +++ b/crates/backend/src/main.rs @@ -1,3 +1,4 @@ +use actix_files::NamedFile; use actix_session::{storage::RedisSessionStore, SessionMiddleware}; use actix_web::{cookie::Key, middleware::Logger, web, App, HttpResponse, HttpServer}; use db::Database; @@ -17,16 +18,29 @@ async fn main() -> std::io::Result<()> { let redis_conn = connect_to_redis_database().await; + // use dotenvy here to get SECRET_KEY + let secret_key = Key::generate(); + println!("Secret Key {:?}", secret_key.master()); + HttpServer::new(move || { - App::new() + let app = App::new() .app_data(web::Data::new(database.clone())) .wrap(Logger::default()) .wrap(SessionMiddleware::new( redis_conn.clone(), - // use dotenvy here to get SECRET_KEY (if we want to have the same on every startup) - Key::generate(), + secret_key.clone(), )) - .configure(controller::register_controllers) + .configure(controller::register_controllers); + + #[cfg(feature = "serve")] + let app = { + println!("running serve"); + app.default_service( + web::get().to(async || NamedFile::open_async("./web/index.html").await), + ) + }; + + app }) .bind(("0.0.0.0", 8080))? .run() @@ -34,8 +48,11 @@ async fn main() -> std::io::Result<()> { } async fn connect_to_redis_database() -> RedisSessionStore { - // Build the redis Connection from the .env file Variables - let redis_connection_string = "redis://127.0.0.1:6379"; + let redis_host = dotenvy::var("REDIS_HOST").expect("REDIS_HOST must be set in .env"); + let redis_port = dotenvy::var("REDIS_PORT") + .map(|x| x.parse::().expect("REDIS_PORT is not a valid port")) + .unwrap_or(6379); + let redis_connection_string = format!("redis://{}:{}", redis_host, redis_port); let store = RedisSessionStore::new(redis_connection_string) .await .unwrap(); @@ -43,13 +60,17 @@ async fn connect_to_redis_database() -> RedisSessionStore { return store; } - fn build_database_url() -> String { let db_user = dotenvy::var("DB_USER").unwrap_or("pgg".to_owned()); let db_name = dotenvy::var("DB_NAME").unwrap_or("pgg".to_owned()); let db_password = dotenvy::var("DB_PASSWORD").unwrap_or("pgg".to_owned()); - let db_host = dotenvy::var("DB_HOST").expect("DB_HOST must be set"); - let db_port = dotenvy::var("DB_PORT").map(|x| x.parse::().expect("DB_PORT is not a valid port")).unwrap_or(5432); + let db_host = dotenvy::var("DB_HOST").expect("DB_HOST must be set in .env"); + let db_port = dotenvy::var("DB_PORT") + .map(|x| x.parse::().expect("DB_PORT is not a valid port")) + .unwrap_or(5432); - format!("postgresql://{}:{}@{}:{}/{}", db_user, db_password, db_host, db_port, db_name) -} \ No newline at end of file + format!( + "postgresql://{}:{}@{}:{}/{}", + db_user, db_password, db_host, db_port, db_name + ) +}