mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-06 09:15:26 +00:00
Clean up the example a touch
This commit is contained in:
parent
017ee38725
commit
a23bfb60eb
@ -1,5 +1,5 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "sqlx-example-realworld"
|
name = "sqlx-example-realworld-postgres"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
workspace = "../.."
|
workspace = "../.."
|
||||||
|
27
examples/realworld-postgres/README.md
Normal file
27
examples/realworld-postgres/README.md
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
# Real World SQLx
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Declare the database URL.
|
||||||
|
|
||||||
|
```
|
||||||
|
export DATABASE_URL="postgres://postgres@localhost/realworld"
|
||||||
|
```
|
||||||
|
|
||||||
|
Create the database.
|
||||||
|
|
||||||
|
```
|
||||||
|
createdb -U postgres realworld
|
||||||
|
```
|
||||||
|
|
||||||
|
Load the database schema.
|
||||||
|
|
||||||
|
```
|
||||||
|
psql -d "$DATABASE_URL" -f ./schema.sql
|
||||||
|
```
|
||||||
|
|
||||||
|
Run.
|
||||||
|
|
||||||
|
```
|
||||||
|
cargo run
|
||||||
|
```
|
@ -1,7 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
# Get current directory (of this script)
|
|
||||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
|
|
||||||
|
|
||||||
# Run schema file
|
|
||||||
psql -d "$DATABASE_URL" -f schema.sql
|
|
@ -1,18 +1,22 @@
|
|||||||
use sqlx::{Pool, Postgres};
|
use sqlx::PgPool;
|
||||||
use std::env;
|
use std::env;
|
||||||
use tide::{Request, Response};
|
use tide::{Request, Response};
|
||||||
|
|
||||||
|
// NOTE: Tide 0.5.x does not handle errors so any fallible methods just [.unwrap] for the moment.
|
||||||
|
// To be clear, that is not recommended and this should be fixed as soon as Tide fixes its
|
||||||
|
// error handling.
|
||||||
|
|
||||||
#[async_std::main]
|
#[async_std::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
dotenv::dotenv()?;
|
dotenv::dotenv()?;
|
||||||
|
|
||||||
let pool = Pool::<Postgres>::new(&env::var("DATABASE_URL")?).await?;
|
let pool = PgPool::new(&env::var("DATABASE_URL")?).await?;
|
||||||
|
|
||||||
let mut server = tide::with_state(pool);
|
let mut server = tide::with_state(pool);
|
||||||
|
|
||||||
server.at("/api/users").post(register);
|
server.at("/api/users").post(register);
|
||||||
|
|
||||||
server.listen(("localhost", 8080)).await?;
|
server.listen(("0.0.0.0", 8080)).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -21,21 +25,24 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
// https://github.com/gothinkster/realworld/tree/master/api#registration
|
// https://github.com/gothinkster/realworld/tree/master/api#registration
|
||||||
|
|
||||||
// #[post("/api/users")]
|
// #[post("/api/users")]
|
||||||
async fn register(mut req: Request<Pool<Postgres>>) -> Response {
|
async fn register(mut req: Request<PgPool>) -> Response {
|
||||||
#[derive(serde::Deserialize)]
|
#[derive(serde::Deserialize)]
|
||||||
struct RegisterRequestBody {
|
struct RegisterRequestBody {
|
||||||
username: String,
|
username: String,
|
||||||
email: String,
|
email: String,
|
||||||
// TODO: password: String,
|
password: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Handle the unwrap
|
|
||||||
let body: RegisterRequestBody = req.body_json().await.unwrap();
|
let body: RegisterRequestBody = req.body_json().await.unwrap();
|
||||||
|
|
||||||
let mut pool = req.state();
|
let mut pool = req.state();
|
||||||
|
|
||||||
// TODO: Handle the unwrap
|
|
||||||
let (user_id,): (i64,) = sqlx::query!(
|
let (user_id,): (i64,) = sqlx::query!(
|
||||||
"INSERT INTO users (username, email) VALUES ($1, $2) RETURNING id",
|
r#"
|
||||||
|
INSERT INTO users ( username, email )
|
||||||
|
VALUES ( $1, $2 )
|
||||||
|
RETURNING id
|
||||||
|
"#,
|
||||||
&*body.username,
|
&*body.username,
|
||||||
&*body.email
|
&*body.email
|
||||||
)
|
)
|
||||||
@ -48,7 +55,6 @@ async fn register(mut req: Request<Pool<Postgres>>) -> Response {
|
|||||||
id: i64,
|
id: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Handle the unwrap
|
|
||||||
Response::new(200)
|
Response::new(200)
|
||||||
.body_json(&RegisterResponseBody { id: user_id })
|
.body_json(&RegisterResponseBody { id: user_id })
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -201,5 +201,5 @@ macro_rules! impl_fmt_error {
|
|||||||
f.pad(self.message())
|
f.pad(self.message())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ pub use sqlx_core::query_as_mapped;
|
|||||||
pub use sqlx_core::mysql::{self, MySql, MySqlConnection, MySqlPool};
|
pub use sqlx_core::mysql::{self, MySql, MySqlConnection, MySqlPool};
|
||||||
|
|
||||||
#[cfg(feature = "postgres")]
|
#[cfg(feature = "postgres")]
|
||||||
pub use sqlx_core::postgres::{self, Postgres, PgConnection, PgPool};
|
pub use sqlx_core::postgres::{self, PgConnection, PgPool, Postgres};
|
||||||
|
|
||||||
#[cfg(feature = "macros")]
|
#[cfg(feature = "macros")]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user