feat: setup a backend server with axum

This commit is contained in:
2024-09-23 18:02:07 +02:00
parent 2e057eee01
commit a50d951af7
5 changed files with 173 additions and 10 deletions

View 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
View 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}"))
}

View 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();
}