From e47e90ec75f6242cf2cee2097a92d27bdc2111da Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Thu, 21 Nov 2019 23:29:40 +0000 Subject: [PATCH] Add small tide example --- .gitignore | 3 ++ Cargo.toml | 7 ++++ examples/tide/Cargo.toml | 15 +++++++++ examples/tide/src/main.rs | 71 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 examples/tide/Cargo.toml create mode 100644 examples/tide/src/main.rs diff --git a/.gitignore b/.gitignore index fb667367..ca1cc0c5 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,6 @@ Cargo.lock .idea/ *.vim *.vi + +# Environment +.env diff --git a/Cargo.toml b/Cargo.toml index 2ccf58f5..fb7b381d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,10 @@ +[workspace] +members = [ + ".", + "sqlx-macros", + "examples/tide" +] + [package] name = "sqlx" version = "0.1.1-pre" diff --git a/examples/tide/Cargo.toml b/examples/tide/Cargo.toml new file mode 100644 index 00000000..0f159d49 --- /dev/null +++ b/examples/tide/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "sqlx-example-tide" +version = "0.1.0" +edition = "2018" +workspace = "../.." + +[dependencies] +anyhow = "1.0.22" +dotenv = "0.15.0" +# async-std = "1.1.0" +# tide = { git = "https://github.com/http-rs/tide", rev = "eb57bcb2ba07a77c91ad3952ca0a0143b03009bc" } +tide = "0.3.0" +tokio = { version = "0.2.0-alpha.4", default-features = false, features = [ "rt-full" ] } +sqlx = { path = "../..", features = [ "postgres" ] } +serde = { version = "1", features = [ "derive"] } \ No newline at end of file diff --git a/examples/tide/src/main.rs b/examples/tide/src/main.rs new file mode 100644 index 00000000..de9211fb --- /dev/null +++ b/examples/tide/src/main.rs @@ -0,0 +1,71 @@ +use sqlx::{Pool, Postgres}; +use std::env; +use tide::error::ResultExt; +use tide::response; +use tide::EndpointResult; +use tide::http::StatusCode; +use tide::{App, Context}; + +// #[async_std::main] +#[tokio::main] +async fn main() -> anyhow::Result<()> { + dotenv::dotenv()?; + + let pool = Pool::::new(&env::var("DATABASE_URL")?).await?; + + run_migrations(&pool).await?; + + let mut app = App::with_state(pool); + + app.at("/v1/user").get(get_all_users).post(create_user); + + app.serve(("localhost", 8080))?; + + Ok(()) +} + +async fn run_migrations(mut pool: &Pool) -> anyhow::Result<()> { + let _ = sqlx::query( + r#" +CREATE TABLE IF NOT EXISTS users ( + id INT GENERATED ALWAYS AS IDENTITY, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), + name TEXT NOT NULL +); + "#, + ) + .execute(&mut pool) + .await?; + + Ok(()) +} + +async fn get_all_users(cx: Context>) -> EndpointResult { + let mut pool = cx.state(); + + let users: Vec<(i32, String)> = sqlx::query(r#"SELECT id, name FROM users"#) + .fetch_all(&mut pool) + .await + .server_err()?; + + Ok(response::json(users)) +} + +#[derive(serde::Deserialize)] +struct CreateUserRequest { + name: String, +} + +async fn create_user(mut cx: Context>) -> EndpointResult { + let req_body: CreateUserRequest = cx.body_json().await.client_err()?; + + let mut pool = cx.state(); + + let _ = sqlx::query(r#"INSERT INTO users ( name ) VALUES ( $1 )"#) + .bind(req_body.name) + .execute(&mut pool) + .await + .server_err()?; + + Ok(StatusCode::CREATED) +}