add the API routes with axum
This commit is contained in:
parent
1b64251c5e
commit
88f4dd6bd6
33
src/api.rs
Normal file
33
src/api.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
/**
|
||||
GET /questions (empty body; return JSON)
|
||||
POST /questions (JSON body; return HTTP status code)
|
||||
PUT /questions/:questionId (JSON body, return HTTP status code)
|
||||
DELETE /questions/:questionId (empty body; return HTTP status code)
|
||||
POST /answers (www-url-encoded body; return HTTP status code)
|
||||
*
|
||||
*/
|
||||
pub async fn get_questions() -> Response {
|
||||
//TODO
|
||||
(StatusCode::OK, " Get Questions").into_response()
|
||||
}
|
||||
|
||||
pub async fn post_questions() -> Response {
|
||||
//TODO
|
||||
(StatusCode::CREATED, "Post Questions").into_response()
|
||||
}
|
||||
pub async fn put_questions() -> Response {
|
||||
//TODO
|
||||
(StatusCode::CREATED, "Put Questions..").into_response()
|
||||
}
|
||||
pub async fn delete_questions() -> Response {
|
||||
//TODO
|
||||
(StatusCode::OK, "Delete Questions..").into_response()
|
||||
}
|
||||
pub async fn post_answers() -> Response {
|
||||
//TODO
|
||||
(StatusCode::CREATED, "Post Answers..").into_response()
|
||||
}
|
13
src/main.rs
13
src/main.rs
@ -1,8 +1,8 @@
|
||||
|
||||
mod api;
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
routing::get,
|
||||
routing::{delete, get, post, put},
|
||||
Router,
|
||||
};
|
||||
use std::net::SocketAddr;
|
||||
@ -13,8 +13,15 @@ async fn handle() -> Response {
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let app = Router::new().route("/", get(handle));
|
||||
let ip = SocketAddr::new([127, 0, 0, 1].into(), 3000);
|
||||
let listener = tokio::net::TcpListener::bind(ip).await.unwrap();
|
||||
let apis = Router::new()
|
||||
.route("/questions", get(api::get_questions))
|
||||
.route("/questions", post(api::post_questions))
|
||||
.route("/questions/:id", put(api::put_questions))
|
||||
.route("/questions", delete(api::delete_questions))
|
||||
.route("/answers", post(api::post_answers));
|
||||
let app = Router::new().route("/", get(handle)).merge(apis);
|
||||
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
Reference in New Issue
Block a user