Changeset View
Changeset View
Standalone View
Standalone View
services/blob/src/http/mod.rs
use crate::config::CONFIG; | use crate::config::CONFIG; | ||||
use crate::database::DatabaseClient; | use crate::database::DatabaseClient; | ||||
use crate::s3::S3Client; | use crate::s3::S3Client; | ||||
use actix_cors::Cors; | |||||
use actix_web::{web, App, HttpServer}; | use actix_web::{web, App, HttpServer}; | ||||
use anyhow::Result; | use anyhow::Result; | ||||
use tracing::info; | use tracing::info; | ||||
mod context; | mod context; | ||||
use context::AppContext; | use context::AppContext; | ||||
mod handlers { | mod handlers { | ||||
pub(super) mod blob; | pub(super) mod blob; | ||||
// convenience exports to be used in handlers | // convenience exports to be used in handlers | ||||
use super::context::{handle_db_error, AppContext}; | use super::context::{handle_db_error, AppContext}; | ||||
} | } | ||||
fn cors_config() -> Cors { | |||||
if CONFIG.is_sandbox { | |||||
jon: not directly related to this diff, but I would like to move away from `--sandbox`, and instead… | |||||
// All origins, methods, request headers and exposed headers allowed. | |||||
// Credentials supported. Max age 1 hour. Does not send wildcard. | |||||
return Cors::permissive(); | |||||
} | |||||
Cors::default() | |||||
.allowed_origin("https://web.comm.app") | |||||
// for local development using prod service | |||||
.allowed_origin("http://localhost:3000") | |||||
.allowed_methods(vec!["GET", "POST", "PUT", "DELETE", "OPTIONS"]) | |||||
.allow_any_header() | |||||
.expose_any_header() | |||||
} | |||||
pub async fn run_http_server( | pub async fn run_http_server( | ||||
db_client: DatabaseClient, | db_client: DatabaseClient, | ||||
s3_client: S3Client, | s3_client: S3Client, | ||||
) -> Result<()> { | ) -> Result<()> { | ||||
info!( | info!( | ||||
"Starting HTTP server listening at port {}", | "Starting HTTP server listening at port {}", | ||||
CONFIG.http_port | CONFIG.http_port | ||||
); | ); | ||||
HttpServer::new(move || { | HttpServer::new(move || { | ||||
// context that is passed to every handler | // context that is passed to every handler | ||||
let ctx = AppContext { | let ctx = AppContext { | ||||
db: db_client.to_owned(), | db: db_client.to_owned(), | ||||
s3: s3_client.to_owned(), | s3: s3_client.to_owned(), | ||||
}; | }; | ||||
App::new() | App::new() | ||||
.wrap(tracing_actix_web::TracingLogger::default()) | .wrap(tracing_actix_web::TracingLogger::default()) | ||||
.wrap(cors_config()) | |||||
.app_data(web::Data::new(ctx)) | .app_data(web::Data::new(ctx)) | ||||
.service( | .service( | ||||
web::resource("/blob/{holder}") | web::resource("/blob/{holder}") | ||||
.route(web::get().to(handlers::blob::get_blob_handler)) | .route(web::get().to(handlers::blob::get_blob_handler)) | ||||
.route(web::delete().to(handlers::blob::delete_blob_handler)), | .route(web::delete().to(handlers::blob::delete_blob_handler)), | ||||
) | ) | ||||
.service( | .service( | ||||
web::resource("/blob") | web::resource("/blob") | ||||
Show All 10 Lines |
not directly related to this diff, but I would like to move away from --sandbox, and instead just see if the binary is in debug mode.
Also gets rid of a bunch of branching logic