-
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
77bbc06
commit 8941ef1
Showing
5 changed files
with
117 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum GatewayError { | ||
#[error("Internal server error")] | ||
InternalServerError, | ||
#[error("Error while starting the server")] | ||
ServerError, | ||
} |
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::GatewayError; | ||
use hyper::service::{make_service_fn, service_fn}; | ||
use hyper::{Body, Method, Request, Response, Server}; | ||
use std::convert::Infallible; | ||
use std::net::SocketAddr; | ||
|
||
#[cfg(test)] | ||
#[path = "gateway_test.rs"] | ||
pub mod gateway_test; | ||
|
||
const NOT_FOUND_RESPONSE: &str = "Not found."; | ||
|
||
pub struct GatewayConfig { | ||
pub config: String, | ||
} | ||
|
||
pub struct Gateway { | ||
pub gateway_config: GatewayConfig, | ||
} | ||
|
||
impl Gateway { | ||
pub fn new(gateway_config: GatewayConfig) -> Self { | ||
Self { gateway_config } | ||
} | ||
|
||
pub async fn build_server(&self) -> Result<(), GatewayError> { | ||
let addr = SocketAddr::from(([127, 0, 0, 1], 8080)); | ||
|
||
let make_service = make_service_fn(|_conn| async { | ||
Ok::<_, Infallible>(service_fn(Self::handle_request)) | ||
}); | ||
|
||
match Server::bind(&addr).serve(make_service).await { | ||
Ok(_) => Ok(()), | ||
Err(_) => Err(GatewayError::ServerError), | ||
} | ||
} | ||
|
||
async fn handle_request(request: Request<Body>) -> Result<Response<Body>, GatewayError> { | ||
let (parts, _body) = request.into_parts(); | ||
let response = match (parts.method, parts.uri.path()) { | ||
(Method::GET, "/is_alive") => is_alive(), | ||
_ => Ok(Response::builder() | ||
.status(404) | ||
.body(Body::from(NOT_FOUND_RESPONSE)) | ||
.map_err(|_| GatewayError::InternalServerError)?), | ||
}; | ||
response | ||
} | ||
} | ||
|
||
fn is_alive() -> Result<Response<Body>, GatewayError> { | ||
Response::builder() | ||
.status(200) | ||
.body(Body::from("Server is alive")) | ||
.map_err(|_| GatewayError::InternalServerError) | ||
} |
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,41 @@ | ||
use crate::gateway::Gateway; | ||
use crate::gateway::GatewayConfig; | ||
use hyper::{Body, Request}; | ||
use tokio::time::{delay_for, Duration}; | ||
|
||
#[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 = Gateway::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." | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_build_server() { | ||
let gateway = Gateway::new(GatewayConfig { | ||
config: "some_configurations".to_string(), | ||
}); | ||
|
||
tokio::spawn(async move { | ||
gateway.build_server().await.unwrap(); | ||
}); | ||
delay_for(Duration::from_secs(1)).await; | ||
|
||
let client = hyper::Client::new(); | ||
let uri = "http://127.0.0.1:8080/is_alive".parse().unwrap(); | ||
let response = client.get(uri).await.unwrap(); | ||
|
||
assert_eq!(response.status(), 200); | ||
assert_eq!( | ||
String::from_utf8_lossy(&hyper::body::to_bytes(response.into_body()).await.unwrap()), | ||
"Server is alive" | ||
); | ||
} |
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,2 @@ | ||
pub mod errors; | ||
pub mod gateway; |