switch to axum
This commit is contained in:
parent
39570aae05
commit
6a039f350a
10
Cargo.toml
10
Cargo.toml
@ -6,6 +6,12 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rocket = "0.5.0-rc.2"
|
||||
tokio = { version = "1.27", features = ["full"] }
|
||||
axum = "0.6"
|
||||
sqlx = { version = "0.6", features = ["postgres", "runtime-tokio-rustls"] }
|
||||
serde = "1.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
|
||||
|
||||
|
||||
6
migrations/20230402213003_user.sql
Normal file
6
migrations/20230402213003_user.sql
Normal file
@ -0,0 +1,6 @@
|
||||
CREATE TABLE users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
username varchar(255) NOT NULL,
|
||||
password bytea
|
||||
);
|
||||
|
||||
91
src/main.rs
91
src/main.rs
@ -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,
|
||||
}
|
||||
|
||||
9
src/user.rs
Normal file
9
src/user.rs
Normal file
@ -0,0 +1,9 @@
|
||||
use rocket::Route;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![
|
||||
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user