rtmc-be/src/api/mod.rs
2024-11-02 19:34:53 +02:00

32 lines
845 B
Rust

use crate::context::AppContext;
use axum::{
routing::{get, post},
http::StatusCode,
Json, Router,
};
pub mod routes;
pub mod ws;
async fn get_api_info() -> String {
format!("API v{}", env!("CARGO_PKG_VERSION"))
}
pub async fn start_api(ctx: AppContext) {
// build our application with a route
let app = Router::new()
// `GET /` goes to `root`
.route("/api/", get(get_api_info))
.route("/ws/", get(ws::WsHandler::ws_handler))
.with_state(ctx);
// `POST /users` goes to `create_user`
// .route("/users", post(create_user));
// run our app with hyper, listening globally on port 3000
log::info!("Listening on http://0.0.0.0:3000");
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}