Skip to content

Commit cf9c0fb

Browse files
authored
Merge pull request #31 from Coduck-Team/28-rocket-axum-replacement
Replace framework 'rocket' with 'axum'
2 parents 583222b + 37383f7 commit cf9c0fb

File tree

7 files changed

+46
-39
lines changed

7 files changed

+46
-39
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
DATABASE_URL="postgres://postgres:password@localhost:5432/bibimbap"
1+
DATABASE_URL="postgres://postgres:password@localhost:5432/coduck"

Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "bibimbap-backend"
2+
name = "coduck-backend"
33
version = "0.1.0"
44
edition = "2021"
55

@@ -8,7 +8,11 @@ path = "src/lib.rs"
88

99
[[bin]]
1010
path = "src/main.rs"
11-
name = "bibimbap-backend"
11+
name = "coduck-backend"
1212

1313
[dependencies]
14-
rocket = "0.5.1"
14+
axum = "0.8.4"
15+
tokio = { version = "1.45.1", features = ["full"] }
16+
17+
[dev-dependencies]
18+
reqwest = { version = "0.12.19", features = ["json", "rustls-tls"] }

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
# Bibimbap
2-
A platform for creation of programming contest problems heavily inspired by Polygon
1+
# Coduck
2+
3+
A platform for the creation of programming contest problems heavily inspired by Polygon

scripts/init_db.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fi
1818

1919
DB_USER=${POSTGRES_USER:=postgres}
2020
DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
21-
DB_NAME="${POSTGRES_DB:=bibimbap}"
21+
DB_NAME="${POSTGRES_DB:=coduck}"
2222
DB_PORT="${POSTGRES_PORT:=5432}"
2323
DB_HOST="${POSTGRES_HOST:=localhost}"
2424

src/lib.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
#[macro_use]
2-
extern crate rocket;
1+
use axum::{routing::get, Router};
32

4-
use rocket::{Build, Rocket};
5-
6-
#[get("/health_check")]
7-
fn health_check() {
8-
()
3+
async fn health_check() -> &'static str {
4+
"OK"
95
}
106

11-
pub fn run() -> Rocket<Build> {
12-
rocket::build().mount("/", routes![health_check])
7+
pub fn build_router() -> Router {
8+
Router::new().route("/health", get(health_check))
139
}

src/main.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
use bibimbap_backend::run;
2-
3-
#[rocket::main]
1+
#[tokio::main]
42
async fn main() {
5-
if let Err(e) = run().launch().await {
6-
println!("Failed to launch Rocket: {e}");
7-
drop(e);
8-
}
3+
let app = coduck_backend::build_router();
4+
5+
let listener = tokio::net::TcpListener::bind("127.0.0.1:8080")
6+
.await
7+
.unwrap();
8+
println!("Listening on {}", listener.local_addr().unwrap());
9+
axum::serve(listener, app)
10+
.await
11+
.expect("Server failed to start");
912
}

tests/health_check.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
#[macro_use]
2-
extern crate rocket;
1+
#[tokio::test]
2+
async fn health_check_works() {
3+
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
4+
let port = listener.local_addr().unwrap().port();
35

4-
use rocket::http::Status;
5-
use rocket::local::blocking::Client;
6-
use rocket::{Build, Rocket};
7-
use std::io::Read;
6+
tokio::spawn(async move {
7+
let app = coduck_backend::build_router();
8+
axum::serve(listener, app)
9+
.await
10+
.expect("Server failed to start");
11+
});
812

9-
fn spawn_app() -> Rocket<Build> {
10-
bibimbap_backend::run()
11-
}
12-
13-
#[test]
14-
fn health_check_works() {
15-
let client = Client::tracked(spawn_app()).expect("Failed to create client");
16-
let response = client.get(uri!("/health_check")).dispatch();
13+
let client = reqwest::Client::new();
14+
let response = client
15+
.get(&format!("http://127.0.0.1:{port}/health"))
16+
.send()
17+
.await
18+
.unwrap();
1719

18-
assert_eq!(response.status(), Status::Ok);
19-
assert_eq!(response.bytes().count(), 0);
20+
assert_eq!(response.status(), reqwest::StatusCode::OK);
21+
let body = response.text().await.unwrap();
22+
assert_eq!(body, "OK");
2023
}

0 commit comments

Comments
 (0)