initial commit

This commit is contained in:
Nick Bland 2021-10-22 12:38:38 +10:00
commit 0466015771
No known key found for this signature in database
GPG Key ID: B46CF88E4DAB4A2C
6 changed files with 1807 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

1732
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

16
Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "mail_app"
version = "0.1.0"
authors = ["Nick Bland"]
edition = "2021"
[lib]
path = "src/lib.rs"
[dependencies]
actix-web = "4.0.0-beta.8"
[dev-dependencies]
actix-rt = "2"
reqwest = "0.11"
tokio = "1"

17
src/lib.rs Normal file
View File

@ -0,0 +1,17 @@
use actix_web::{web, App, HttpResponse, HttpServer};
use actix_web::dev::Server;
use std::net::TcpListener;
async fn health_check() -> HttpResponse {
HttpResponse::Ok().finish()
}
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
let server = HttpServer::new(|| {
App::new()
.route("/health_check", web::get().to(health_check))
})
.listen(listener)?
.run();
Ok(server)
}

10
src/main.rs Normal file
View File

@ -0,0 +1,10 @@
use mail_app::run;
use std::net::TcpListener;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:8000").expect("Failed to bind to 8000");
run(listener)?.await
}

31
tests/health_check.rs Normal file
View File

@ -0,0 +1,31 @@
use std::net::TcpListener;
#[actix_rt::test]
async fn health_check_works() {
// Arrange
let address = spawn_app();
let client = reqwest::Client::new();
// Perform a 'reqwest' against endpoint
let response = client
.get(&format!("{}/health_check", &address))
.send()
.await
.expect("Failed to execute request.");
// Assert our test
assert!(response.status().is_success());
assert_eq!(Some(0), response.content_length());
}
fn spawn_app() -> String {
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to random port");
let port = listener.local_addr().unwrap().port();
let server = mail_app::run(listener).expect("Failed to bind address");
let _ = tokio::spawn(server);
format!("http://127.0.0.1:{}", port)
}