diff --git a/services/blob/src/config.rs b/services/blob/src/config.rs --- a/services/blob/src/config.rs +++ b/services/blob/src/config.rs @@ -1,5 +1,5 @@ use aws_sdk_dynamodb::Region; -use clap::{builder::FalseyValueParser, Parser}; +use clap::{builder::FalseyValueParser, Parser, ValueEnum}; use once_cell::sync::Lazy; use tracing::info; @@ -7,9 +7,18 @@ AWS_REGION, DEFAULT_LISTEN_PORT, LOCALSTACK_URL, SANDBOX_ENV_VAR, }; +#[derive(ValueEnum, Clone, Debug)] +pub enum Protocol { + Grpc, + Http, +} + #[derive(Parser)] #[command(version, about, long_about = None)] pub struct AppConfig { + /// Protocol to use for the service server + #[arg(long, value_enum, default_value_t = Protocol::Grpc)] + pub protocol: Protocol, /// HTTP/gRPC server listening port #[arg(long = "port", default_value_t = DEFAULT_LISTEN_PORT)] pub listen_port: u16, diff --git a/services/blob/src/http/mod.rs b/services/blob/src/http/mod.rs new file mode 100644 --- /dev/null +++ b/services/blob/src/http/mod.rs @@ -0,0 +1,33 @@ +use crate::config::CONFIG; +use crate::database::DatabaseClient; +use crate::s3::S3Client; + +use actix_web::{web, App, HttpServer}; +use anyhow::Result; +use tracing::info; + +async fn hello_handler() -> impl actix_web::Responder { + "Hello, world!" +} + +// disable unused warning for now +#[allow(unused)] +pub async fn run_http_server( + db_client: DatabaseClient, + s3_client: S3Client, +) -> Result<()> { + info!( + "Starting HTTP server listening at port {}", + CONFIG.listen_port + ); + HttpServer::new(move || { + App::new() + .wrap(tracing_actix_web::TracingLogger::default()) + .service(web::resource("/hello").route(web::get().to(hello_handler))) + }) + .bind(("0.0.0.0", CONFIG.listen_port))? + .run() + .await?; + + Ok(()) +} diff --git a/services/blob/src/main.rs b/services/blob/src/main.rs --- a/services/blob/src/main.rs +++ b/services/blob/src/main.rs @@ -2,10 +2,12 @@ pub mod constants; pub mod database; pub mod grpc; +pub mod http; pub mod s3; pub mod tools; use anyhow::Result; +use config::Protocol; use tracing_subscriber::filter::{EnvFilter, LevelFilter}; fn configure_logging() -> Result<()> { @@ -28,5 +30,8 @@ let db = database::DatabaseClient::new(&aws_config); let s3 = s3::S3Client::new(&aws_config); - crate::grpc::run_grpc_server(db, s3).await + match &config::CONFIG.protocol { + Protocol::Http => crate::http::run_http_server(db, s3).await, + Protocol::Grpc => crate::grpc::run_grpc_server(db, s3).await, + } }