Skip to content

Commit

Permalink
bug: Fix thruster postgres example (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
trezm authored Oct 21, 2022
1 parent 1b983e8 commit 7c05afc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/thruster/hello-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ edition = "2021"

[dependencies]
shuttle-service = { version = "0.7.0", features = ["web-thruster"] }
thruster = { version = "1.2.6", features = ["hyper_server"] }
thruster = { version = "1.3.0", features = ["hyper_server"] }
2 changes: 1 addition & 1 deletion examples/thruster/postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ serde_json = { version = "1.0" }
shuttle-aws-rds = { version = "0.7.0", features = ["postgres"] }
shuttle-service = { version = "0.7.0", features = ["web-thruster"] }
sqlx = { version = "0.6", features = ["runtime-tokio-native-tls", "postgres"] }
thruster = { version = "1.2.6", features = ["hyper_server"] }
thruster = { version = "1.3.0", features = ["hyper_server"] }
19 changes: 10 additions & 9 deletions examples/thruster/postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use serde::{Deserialize, Serialize};
use shuttle_service::error::CustomError;
use sqlx::{Executor, FromRow, PgPool};
use thruster::{
context::{hyper_request::HyperRequest, typed_hyper_context::TypedHyperContext},
context::{
context_ext::ContextExt, hyper_request::HyperRequest,
typed_hyper_context::TypedHyperContext,
},
errors::{ErrorSet, ThrusterError},
m, middleware_fn, App, Context, HyperServer, MiddlewareNext, MiddlewareResult, ThrusterServer,
};
Expand Down Expand Up @@ -41,14 +44,15 @@ fn generate_context(request: HyperRequest, state: &ServerConfig, _path: &str) ->
#[middleware_fn]
async fn retrieve(mut context: Ctx, _next: MiddlewareNext<Ctx>) -> MiddlewareResult<Ctx> {
let id: i32 = context
.query_params
.params()
.get("id")
.ok_or_else(|| {
ThrusterError::parsing_error(
Ctx::new_without_request(context.extra.clone()),
"id is required",
)
})?
.param
.parse()
.map_err(|_e| {
ThrusterError::parsing_error(
Expand All @@ -71,19 +75,16 @@ async fn retrieve(mut context: Ctx, _next: MiddlewareNext<Ctx>) -> MiddlewareRes
}

#[middleware_fn]
async fn add(context: Ctx, _next: MiddlewareNext<Ctx>) -> MiddlewareResult<Ctx> {
async fn add(mut context: Ctx, _next: MiddlewareNext<Ctx>) -> MiddlewareResult<Ctx> {
let extra = context.extra.clone();

let (body, mut context) = context
.get_body()
let todo_req = context
.get_json::<TodoNew>()
.await
.map_err(|_e| ThrusterError::generic_error(Ctx::new_without_request(extra)))?;
let data: TodoNew = serde_json::from_str(&body).map_err(|_e| {
ThrusterError::generic_error(Ctx::new_without_request(context.extra.clone()))
})?;

let todo: Todo = sqlx::query_as("INSERT INTO todos(note) VALUES ($1) RETURNING id, note")
.bind(&data.note)
.bind(&todo_req.note)
.fetch_one(&context.extra.pool)
.await
.map_err(|_e| {
Expand Down

0 comments on commit 7c05afc

Please sign in to comment.