Create a tiny server for testing the Fetch API

This commit is contained in:
Savanni D'Gerinel 2025-02-13 22:41:17 -05:00
parent f6534d5d05
commit e9f89e1bdb
2 changed files with 28 additions and 2 deletions

View File

@ -4,3 +4,6 @@ version = "0.1.0"
edition = "2021"
[dependencies]
axum = "0.8.1"
tokio = { version = "1.43.0", features = ["full", "rt"] }
tower-http = { version = "0.6.2", features = ["cors"] }

View File

@ -1,3 +1,26 @@
fn main() {
println!("Hello, world!");
use axum::{http::{Method, StatusCode}, routing::get, Json, Router};
use tower_http::cors::{Any, CorsLayer};
#[tokio::main]
async fn main() {
let app = Router::new()
.route(
"/api/v1/health",
get(|| async { (StatusCode::OK, Json(None::<String>)) }),
).layer(
CorsLayer::new()
.allow_methods([Method::GET]).allow_origin(Any),
)
.route(
"/api/v1/denied",
get(|| async { (StatusCode::UNAUTHORIZED, Json(None::<String>)) }),
).layer(
CorsLayer::new()
.allow_methods([Method::GET]).allow_origin(Any),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:8001")
.await
.unwrap();
axum::serve(listener, app).await.unwrap();
}