-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement API Gateway skeleton and unit tests
- Loading branch information
1 parent
80cd54e
commit 7643196
Showing
6 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
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")] | ||
InternalServerError, | ||
#[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), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crate::errors::{GatewayConfigError, GatewayError}; | ||
use hyper::service::{make_service_fn, service_fn}; | ||
use hyper::{Body, Method, Request, Response, Server, StatusCode}; | ||
use std::convert::Infallible; | ||
use std::net::SocketAddr; | ||
use std::str::FromStr; | ||
|
||
#[cfg(test)] | ||
#[path = "gateway_test.rs"] | ||
pub mod gateway_test; | ||
|
||
const NOT_FOUND_RESPONSE: &str = "Not found."; | ||
type RequestBody = Request<Body>; | ||
type ResponseBody = Response<Body>; | ||
pub type GatewayResult = Result<(), GatewayError>; | ||
|
||
pub struct Gateway { | ||
pub gateway_config: GatewayConfig, | ||
} | ||
|
||
impl Gateway { | ||
pub async fn build_server(&self) -> GatewayResult { | ||
let addr = SocketAddr::from_str(&self.gateway_config.bind_address).map_err(|_| { | ||
GatewayConfigError::InvalidServerBindAddress(self.gateway_config.bind_address.clone()) | ||
})?; | ||
|
||
let make_service = | ||
make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle_request)) }); | ||
|
||
Server::bind(&addr).serve(make_service).await?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
pub struct GatewayConfig { | ||
pub bind_address: String, | ||
} | ||
|
||
async fn handle_request(request: RequestBody) -> Result<Response<Body>, GatewayError> { | ||
let (parts, _body) = request.into_parts(); | ||
let response = match (parts.method, parts.uri.path()) { | ||
(Method::GET, "/is_alive") => is_alive(), | ||
_ => response(StatusCode::NOT_FOUND, NOT_FOUND_RESPONSE.to_string()), | ||
}; | ||
response | ||
} | ||
|
||
fn is_alive() -> Result<ResponseBody, GatewayError> { | ||
unimplemented!("Future handling should be implemented here."); | ||
} | ||
|
||
fn response(status: StatusCode, body_content: String) -> Result<Response<Body>, GatewayError> { | ||
Ok(Response::builder() | ||
.status(status) | ||
.body(Body::from(body_content))?) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use crate::gateway::handle_request; | ||
use hyper::{Body, Request}; | ||
|
||
#[tokio::test] | ||
async fn test_invalid_request() { | ||
// Create a sample GET request for an invalid path | ||
let request = Request::get("/some_invalid_path") | ||
.body(Body::empty()) | ||
.unwrap(); | ||
let response = handle_request(request).await.unwrap(); | ||
|
||
assert_eq!(response.status(), 404); | ||
assert_eq!( | ||
String::from_utf8_lossy(&hyper::body::to_bytes(response.into_body()).await.unwrap()), | ||
"Not found." | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
|
||
pub mod errors; | ||
pub mod gateway; |