switch to axum
This commit is contained in:
+76
-15
@@ -1,22 +1,83 @@
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
response::IntoResponse,
|
||||
routing::{get, post},
|
||||
Extension, Json, Router,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
use sqlx::PgPool;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use rocket::response::status::BadRequest;
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// initialize tracing
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
#[get("/event")]
|
||||
fn events() -> String {
|
||||
format!("event list")
|
||||
PgPool::connect(
|
||||
"postgresql://school_app_api_user:school_app_api_pass@localhost/school_app_api",
|
||||
);
|
||||
|
||||
let db_url = std::env::var("DATABASE_URL")
|
||||
.expect("Set `DATABASE_URL` to the url of the postgres database.");
|
||||
|
||||
let pool = PgPoolOptions::new()
|
||||
.max_connections(50)
|
||||
.connect(&db_url)
|
||||
.await
|
||||
.context("could not connect to database_url")?;
|
||||
|
||||
// build our application with a route
|
||||
let app = Router::new()
|
||||
// `GET /` goes to `root`
|
||||
.route("/", get(root))
|
||||
// `POST /users` goes to `create_user`
|
||||
.route("/user/signup", post(signup))
|
||||
.layer(Extension(pool));
|
||||
|
||||
// run our app with hyper
|
||||
// `axum::Server` is a re-export of `hyper::Server`
|
||||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||
tracing::info!("listening on {}", addr);
|
||||
axum::Server::bind(&addr)
|
||||
.serve(app.into_make_service())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[get("/event/<id>")]
|
||||
fn event_id(id: Result<u64, &str>) -> Result<String, BadRequest<String>> {
|
||||
match id {
|
||||
Ok(id) => Ok(format!("event for {}", id)),
|
||||
Err(_) => Err(BadRequest(Some(format!("event id must be a valid u64")))),
|
||||
}
|
||||
// basic handler that responds with a static string
|
||||
async fn root() -> &'static str {
|
||||
"Hello, World!"
|
||||
}
|
||||
|
||||
#[launch]
|
||||
fn rocket() -> _ {
|
||||
rocket::build().mount("/", routes![events, event_id])
|
||||
async fn signup(
|
||||
// this argument tells axum to parse the request body
|
||||
// as JSON into a `CreateUser` type
|
||||
Json(payload): Json<Signup>,
|
||||
Extension(pool): Extension<PgPool>,
|
||||
) -> impl IntoResponse {
|
||||
// insert your application logic here
|
||||
sqlx::query!(
|
||||
r#"
|
||||
|
||||
"#
|
||||
);
|
||||
|
||||
// this will be converted into a JSON response
|
||||
// with a status code of `201 Created`
|
||||
(StatusCode::CREATED, Json(user))
|
||||
}
|
||||
|
||||
// the input to our `create_user` handler
|
||||
#[derive(Deserialize)]
|
||||
struct Signup {
|
||||
username: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
// the output to our `create_user` handler
|
||||
#[derive(Serialize, sqlx::FromRow)]
|
||||
struct User {
|
||||
id: u64,
|
||||
username: String,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
use rocket::Route;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![
|
||||
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user