From a23bfb60ebccf04d3fba340f08437930d0b6514e Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Fri, 27 Dec 2019 21:47:51 -0800 Subject: [PATCH] Clean up the example a touch --- examples/realworld-postgres/Cargo.toml | 2 +- examples/realworld-postgres/README.md | 27 +++++++++++++++++++++++++ examples/realworld-postgres/setup.sh | 7 ------- examples/realworld-postgres/src/main.rs | 24 +++++++++++++--------- sqlx-core/src/error.rs | 2 +- src/lib.rs | 2 +- 6 files changed, 45 insertions(+), 19 deletions(-) create mode 100644 examples/realworld-postgres/README.md delete mode 100644 examples/realworld-postgres/setup.sh diff --git a/examples/realworld-postgres/Cargo.toml b/examples/realworld-postgres/Cargo.toml index 73d9dec4..7915f732 100644 --- a/examples/realworld-postgres/Cargo.toml +++ b/examples/realworld-postgres/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "sqlx-example-realworld" +name = "sqlx-example-realworld-postgres" version = "0.1.0" edition = "2018" workspace = "../.." diff --git a/examples/realworld-postgres/README.md b/examples/realworld-postgres/README.md new file mode 100644 index 00000000..aa6c40c6 --- /dev/null +++ b/examples/realworld-postgres/README.md @@ -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 +``` diff --git a/examples/realworld-postgres/setup.sh b/examples/realworld-postgres/setup.sh deleted file mode 100644 index 68581759..00000000 --- a/examples/realworld-postgres/setup.sh +++ /dev/null @@ -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 diff --git a/examples/realworld-postgres/src/main.rs b/examples/realworld-postgres/src/main.rs index 270cdaff..69f3d500 100644 --- a/examples/realworld-postgres/src/main.rs +++ b/examples/realworld-postgres/src/main.rs @@ -1,18 +1,22 @@ -use sqlx::{Pool, Postgres}; +use sqlx::PgPool; use std::env; 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 fn main() -> anyhow::Result<()> { dotenv::dotenv()?; - let pool = Pool::::new(&env::var("DATABASE_URL")?).await?; + let pool = PgPool::new(&env::var("DATABASE_URL")?).await?; let mut server = tide::with_state(pool); server.at("/api/users").post(register); - server.listen(("localhost", 8080)).await?; + server.listen(("0.0.0.0", 8080)).await?; Ok(()) } @@ -21,21 +25,24 @@ async fn main() -> anyhow::Result<()> { // https://github.com/gothinkster/realworld/tree/master/api#registration // #[post("/api/users")] -async fn register(mut req: Request>) -> Response { +async fn register(mut req: Request) -> Response { #[derive(serde::Deserialize)] struct RegisterRequestBody { username: String, email: String, - // TODO: password: String, + password: String, } - // TODO: Handle the unwrap let body: RegisterRequestBody = req.body_json().await.unwrap(); + let mut pool = req.state(); - // TODO: Handle the unwrap 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.email ) @@ -48,7 +55,6 @@ async fn register(mut req: Request>) -> Response { id: i64, } - // TODO: Handle the unwrap Response::new(200) .body_json(&RegisterResponseBody { id: user_id }) .unwrap() diff --git a/sqlx-core/src/error.rs b/sqlx-core/src/error.rs index 6c3b2a59..75840a07 100644 --- a/sqlx-core/src/error.rs +++ b/sqlx-core/src/error.rs @@ -201,5 +201,5 @@ macro_rules! impl_fmt_error { f.pad(self.message()) } } - } + }; } diff --git a/src/lib.rs b/src/lib.rs index 044d969f..465c23e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ pub use sqlx_core::query_as_mapped; pub use sqlx_core::mysql::{self, MySql, MySqlConnection, MySqlPool}; #[cfg(feature = "postgres")] -pub use sqlx_core::postgres::{self, Postgres, PgConnection, PgPool}; +pub use sqlx_core::postgres::{self, PgConnection, PgPool, Postgres}; #[cfg(feature = "macros")] #[doc(hidden)]