feat: separate olap oltp (#348)

This commit is contained in:
Sangamesh Kulkarni
2023-01-12 19:49:06 +05:30
committed by GitHub
parent 6a211d6c29
commit 8c5eab8499
14 changed files with 213 additions and 74 deletions

View File

@ -31,7 +31,7 @@ use routes::AppState;
pub use self::env::logger;
use crate::{
configs::settings::Settings,
configs::settings,
core::errors::{self, ApplicationResult},
};
@ -57,7 +57,8 @@ pub mod pii {
pub use masking::*;
}
pub fn mk_app(
#[cfg(feature = "olap")]
pub fn mk_olap_app(
state: AppState,
request_body_limit: usize,
) -> actix_web::App<
@ -68,6 +69,57 @@ pub fn mk_app(
Error = actix_web::Error,
InitError = (),
>,
> {
let application_builder = get_application_builder(request_body_limit);
let mut server_app = application_builder
.service(routes::Payments::olap_server(state.clone()))
.service(routes::Customers::olap_server(state.clone()))
.service(routes::Refunds::olap_server(state.clone()))
.service(routes::Payouts::olap_server(state.clone()))
.service(routes::MerchantAccount::olap_server(state.clone()))
.service(routes::MerchantConnectorAccount::olap_server(state.clone()));
#[cfg(feature = "stripe")]
{
server_app = server_app.service(routes::StripeApis::server(state.clone()));
}
server_app = server_app.service(routes::Health::olap_server(state));
server_app
}
/// Starts the OLAP server with only OLAP services
///
/// # Panics
///
/// Unwrap used because without the value we can't start the server
#[allow(clippy::expect_used, clippy::unwrap_used)]
#[cfg(feature = "olap")]
pub async fn start_olap_server(conf: settings::Settings) -> ApplicationResult<(Server, AppState)> {
logger::debug!(startup_config=?conf);
let server = conf.server.clone();
let state = routes::AppState::new(conf).await;
// Cloning to close connections before shutdown
let app_state = state.clone();
let request_body_limit = server.request_body_limit;
let server = actix_web::HttpServer::new(move || mk_olap_app(state.clone(), request_body_limit))
.bind((server.host.as_str(), server.port))?
.workers(server.workers.unwrap_or_else(num_cpus::get_physical))
.run();
Ok((server, app_state))
}
pub fn get_application_builder(
request_body_limit: usize,
) -> actix_web::App<
impl ServiceFactory<
ServiceRequest,
Config = (),
Response = actix_web::dev::ServiceResponse<impl MessageBody>,
Error = actix_web::Error,
InitError = (),
>,
> {
let json_cfg = actix_web::web::JsonConfig::default()
.limit(request_body_limit)
@ -75,7 +127,7 @@ pub fn mk_app(
.content_type(|mime| mime == mime::APPLICATION_JSON) // FIXME: This doesn't seem to be enforced.
.error_handler(utils::error_parser::custom_json_error_handler);
let mut server_app = actix_web::App::new()
actix_web::App::new()
.app_data(json_cfg)
.wrap(middleware::RequestId)
.wrap(router_env::tracing_actix_web::TracingLogger::default())
@ -88,36 +140,53 @@ pub fn mk_app(
errors::error_handlers::custom_error_handlers,
))
.wrap(cors::cors())
.service(routes::Payments::server(state.clone()))
.service(routes::Customers::server(state.clone()))
.service(routes::Refunds::server(state.clone()))
.service(routes::Payouts::server(state.clone()))
.service(routes::PaymentMethods::server(state.clone()))
.service(routes::MerchantAccount::server(state.clone()))
.service(routes::MerchantConnectorAccount::server(state.clone()))
.service(routes::EphemeralKey::server(state.clone()))
.service(routes::Webhooks::server(state.clone()));
}
pub fn mk_oltp_app(
state: AppState,
request_body_limit: usize,
) -> actix_web::App<
impl ServiceFactory<
ServiceRequest,
Config = (),
Response = actix_web::dev::ServiceResponse<impl MessageBody>,
Error = actix_web::Error,
InitError = (),
>,
> {
let application_builder = get_application_builder(request_body_limit);
let mut server_app = application_builder
.service(routes::Payments::oltp_server(state.clone()))
.service(routes::Customers::oltp_server(state.clone()))
.service(routes::Refunds::oltp_server(state.clone()))
.service(routes::Payouts::oltp_server(state.clone()))
.service(routes::PaymentMethods::oltp_server(state.clone()))
.service(routes::EphemeralKey::oltp_server(state.clone()))
.service(routes::Webhooks::oltp_server(state.clone()));
#[cfg(feature = "stripe")]
{
server_app = server_app.service(routes::StripeApis::server(state.clone()));
}
server_app = server_app.service(routes::Health::server(state));
server_app = server_app.service(routes::Health::oltp_server(state));
server_app
}
#[allow(clippy::expect_used, clippy::unwrap_used)]
/// Starts the OLTP server with only OLTP services
///
/// # Panics
///
/// Unwrap used because without the value we can't start the server
pub async fn start_server(conf: Settings) -> ApplicationResult<(Server, AppState)> {
#[allow(clippy::expect_used, clippy::unwrap_used)]
pub async fn start_oltp_server(conf: settings::Settings) -> ApplicationResult<(Server, AppState)> {
logger::debug!(startup_config=?conf);
let server = conf.server.clone();
let state = routes::AppState::new(conf).await;
// Cloning to close connections before shutdown
let app_state = state.clone();
let request_body_limit = server.request_body_limit;
let server = actix_web::HttpServer::new(move || mk_app(state.clone(), request_body_limit))
let server = actix_web::HttpServer::new(move || mk_oltp_app(state.clone(), request_body_limit))
.bind((server.host.as_str(), server.port))?
.workers(server.workers.unwrap_or_else(num_cpus::get_physical))
.run();