Skip to content

Commit

Permalink
refactor: simplify and reorganize Gateway
Browse files Browse the repository at this point in the history
- use more concise types for ip/port to make cast to IP (and config creation in general) error-free.
- extract routes to dedicated function: this practice and naming as `app` a known idiom: this makes
for easier testing later on, and also will contain other configurations later on like banned IPs.
- Fix naming of arg in `add_transaction`: the arg is already
  deserialized from json, so no _json suffix.
  • Loading branch information
Gilad Chase committed Apr 4, 2024
1 parent 3a958a9 commit 9aa64ae
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
8 changes: 0 additions & 8 deletions crates/gateway/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use thiserror::Error;

#[derive(Debug, Error)]
pub enum GatewayError {
#[error(transparent)]
ConfigError(#[from] GatewayConfigError),
#[error(transparent)]
HTTPError(#[from] hyper::http::Error),
#[error("Internal server error")]
Expand All @@ -13,9 +11,3 @@ pub enum GatewayError {
#[error("Error while starting the server")]
ServerStartError(#[from] hyper::Error),
}

#[derive(Debug, Error)]
pub enum GatewayConfigError {
#[error("Server address is not an bind IP address: {0}")]
InvalidServerBindAddress(String),
}
43 changes: 26 additions & 17 deletions crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::errors::{GatewayConfigError, GatewayError};
use crate::errors::GatewayError;
use axum::response::IntoResponse;
use axum::routing::{get, post};
use axum::{Json, Router};
use starknet_api::external_transaction::ExternalTransaction;
use std::net::SocketAddr;
use std::str::FromStr;
use std::net::{IpAddr, SocketAddr};

#[cfg(test)]
#[path = "gateway_test.rs"]
Expand All @@ -13,20 +12,14 @@ pub mod gateway_test;
pub type GatewayResult = Result<(), GatewayError>;

pub struct Gateway {
pub gateway_config: GatewayConfig,
pub config: GatewayConfig,
}

impl Gateway {
pub async fn build_server(&self) -> GatewayResult {
pub async fn build_server(self) -> GatewayResult {
// Parses the bind address from GatewayConfig, returning an error for invalid addresses.
let addr = SocketAddr::from_str(&self.gateway_config.bind_address).map_err(|_| {
GatewayConfigError::InvalidServerBindAddress(self.gateway_config.bind_address.clone())
})?;

// Sets up the router with the specified routes for the server.
let app = Router::new()
.route("/is_alive", get(is_alive))
.route("/add_transaction", post(add_transaction));
let addr = SocketAddr::new(self.config.ip, self.config.port);
let app = app();

// Create a server that runs forever.
axum::Server::bind(&addr)
Expand All @@ -38,18 +31,34 @@ impl Gateway {
}
}

pub struct GatewayConfig {
pub bind_address: String,
/// Sets up the router with the specified routes for the server.
pub fn app() -> Router {
Router::new()
.route("/is_alive", get(is_alive))
.route("/add_transaction", post(add_transaction))
// TODO: when we need to configure the router, like adding banned ips, add it here via
// `with_state`.
}

async fn is_alive() -> impl IntoResponse {
unimplemented!("Future handling should be implemented here.");
}

async fn add_transaction(Json(transaction_json): Json<ExternalTransaction>) -> impl IntoResponse {
match transaction_json {
async fn add_transaction(Json(transaction): Json<ExternalTransaction>) -> impl IntoResponse {
match transaction {
ExternalTransaction::Declare(_) => "DECLARE",
ExternalTransaction::DeployAccount(_) => "DEPLOY_ACCOUNT",
ExternalTransaction::Invoke(_) => "INVOKE",
}
}

pub struct GatewayConfig {
pub ip: IpAddr,
pub port: u16,
}

impl GatewayConfig {
pub fn new(ip: IpAddr, port: u16) -> Self {
Self { ip, port }
}
}

0 comments on commit 9aa64ae

Please sign in to comment.