mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-02 21:07:58 +08:00
feat: separate olap oltp (#348)
This commit is contained in:
committed by
GitHub
parent
6a211d6c29
commit
8c5eab8499
@ -18,6 +18,14 @@ port = 5432
|
|||||||
dbname = "hyperswitch_db"
|
dbname = "hyperswitch_db"
|
||||||
pool_size = 5
|
pool_size = 5
|
||||||
|
|
||||||
|
[replica_database]
|
||||||
|
username = "replica_user"
|
||||||
|
password = "replica_pass"
|
||||||
|
host = "localhost"
|
||||||
|
port = 5432
|
||||||
|
dbname = "hyperswitch_db"
|
||||||
|
pool_size = 5
|
||||||
|
|
||||||
[proxy]
|
[proxy]
|
||||||
|
|
||||||
[keys]
|
[keys]
|
||||||
|
|||||||
@ -26,8 +26,8 @@ pool_size = 5 # Number of connections to keep open
|
|||||||
|
|
||||||
# Replica SQL data store credentials
|
# Replica SQL data store credentials
|
||||||
[replica_database]
|
[replica_database]
|
||||||
username = "db_user" # DB Username
|
username = "replica_user" # DB Username
|
||||||
password = "db_pass" # DB Password
|
password = "replica_pass" # DB Password
|
||||||
host = "localhost" # DB Host
|
host = "localhost" # DB Host
|
||||||
port = 5432 # DB Port
|
port = 5432 # DB Port
|
||||||
dbname = "hyperswitch_db" # Name of Database
|
dbname = "hyperswitch_db" # Name of Database
|
||||||
|
|||||||
@ -9,7 +9,6 @@ use structopt::StructOpt;
|
|||||||
async fn main() -> ApplicationResult<()> {
|
async fn main() -> ApplicationResult<()> {
|
||||||
// get commandline config before initializing config
|
// get commandline config before initializing config
|
||||||
let cmd_line = CmdLineConf::from_args();
|
let cmd_line = CmdLineConf::from_args();
|
||||||
|
|
||||||
if let Some(Subcommand::GenerateOpenapiSpec) = cmd_line.subcommand {
|
if let Some(Subcommand::GenerateOpenapiSpec) = cmd_line.subcommand {
|
||||||
let file_path = "openapi/generated.json";
|
let file_path = "openapi/generated.json";
|
||||||
#[allow(clippy::expect_used)]
|
#[allow(clippy::expect_used)]
|
||||||
@ -33,7 +32,14 @@ async fn main() -> ApplicationResult<()> {
|
|||||||
logger::info!("Application started [{:?}] [{:?}]", conf.server, conf.log);
|
logger::info!("Application started [{:?}] [{:?}]", conf.server, conf.log);
|
||||||
|
|
||||||
#[allow(clippy::expect_used)]
|
#[allow(clippy::expect_used)]
|
||||||
let (server, mut state) = router::start_server(conf)
|
#[cfg(not(feature = "olap"))]
|
||||||
|
let (server, mut state) = router::start_oltp_server(conf)
|
||||||
|
.await
|
||||||
|
.expect("Failed to create the server");
|
||||||
|
|
||||||
|
#[allow(clippy::expect_used)]
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
|
let (server, mut state) = router::start_olap_server(conf)
|
||||||
.await
|
.await
|
||||||
.expect("Failed to create the server");
|
.expect("Failed to create the server");
|
||||||
|
|
||||||
|
|||||||
@ -331,6 +331,7 @@ pub async fn payment_intents_cancel(
|
|||||||
|
|
||||||
#[instrument(skip_all)]
|
#[instrument(skip_all)]
|
||||||
#[get("/list")]
|
#[get("/list")]
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
pub async fn payment_intent_list(
|
pub async fn payment_intent_list(
|
||||||
state: web::Data<routes::AppState>,
|
state: web::Data<routes::AppState>,
|
||||||
req: HttpRequest,
|
req: HttpRequest,
|
||||||
|
|||||||
@ -19,7 +19,6 @@ use self::{
|
|||||||
flows::{ConstructFlowSpecificData, Feature},
|
flows::{ConstructFlowSpecificData, Feature},
|
||||||
operations::{BoxedOperation, Operation},
|
operations::{BoxedOperation, Operation},
|
||||||
};
|
};
|
||||||
use super::errors::StorageErrorExt;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
core::errors::{self, RouterResponse, RouterResult},
|
core::errors::{self, RouterResponse, RouterResult},
|
||||||
db::StorageInterface,
|
db::StorageInterface,
|
||||||
@ -30,7 +29,6 @@ use crate::{
|
|||||||
types::{
|
types::{
|
||||||
self, api,
|
self, api,
|
||||||
storage::{self, enums as storage_enums},
|
storage::{self, enums as storage_enums},
|
||||||
transformers::ForeignInto,
|
|
||||||
},
|
},
|
||||||
utils::OptionExt,
|
utils::OptionExt,
|
||||||
};
|
};
|
||||||
@ -541,6 +539,7 @@ pub fn should_call_connector<Op: Debug, F: Clone>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
pub async fn list_payments(
|
pub async fn list_payments(
|
||||||
db: &dyn StorageInterface,
|
db: &dyn StorageInterface,
|
||||||
merchant: storage::MerchantAccount,
|
merchant: storage::MerchantAccount,
|
||||||
@ -551,11 +550,16 @@ pub async fn list_payments(
|
|||||||
let payment_intent =
|
let payment_intent =
|
||||||
helpers::filter_by_constraints(db, &constraints, merchant_id, merchant.storage_scheme)
|
helpers::filter_by_constraints(db, &constraints, merchant_id, merchant.storage_scheme)
|
||||||
.await
|
.await
|
||||||
.map_err(|err| err.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound))?;
|
.map_err(|err| {
|
||||||
|
errors::StorageErrorExt::to_not_found_response(
|
||||||
|
err,
|
||||||
|
errors::ApiErrorResponse::PaymentNotFound,
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
let data: Vec<api::PaymentsResponse> = payment_intent
|
let data: Vec<api::PaymentsResponse> = payment_intent
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(ForeignInto::foreign_into)
|
.map(types::transformers::ForeignInto::foreign_into)
|
||||||
.collect();
|
.collect();
|
||||||
Ok(services::ApplicationResponse::Json(
|
Ok(services::ApplicationResponse::Json(
|
||||||
api::PaymentListResponse {
|
api::PaymentListResponse {
|
||||||
|
|||||||
@ -1053,6 +1053,7 @@ where
|
|||||||
Some(func(option1?, option2?))
|
Some(func(option1?, option2?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
pub(super) async fn filter_by_constraints(
|
pub(super) async fn filter_by_constraints(
|
||||||
db: &dyn StorageInterface,
|
db: &dyn StorageInterface,
|
||||||
constraints: &api::PaymentListConstraints,
|
constraints: &api::PaymentListConstraints,
|
||||||
@ -1065,6 +1066,7 @@ pub(super) async fn filter_by_constraints(
|
|||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
pub(super) fn validate_payment_list_request(
|
pub(super) fn validate_payment_list_request(
|
||||||
req: &api::PaymentListConstraints,
|
req: &api::PaymentListConstraints,
|
||||||
) -> CustomResult<(), errors::ApiErrorResponse> {
|
) -> CustomResult<(), errors::ApiErrorResponse> {
|
||||||
|
|||||||
@ -476,6 +476,7 @@ pub async fn validate_and_create_refund(
|
|||||||
/// If payment-id is not provided, lists the refunds associated with that particular merchant - to the limit specified,if no limits given, it is 10 by default
|
/// If payment-id is not provided, lists the refunds associated with that particular merchant - to the limit specified,if no limits given, it is 10 by default
|
||||||
|
|
||||||
#[instrument(skip_all)]
|
#[instrument(skip_all)]
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
pub async fn refund_list(
|
pub async fn refund_list(
|
||||||
db: &dyn db::StorageInterface,
|
db: &dyn db::StorageInterface,
|
||||||
merchant_account: storage::merchant_account::MerchantAccount,
|
merchant_account: storage::merchant_account::MerchantAccount,
|
||||||
|
|||||||
@ -1,10 +1,9 @@
|
|||||||
use super::MockDb;
|
use super::MockDb;
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
|
use crate::types::api;
|
||||||
use crate::{
|
use crate::{
|
||||||
core::errors::{self, CustomResult},
|
core::errors::{self, CustomResult},
|
||||||
types::{
|
types::storage::{self as types, enums},
|
||||||
api,
|
|
||||||
storage::{self as types, enums},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
@ -29,6 +28,7 @@ pub trait PaymentIntentInterface {
|
|||||||
storage_scheme: enums::MerchantStorageScheme,
|
storage_scheme: enums::MerchantStorageScheme,
|
||||||
) -> CustomResult<types::PaymentIntent, errors::StorageError>;
|
) -> CustomResult<types::PaymentIntent, errors::StorageError>;
|
||||||
|
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
async fn filter_payment_intent_by_constraints(
|
async fn filter_payment_intent_by_constraints(
|
||||||
&self,
|
&self,
|
||||||
merchant_id: &str,
|
merchant_id: &str,
|
||||||
@ -44,15 +44,17 @@ mod storage {
|
|||||||
use redis_interface::{HsetnxReply, RedisEntryId};
|
use redis_interface::{HsetnxReply, RedisEntryId};
|
||||||
|
|
||||||
use super::PaymentIntentInterface;
|
use super::PaymentIntentInterface;
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
|
use crate::types::api;
|
||||||
use crate::{
|
use crate::{
|
||||||
connection::pg_connection,
|
connection::pg_connection,
|
||||||
core::errors::{self, CustomResult},
|
core::errors::{self, CustomResult},
|
||||||
services::Store,
|
services::Store,
|
||||||
types::{
|
types::storage::{enums, kv, payment_intent::*},
|
||||||
api,
|
utils::{
|
||||||
storage::{enums, kv, payment_intent::*},
|
self,
|
||||||
|
storage_partitioning::{self, KvStorePartition},
|
||||||
},
|
},
|
||||||
utils::{self, storage_partitioning::KvStorePartition},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
@ -110,13 +112,14 @@ mod storage {
|
|||||||
insertable: kv::Insertable::PaymentIntent(new),
|
insertable: kv::Insertable::PaymentIntent(new),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
let stream_name = self.get_drainer_stream_name(&PaymentIntent::shard_key(
|
let stream_name =
|
||||||
crate::utils::storage_partitioning::PartitionKey::MerchantIdPaymentId {
|
self.get_drainer_stream_name(&PaymentIntent::shard_key(
|
||||||
merchant_id: &created_intent.merchant_id,
|
storage_partitioning::PartitionKey::MerchantIdPaymentId {
|
||||||
payment_id: &created_intent.payment_id,
|
merchant_id: &created_intent.merchant_id,
|
||||||
},
|
payment_id: &created_intent.payment_id,
|
||||||
self.config.drainer_num_partitions,
|
},
|
||||||
));
|
self.config.drainer_num_partitions,
|
||||||
|
));
|
||||||
self.redis_conn
|
self.redis_conn
|
||||||
.stream_append_entry(
|
.stream_append_entry(
|
||||||
&stream_name,
|
&stream_name,
|
||||||
@ -179,12 +182,13 @@ mod storage {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let stream_name = self.get_drainer_stream_name(&PaymentIntent::shard_key(
|
let stream_name = self.get_drainer_stream_name(&PaymentIntent::shard_key(
|
||||||
crate::utils::storage_partitioning::PartitionKey::MerchantIdPaymentId {
|
storage_partitioning::PartitionKey::MerchantIdPaymentId {
|
||||||
merchant_id: &updated_intent.merchant_id,
|
merchant_id: &updated_intent.merchant_id,
|
||||||
payment_id: &updated_intent.payment_id,
|
payment_id: &updated_intent.payment_id,
|
||||||
},
|
},
|
||||||
self.config.drainer_num_partitions,
|
self.config.drainer_num_partitions,
|
||||||
));
|
));
|
||||||
|
|
||||||
self.redis_conn
|
self.redis_conn
|
||||||
.stream_append_entry(
|
.stream_append_entry(
|
||||||
&stream_name,
|
&stream_name,
|
||||||
@ -236,6 +240,7 @@ mod storage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
async fn filter_payment_intent_by_constraints(
|
async fn filter_payment_intent_by_constraints(
|
||||||
&self,
|
&self,
|
||||||
merchant_id: &str,
|
merchant_id: &str,
|
||||||
@ -244,7 +249,7 @@ mod storage {
|
|||||||
) -> CustomResult<Vec<PaymentIntent>, errors::StorageError> {
|
) -> CustomResult<Vec<PaymentIntent>, errors::StorageError> {
|
||||||
match storage_scheme {
|
match storage_scheme {
|
||||||
enums::MerchantStorageScheme::PostgresOnly => {
|
enums::MerchantStorageScheme::PostgresOnly => {
|
||||||
let conn = pg_connection(&self.master_pool).await;
|
let conn = pg_connection(&self.replica_pool).await;
|
||||||
PaymentIntent::filter_by_constraints(&conn, merchant_id, pc)
|
PaymentIntent::filter_by_constraints(&conn, merchant_id, pc)
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
@ -262,14 +267,13 @@ mod storage {
|
|||||||
use error_stack::IntoReport;
|
use error_stack::IntoReport;
|
||||||
|
|
||||||
use super::PaymentIntentInterface;
|
use super::PaymentIntentInterface;
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
|
use crate::types::api;
|
||||||
use crate::{
|
use crate::{
|
||||||
connection::pg_connection,
|
connection::pg_connection,
|
||||||
core::errors::{self, CustomResult},
|
core::errors::{self, CustomResult},
|
||||||
services::Store,
|
services::Store,
|
||||||
types::{
|
types::storage::{enums, payment_intent::*},
|
||||||
api,
|
|
||||||
storage::{enums, payment_intent::*},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
@ -309,13 +313,14 @@ mod storage {
|
|||||||
.into_report()
|
.into_report()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
async fn filter_payment_intent_by_constraints(
|
async fn filter_payment_intent_by_constraints(
|
||||||
&self,
|
&self,
|
||||||
merchant_id: &str,
|
merchant_id: &str,
|
||||||
pc: &api::PaymentListConstraints,
|
pc: &api::PaymentListConstraints,
|
||||||
_storage_scheme: enums::MerchantStorageScheme,
|
_storage_scheme: enums::MerchantStorageScheme,
|
||||||
) -> CustomResult<Vec<PaymentIntent>, errors::StorageError> {
|
) -> CustomResult<Vec<PaymentIntent>, errors::StorageError> {
|
||||||
let conn = pg_connection(&self.master_pool).await;
|
let conn = pg_connection(&self.replica_pool).await;
|
||||||
PaymentIntent::filter_by_constraints(&conn, merchant_id, pc)
|
PaymentIntent::filter_by_constraints(&conn, merchant_id, pc)
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
@ -326,6 +331,7 @@ mod storage {
|
|||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
impl PaymentIntentInterface for MockDb {
|
impl PaymentIntentInterface for MockDb {
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
async fn filter_payment_intent_by_constraints(
|
async fn filter_payment_intent_by_constraints(
|
||||||
&self,
|
&self,
|
||||||
_merchant_id: &str,
|
_merchant_id: &str,
|
||||||
|
|||||||
@ -56,6 +56,7 @@ pub trait RefundInterface {
|
|||||||
storage_scheme: enums::MerchantStorageScheme,
|
storage_scheme: enums::MerchantStorageScheme,
|
||||||
) -> CustomResult<storage_types::Refund, errors::StorageError>;
|
) -> CustomResult<storage_types::Refund, errors::StorageError>;
|
||||||
|
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
async fn filter_refund_by_constraints(
|
async fn filter_refund_by_constraints(
|
||||||
&self,
|
&self,
|
||||||
merchant_id: &str,
|
merchant_id: &str,
|
||||||
@ -172,6 +173,7 @@ mod storage {
|
|||||||
.into_report()
|
.into_report()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
async fn filter_refund_by_constraints(
|
async fn filter_refund_by_constraints(
|
||||||
&self,
|
&self,
|
||||||
merchant_id: &str,
|
merchant_id: &str,
|
||||||
@ -179,7 +181,7 @@ mod storage {
|
|||||||
_storage_scheme: enums::MerchantStorageScheme,
|
_storage_scheme: enums::MerchantStorageScheme,
|
||||||
limit: i64,
|
limit: i64,
|
||||||
) -> CustomResult<Vec<storage_models::refund::Refund>, errors::StorageError> {
|
) -> CustomResult<Vec<storage_models::refund::Refund>, errors::StorageError> {
|
||||||
let conn = pg_connection(&self.master_pool).await;
|
let conn = pg_connection(&self.replica_pool).await;
|
||||||
<storage_models::refund::Refund as storage_types::RefundDbExt>::filter_by_constraints(
|
<storage_models::refund::Refund as storage_types::RefundDbExt>::filter_by_constraints(
|
||||||
&conn,
|
&conn,
|
||||||
merchant_id,
|
merchant_id,
|
||||||
@ -574,6 +576,7 @@ mod storage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
async fn filter_refund_by_constraints(
|
async fn filter_refund_by_constraints(
|
||||||
&self,
|
&self,
|
||||||
merchant_id: &str,
|
merchant_id: &str,
|
||||||
@ -583,7 +586,7 @@ mod storage {
|
|||||||
) -> CustomResult<Vec<storage_models::refund::Refund>, errors::StorageError> {
|
) -> CustomResult<Vec<storage_models::refund::Refund>, errors::StorageError> {
|
||||||
match storage_scheme {
|
match storage_scheme {
|
||||||
enums::MerchantStorageScheme::PostgresOnly => {
|
enums::MerchantStorageScheme::PostgresOnly => {
|
||||||
let conn = pg_connection(&self.master_pool).await;
|
let conn = pg_connection(&self.replica_pool).await;
|
||||||
<storage_models::refund::Refund as storage_types::RefundDbExt>::filter_by_constraints(&conn, merchant_id, refund_details, limit)
|
<storage_models::refund::Refund as storage_types::RefundDbExt>::filter_by_constraints(&conn, merchant_id, refund_details, limit)
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)
|
.map_err(Into::into)
|
||||||
@ -700,6 +703,7 @@ impl RefundInterface for MockDb {
|
|||||||
Err(errors::StorageError::MockDbError)?
|
Err(errors::StorageError::MockDbError)?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
async fn filter_refund_by_constraints(
|
async fn filter_refund_by_constraints(
|
||||||
&self,
|
&self,
|
||||||
_merchant_id: &str,
|
_merchant_id: &str,
|
||||||
|
|||||||
@ -31,7 +31,7 @@ use routes::AppState;
|
|||||||
|
|
||||||
pub use self::env::logger;
|
pub use self::env::logger;
|
||||||
use crate::{
|
use crate::{
|
||||||
configs::settings::Settings,
|
configs::settings,
|
||||||
core::errors::{self, ApplicationResult},
|
core::errors::{self, ApplicationResult},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -57,7 +57,8 @@ pub mod pii {
|
|||||||
pub use masking::*;
|
pub use masking::*;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mk_app(
|
#[cfg(feature = "olap")]
|
||||||
|
pub fn mk_olap_app(
|
||||||
state: AppState,
|
state: AppState,
|
||||||
request_body_limit: usize,
|
request_body_limit: usize,
|
||||||
) -> actix_web::App<
|
) -> actix_web::App<
|
||||||
@ -68,6 +69,57 @@ pub fn mk_app(
|
|||||||
Error = actix_web::Error,
|
Error = actix_web::Error,
|
||||||
InitError = (),
|
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()
|
let json_cfg = actix_web::web::JsonConfig::default()
|
||||||
.limit(request_body_limit)
|
.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.
|
.content_type(|mime| mime == mime::APPLICATION_JSON) // FIXME: This doesn't seem to be enforced.
|
||||||
.error_handler(utils::error_parser::custom_json_error_handler);
|
.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)
|
.app_data(json_cfg)
|
||||||
.wrap(middleware::RequestId)
|
.wrap(middleware::RequestId)
|
||||||
.wrap(router_env::tracing_actix_web::TracingLogger::default())
|
.wrap(router_env::tracing_actix_web::TracingLogger::default())
|
||||||
@ -88,36 +140,53 @@ pub fn mk_app(
|
|||||||
errors::error_handlers::custom_error_handlers,
|
errors::error_handlers::custom_error_handlers,
|
||||||
))
|
))
|
||||||
.wrap(cors::cors())
|
.wrap(cors::cors())
|
||||||
.service(routes::Payments::server(state.clone()))
|
}
|
||||||
.service(routes::Customers::server(state.clone()))
|
|
||||||
.service(routes::Refunds::server(state.clone()))
|
pub fn mk_oltp_app(
|
||||||
.service(routes::Payouts::server(state.clone()))
|
state: AppState,
|
||||||
.service(routes::PaymentMethods::server(state.clone()))
|
request_body_limit: usize,
|
||||||
.service(routes::MerchantAccount::server(state.clone()))
|
) -> actix_web::App<
|
||||||
.service(routes::MerchantConnectorAccount::server(state.clone()))
|
impl ServiceFactory<
|
||||||
.service(routes::EphemeralKey::server(state.clone()))
|
ServiceRequest,
|
||||||
.service(routes::Webhooks::server(state.clone()));
|
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")]
|
#[cfg(feature = "stripe")]
|
||||||
{
|
{
|
||||||
server_app = server_app.service(routes::StripeApis::server(state.clone()));
|
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
|
server_app
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::expect_used, clippy::unwrap_used)]
|
/// Starts the OLTP server with only OLTP services
|
||||||
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Unwrap used because without the value we can't start the server
|
/// 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);
|
logger::debug!(startup_config=?conf);
|
||||||
let server = conf.server.clone();
|
let server = conf.server.clone();
|
||||||
let state = routes::AppState::new(conf).await;
|
let state = routes::AppState::new(conf).await;
|
||||||
// Cloning to close connections before shutdown
|
// Cloning to close connections before shutdown
|
||||||
let app_state = state.clone();
|
let app_state = state.clone();
|
||||||
let request_body_limit = server.request_body_limit;
|
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))?
|
.bind((server.host.as_str(), server.port))?
|
||||||
.workers(server.workers.unwrap_or_else(num_cpus::get_physical))
|
.workers(server.workers.unwrap_or_else(num_cpus::get_physical))
|
||||||
.run();
|
.run();
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
use actix_web::{web, Scope};
|
use actix_web::{web, Scope};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
admin::*, customers::*, ephemeral_key::*, health::*, mandates::*, payment_methods::*,
|
customers::*, ephemeral_key::*, health::*, mandates::*, payment_methods::*, payments::*,
|
||||||
payments::*, payouts::*, refunds::*, webhooks::*,
|
payouts::*, refunds::*, webhooks::*,
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
configs::settings::Settings,
|
configs::settings::Settings,
|
||||||
@ -43,7 +43,13 @@ impl AppState {
|
|||||||
pub struct Health;
|
pub struct Health;
|
||||||
|
|
||||||
impl Health {
|
impl Health {
|
||||||
pub fn server(state: AppState) -> Scope {
|
pub fn oltp_server(state: AppState) -> Scope {
|
||||||
|
web::scope("")
|
||||||
|
.app_data(web::Data::new(state))
|
||||||
|
.service(web::resource("/health").route(web::get().to(health)))
|
||||||
|
}
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
|
pub fn olap_server(state: AppState) -> Scope {
|
||||||
web::scope("")
|
web::scope("")
|
||||||
.app_data(web::Data::new(state))
|
.app_data(web::Data::new(state))
|
||||||
.service(web::resource("/health").route(web::get().to(health)))
|
.service(web::resource("/health").route(web::get().to(health)))
|
||||||
@ -53,12 +59,17 @@ impl Health {
|
|||||||
pub struct Payments;
|
pub struct Payments;
|
||||||
|
|
||||||
impl Payments {
|
impl Payments {
|
||||||
pub fn server(state: AppState) -> Scope {
|
#[cfg(feature = "olap")]
|
||||||
|
pub fn olap_server(state: AppState) -> Scope {
|
||||||
|
web::scope("/payments")
|
||||||
|
.app_data(web::Data::new(state))
|
||||||
|
.service(web::resource("/list").route(web::get().to(payments_list)))
|
||||||
|
}
|
||||||
|
pub fn oltp_server(state: AppState) -> Scope {
|
||||||
// Routes are matched in the order they are declared.
|
// Routes are matched in the order they are declared.
|
||||||
web::scope("/payments")
|
web::scope("/payments")
|
||||||
.app_data(web::Data::new(state))
|
.app_data(web::Data::new(state))
|
||||||
.service(web::resource("").route(web::post().to(payments_create)))
|
.service(web::resource("").route(web::post().to(payments_create)))
|
||||||
.service(web::resource("/list").route(web::get().to(payments_list)))
|
|
||||||
.service(
|
.service(
|
||||||
web::resource("/session_tokens").route(web::post().to(payments_connector_session)),
|
web::resource("/session_tokens").route(web::post().to(payments_connector_session)),
|
||||||
)
|
)
|
||||||
@ -84,7 +95,16 @@ impl Payments {
|
|||||||
pub struct Customers;
|
pub struct Customers;
|
||||||
|
|
||||||
impl Customers {
|
impl Customers {
|
||||||
pub fn server(state: AppState) -> Scope {
|
#[cfg(feature = "olap")]
|
||||||
|
pub fn olap_server(state: AppState) -> Scope {
|
||||||
|
web::scope("/customers")
|
||||||
|
.app_data(web::Data::new(state))
|
||||||
|
.service(
|
||||||
|
web::resource("/{customer_id}/mandates")
|
||||||
|
.route(web::get().to(get_customer_mandates)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
pub fn oltp_server(state: AppState) -> Scope {
|
||||||
web::scope("/customers")
|
web::scope("/customers")
|
||||||
.app_data(web::Data::new(state))
|
.app_data(web::Data::new(state))
|
||||||
.service(web::resource("").route(web::post().to(customers_create)))
|
.service(web::resource("").route(web::post().to(customers_create)))
|
||||||
@ -94,10 +114,6 @@ impl Customers {
|
|||||||
.route(web::post().to(customers_update))
|
.route(web::post().to(customers_update))
|
||||||
.route(web::delete().to(customers_delete)),
|
.route(web::delete().to(customers_delete)),
|
||||||
)
|
)
|
||||||
.service(
|
|
||||||
web::resource("/{customer_id}/mandates")
|
|
||||||
.route(web::get().to(get_customer_mandates)),
|
|
||||||
)
|
|
||||||
.service(
|
.service(
|
||||||
web::resource("/{customer_id}/payment_methods")
|
web::resource("/{customer_id}/payment_methods")
|
||||||
.route(web::get().to(list_customer_payment_method_api)),
|
.route(web::get().to(list_customer_payment_method_api)),
|
||||||
@ -108,12 +124,17 @@ impl Customers {
|
|||||||
pub struct Refunds;
|
pub struct Refunds;
|
||||||
|
|
||||||
impl Refunds {
|
impl Refunds {
|
||||||
pub fn server(state: AppState) -> Scope {
|
#[cfg(feature = "olap")]
|
||||||
|
pub fn olap_server(state: AppState) -> Scope {
|
||||||
|
web::scope("/refunds")
|
||||||
|
.app_data(web::Data::new(state))
|
||||||
|
.service(web::resource("/list").route(web::get().to(refunds_list)))
|
||||||
|
}
|
||||||
|
pub fn oltp_server(state: AppState) -> Scope {
|
||||||
// Routes are matches in the order they are declared.
|
// Routes are matches in the order they are declared.
|
||||||
web::scope("/refunds")
|
web::scope("/refunds")
|
||||||
.app_data(web::Data::new(state))
|
.app_data(web::Data::new(state))
|
||||||
.service(web::resource("").route(web::post().to(refunds_create)))
|
.service(web::resource("").route(web::post().to(refunds_create)))
|
||||||
.service(web::resource("/list").route(web::get().to(refunds_list)))
|
|
||||||
.service(
|
.service(
|
||||||
web::resource("/{id}")
|
web::resource("/{id}")
|
||||||
.route(web::get().to(refunds_retrieve))
|
.route(web::get().to(refunds_retrieve))
|
||||||
@ -125,7 +146,13 @@ impl Refunds {
|
|||||||
pub struct Payouts;
|
pub struct Payouts;
|
||||||
|
|
||||||
impl Payouts {
|
impl Payouts {
|
||||||
pub fn server(state: AppState) -> Scope {
|
#[cfg(feature = "olap")]
|
||||||
|
pub fn olap_server(state: AppState) -> Scope {
|
||||||
|
web::scope("/payouts")
|
||||||
|
.app_data(web::Data::new(state))
|
||||||
|
.service(web::resource("/accounts").route(web::get().to(payouts_accounts)))
|
||||||
|
}
|
||||||
|
pub fn oltp_server(state: AppState) -> Scope {
|
||||||
web::scope("/payouts")
|
web::scope("/payouts")
|
||||||
.app_data(web::Data::new(state))
|
.app_data(web::Data::new(state))
|
||||||
.service(web::resource("/create").route(web::post().to(payouts_create)))
|
.service(web::resource("/create").route(web::post().to(payouts_create)))
|
||||||
@ -133,14 +160,13 @@ impl Payouts {
|
|||||||
.service(web::resource("/update").route(web::post().to(payouts_update)))
|
.service(web::resource("/update").route(web::post().to(payouts_update)))
|
||||||
.service(web::resource("/reverse").route(web::post().to(payouts_reverse)))
|
.service(web::resource("/reverse").route(web::post().to(payouts_reverse)))
|
||||||
.service(web::resource("/cancel").route(web::post().to(payouts_cancel)))
|
.service(web::resource("/cancel").route(web::post().to(payouts_cancel)))
|
||||||
.service(web::resource("/accounts").route(web::get().to(payouts_accounts)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct PaymentMethods;
|
pub struct PaymentMethods;
|
||||||
|
|
||||||
impl PaymentMethods {
|
impl PaymentMethods {
|
||||||
pub fn server(state: AppState) -> Scope {
|
pub fn oltp_server(state: AppState) -> Scope {
|
||||||
web::scope("/payment_methods")
|
web::scope("/payment_methods")
|
||||||
.app_data(web::Data::new(state))
|
.app_data(web::Data::new(state))
|
||||||
.service(web::resource("").route(web::post().to(create_payment_method_api)))
|
.service(web::resource("").route(web::post().to(create_payment_method_api)))
|
||||||
@ -156,9 +182,11 @@ impl PaymentMethods {
|
|||||||
pub struct MerchantAccount;
|
pub struct MerchantAccount;
|
||||||
|
|
||||||
impl MerchantAccount {
|
impl MerchantAccount {
|
||||||
pub fn server(config: AppState) -> Scope {
|
#[cfg(feature = "olap")]
|
||||||
|
pub fn olap_server(state: AppState) -> Scope {
|
||||||
|
use super::admin::*;
|
||||||
web::scope("/accounts")
|
web::scope("/accounts")
|
||||||
.app_data(web::Data::new(config))
|
.app_data(web::Data::new(state))
|
||||||
.service(web::resource("").route(web::post().to(merchant_account_create)))
|
.service(web::resource("").route(web::post().to(merchant_account_create)))
|
||||||
.service(
|
.service(
|
||||||
web::resource("/{id}")
|
web::resource("/{id}")
|
||||||
@ -172,9 +200,11 @@ impl MerchantAccount {
|
|||||||
pub struct MerchantConnectorAccount;
|
pub struct MerchantConnectorAccount;
|
||||||
|
|
||||||
impl MerchantConnectorAccount {
|
impl MerchantConnectorAccount {
|
||||||
pub fn server(config: AppState) -> Scope {
|
#[cfg(feature = "olap")]
|
||||||
|
pub fn olap_server(state: AppState) -> Scope {
|
||||||
|
use super::admin::*;
|
||||||
web::scope("/account")
|
web::scope("/account")
|
||||||
.app_data(web::Data::new(config))
|
.app_data(web::Data::new(state))
|
||||||
.service(
|
.service(
|
||||||
web::resource("/{merchant_id}/connectors")
|
web::resource("/{merchant_id}/connectors")
|
||||||
.route(web::post().to(payment_connector_create))
|
.route(web::post().to(payment_connector_create))
|
||||||
@ -195,7 +225,7 @@ impl MerchantConnectorAccount {
|
|||||||
pub struct EphemeralKey;
|
pub struct EphemeralKey;
|
||||||
|
|
||||||
impl EphemeralKey {
|
impl EphemeralKey {
|
||||||
pub fn server(config: AppState) -> Scope {
|
pub fn oltp_server(config: AppState) -> Scope {
|
||||||
web::scope("/ephemeral_keys")
|
web::scope("/ephemeral_keys")
|
||||||
.app_data(web::Data::new(config))
|
.app_data(web::Data::new(config))
|
||||||
.service(web::resource("").route(web::post().to(ephemeral_key_create)))
|
.service(web::resource("").route(web::post().to(ephemeral_key_create)))
|
||||||
@ -206,10 +236,14 @@ impl EphemeralKey {
|
|||||||
pub struct Mandates;
|
pub struct Mandates;
|
||||||
|
|
||||||
impl Mandates {
|
impl Mandates {
|
||||||
pub fn server(config: AppState) -> Scope {
|
pub fn olap_server(state: AppState) -> Scope {
|
||||||
|
web::scope("/mandates")
|
||||||
|
.app_data(web::Data::new(state))
|
||||||
|
.service(web::resource("/{id}").route(web::get().to(get_mandate)))
|
||||||
|
}
|
||||||
|
pub fn oltp_server(config: AppState) -> Scope {
|
||||||
web::scope("/mandates")
|
web::scope("/mandates")
|
||||||
.app_data(web::Data::new(config))
|
.app_data(web::Data::new(config))
|
||||||
.service(web::resource("/{id}").route(web::get().to(get_mandate)))
|
|
||||||
.service(web::resource("/revoke/{id}").route(web::post().to(revoke_mandate)))
|
.service(web::resource("/revoke/{id}").route(web::post().to(revoke_mandate)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -217,7 +251,7 @@ impl Mandates {
|
|||||||
pub struct Webhooks;
|
pub struct Webhooks;
|
||||||
|
|
||||||
impl Webhooks {
|
impl Webhooks {
|
||||||
pub fn server(config: AppState) -> Scope {
|
pub fn oltp_server(config: AppState) -> Scope {
|
||||||
web::scope("/webhooks")
|
web::scope("/webhooks")
|
||||||
.app_data(web::Data::new(config))
|
.app_data(web::Data::new(config))
|
||||||
.service(
|
.service(
|
||||||
|
|||||||
@ -322,6 +322,7 @@ pub async fn payments_cancel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip_all, fields(flow = ?Flow::PaymentsList))]
|
#[instrument(skip_all, fields(flow = ?Flow::PaymentsList))]
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
// #[get("/list")]
|
// #[get("/list")]
|
||||||
pub async fn payments_list(
|
pub async fn payments_list(
|
||||||
state: web::Data<app::AppState>,
|
state: web::Data<app::AppState>,
|
||||||
|
|||||||
@ -80,6 +80,7 @@ pub async fn refunds_update(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip_all, fields(flow = ?Flow::RefundsList))]
|
#[instrument(skip_all, fields(flow = ?Flow::RefundsList))]
|
||||||
|
#[cfg(feature = "olap")]
|
||||||
// #[get("/list")]
|
// #[get("/list")]
|
||||||
pub async fn refunds_list(
|
pub async fn refunds_list(
|
||||||
state: web::Data<AppState>,
|
state: web::Data<AppState>,
|
||||||
|
|||||||
@ -11,7 +11,7 @@ use actix_web::{
|
|||||||
test::{call_and_read_body_json, TestRequest},
|
test::{call_and_read_body_json, TestRequest},
|
||||||
};
|
};
|
||||||
use derive_deref::Deref;
|
use derive_deref::Deref;
|
||||||
use router::{configs::settings::Settings, routes::AppState, start_server};
|
use router::{configs::settings::Settings, routes::AppState};
|
||||||
use serde::{de::DeserializeOwned, Deserialize};
|
use serde::{de::DeserializeOwned, Deserialize};
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
use tokio::sync::OnceCell;
|
use tokio::sync::OnceCell;
|
||||||
@ -20,7 +20,9 @@ static SERVER: OnceCell<bool> = OnceCell::const_new();
|
|||||||
|
|
||||||
async fn spawn_server() -> bool {
|
async fn spawn_server() -> bool {
|
||||||
let conf = Settings::new().expect("invalid settings");
|
let conf = Settings::new().expect("invalid settings");
|
||||||
let (server, _state) = start_server(conf).await.expect("failed to create server");
|
let (server, _state) = router::start_oltp_server(conf)
|
||||||
|
.await
|
||||||
|
.expect("failed to create server");
|
||||||
|
|
||||||
let _server = tokio::spawn(server);
|
let _server = tokio::spawn(server);
|
||||||
true
|
true
|
||||||
@ -47,7 +49,7 @@ pub async fn mk_service(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let app_state = AppState::with_storage(conf, router::db::StorageImpl::Mock).await;
|
let app_state = AppState::with_storage(conf, router::db::StorageImpl::Mock).await;
|
||||||
actix_web::test::init_service(router::mk_app(app_state, request_body_limit)).await
|
actix_web::test::init_service(router::mk_oltp_app(app_state, request_body_limit)).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Guest;
|
pub struct Guest;
|
||||||
|
|||||||
Reference in New Issue
Block a user