File tree Expand file tree Collapse file tree 7 files changed +46
-39
lines changed
Expand file tree Collapse file tree 7 files changed +46
-39
lines changed Original file line number Diff line number Diff line change 1- DATABASE_URL = " postgres://postgres:password@localhost:5432/bibimbap "
1+ DATABASE_URL = " postgres://postgres:password@localhost:5432/coduck "
Original file line number Diff line number Diff line change 11[package ]
2- name = " bibimbap -backend"
2+ name = " coduck -backend"
33version = " 0.1.0"
44edition = " 2021"
55
@@ -8,7 +8,11 @@ path = "src/lib.rs"
88
99[[bin ]]
1010path = " 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" ] }
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1818
1919DB_USER=${POSTGRES_USER:= postgres}
2020DB_PASSWORD=" ${POSTGRES_PASSWORD:= password} "
21- DB_NAME=" ${POSTGRES_DB:= bibimbap } "
21+ DB_NAME=" ${POSTGRES_DB:= coduck } "
2222DB_PORT=" ${POSTGRES_PORT:= 5432} "
2323DB_HOST=" ${POSTGRES_HOST:= localhost} "
2424
Original file line number Diff line number Diff line change 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}
Original file line number Diff line number Diff line change 1- use bibimbap_backend:: run;
2-
3- #[ rocket:: main]
1+ #[ tokio:: main]
42async 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}
Original file line number Diff line number Diff line change 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}
You can’t perform that action at this time.
0 commit comments