From be74d2eab445eaba17b3eb1c9866755f582642c1 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 19 Dec 2023 11:04:49 +0100 Subject: [PATCH] postgres: remove hyper example --- postgres/Cargo.toml | 1 - postgres/examples/hyper.rs | 61 -------------------------------------- 2 files changed, 62 deletions(-) delete mode 100644 postgres/examples/hyper.rs diff --git a/postgres/Cargo.toml b/postgres/Cargo.toml index 9fc945b..79bb758 100644 --- a/postgres/Cargo.toml +++ b/postgres/Cargo.toml @@ -26,5 +26,4 @@ tokio-postgres = "0.7" [dev-dependencies] futures-util = "0.3.2" -hyper = { version = "0.14.1", features = ["http1", "server", "tcp"] } tokio = { version = "1.0.0", features = ["macros", "rt-multi-thread"] } diff --git a/postgres/examples/hyper.rs b/postgres/examples/hyper.rs deleted file mode 100644 index 005b2a0..0000000 --- a/postgres/examples/hyper.rs +++ /dev/null @@ -1,61 +0,0 @@ -use std::convert::Infallible; - -use bb8::{Pool, RunError}; -use bb8_postgres::PostgresConnectionManager; -use hyper::service::{make_service_fn, service_fn}; -use hyper::{Body, Error, Response, Server}; -use tokio_postgres::NoTls; - -// Select some static data from a Postgres DB and return it via hyper. -// -// The simplest way to start the db is using Docker: -// docker run --name gotham-middleware-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres -#[tokio::main] -async fn main() { - let addr = ([127, 0, 0, 1], 3000).into(); - - let pg_mgr = PostgresConnectionManager::new_from_stringlike( - "postgresql://postgres:mysecretpassword@localhost:5432", - NoTls, - ) - .unwrap(); - - let pool = match Pool::builder().build(pg_mgr).await { - Ok(pool) => pool, - Err(e) => panic!("bb8 error {e}"), - }; - - let _ = Server::bind(&addr) - .serve(make_service_fn(move |_| { - let pool = pool.clone(); - async move { - Ok::<_, Error>(service_fn(move |_| { - let pool = pool.clone(); - async move { - println!("Got request"); - Ok::<_, Infallible>(match handler(pool).await { - Ok(rsp) => { - println!("Sending success response"); - rsp - } - Err(e) => { - println!("Sending error response"); - Response::new(Body::from(format!("Internal error {e:?}"))) - } - }) - } - })) - } - })) - .await; -} - -async fn handler( - pool: Pool>, -) -> Result, RunError> { - let conn = pool.get().await?; - let stmt = conn.prepare("SELECT 1").await?; - let row = conn.query_one(&stmt, &[]).await?; - let v = row.get::(0); - Ok(Response::new(Body::from(format!("Got results {v:?}")))) -}