Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example using rustls, without native bindings to openssl #43

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions poem/postgres-rustls/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "postgres-rustls"
version = "0.1.0"
edition = "2021"

[dependencies]
poem = "1.3.55"
serde = "1.0.148"
shuttle-poem = { version = "0.15.0" }
shuttle-runtime = { version = "0.15.0" }
shuttle-shared-db = { version = "0.15.0", features = ["postgres-rustls"] }
sqlx = { version = "0.6.2", features = ["runtime-tokio-rustls", "postgres"] }
tokio = { version = "1.26.0" }
1 change: 1 addition & 0 deletions poem/postgres-rustls/Shuttle.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name = "postgres-rustls-poem-app"
6 changes: 6 additions & 0 deletions poem/postgres-rustls/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DROP TABLE IF EXISTS todos;

CREATE TABLE todos (
id serial PRIMARY KEY,
note TEXT NOT NULL
);
61 changes: 61 additions & 0 deletions poem/postgres-rustls/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use poem::{
error::BadRequest,
get, handler,
middleware::AddData,
post,
web::{Data, Json, Path},
EndpointExt, Result, Route,
};
use serde::{Deserialize, Serialize};
use shuttle_runtime::CustomError;
use shuttle_poem::ShuttlePoem;
use sqlx::{Executor, FromRow, PgPool};

#[handler]
async fn retrieve(Path(id): Path<i32>, state: Data<&PgPool>) -> Result<Json<Todo>> {
let todo = sqlx::query_as("SELECT * FROM todos WHERE id = $1")
.bind(id)
.fetch_one(state.0)
.await
.map_err(BadRequest)?;

Ok(Json(todo))
}

#[handler]
async fn add(Json(data): Json<TodoNew>, state: Data<&PgPool>) -> Result<Json<Todo>> {
let todo = sqlx::query_as("INSERT INTO todos(note) VALUES ($1) RETURNING id, note")
.bind(&data.note)
.fetch_one(state.0)
.await
.map_err(BadRequest)?;

Ok(Json(todo))
}

#[shuttle_runtime::main]
async fn poem(
#[shuttle_shared_db::Postgres] pool: PgPool,
) -> ShuttlePoem<impl poem::Endpoint> {
pool.execute(include_str!("../schema.sql"))
.await
.map_err(CustomError::new)?;

let app = Route::new()
.at("/todo", post(add))
.at("/todo/:id", get(retrieve))
.with(AddData::new(pool));

Ok(app.into())
}

#[derive(Deserialize)]
struct TodoNew {
pub note: String,
}

#[derive(Serialize, FromRow)]
struct Todo {
pub id: i32,
pub note: String,
}