feat: setup a backend server with axum
This commit is contained in:
8
crates/backend/Cargo.toml
Normal file
8
crates/backend/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "backend"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
axum = "0.7.6"
|
||||
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] }
|
12
crates/backend/src/lib.rs
Normal file
12
crates/backend/src/lib.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use axum::http::{StatusCode, Uri};
|
||||
use axum::{routing::get, Router};
|
||||
|
||||
pub fn get_router() -> Router {
|
||||
Router::new()
|
||||
.route("/", get(|| async { "Hello, world!" }))
|
||||
.fallback(fallback)
|
||||
}
|
||||
|
||||
async fn fallback(uri: Uri) -> (StatusCode, String) {
|
||||
(StatusCode::NOT_FOUND, format!("No route for {uri}"))
|
||||
}
|
10
crates/backend/src/main.rs
Normal file
10
crates/backend/src/main.rs
Normal file
@ -0,0 +1,10 @@
|
||||
use backend::get_router;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let app = get_router();
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap();
|
||||
|
||||
println!("Listening on {}", listener.local_addr().unwrap());
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
Reference in New Issue
Block a user