Skip to content

Commit

Permalink
Upgrade to hyper 1 and friends (#69)
Browse files Browse the repository at this point in the history
* Upgrade to hyper 1 et al.

* cargo update

* Bump to 0.9.0 release of aws-lambda
  • Loading branch information
jonhoo authored Jan 2, 2024
1 parent c17c7e5 commit e310dd2
Show file tree
Hide file tree
Showing 11 changed files with 811 additions and 645 deletions.
1,327 changes: 749 additions & 578 deletions server/Cargo.lock

Large diffs are not rendered by default.

23 changes: 13 additions & 10 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ version = "0.1.0"
edition = "2021"

[dependencies]
aws-config = "0.54"
aws-sdk-dynamodb = "0.24"
aws-smithy-types = "0.54"
aws-smithy-http = "0.54"
axum = "0.6.10"
http = "0.2"
hyper = { version = "0.14", features = ["server", "http1", "http2"] }
lambda_http = { version = "0.7", default-features = false, features = ["apigw_http"] }
lambda_runtime = "0.7"
aws-config = { version = "1.0.1", features = ["behavior-version-latest"] }
aws-sdk-dynamodb = "1.1.0"
aws-smithy-types = "1.0.1"
aws-smithy-runtime-api = "1.0.0"
aws-smithy-http = "0.60"
axum = "0.7.2"
http = "1.0"
http-body-util = "0.1.0"
hyper = { version = "1.0.1", features = ["server", "http1", "http2"] }
# https://github.com/awslabs/aws-lambda-rust-runtime/issues/737
lambda_http = { version = "0.9.0", default-features = false, features = ["apigw_http"] }
lambda_runtime = "0.9.0"
rand = "0.8"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tokio = { version = "1", features = ["macros"] }
tower = "0.4"
tower-http = { version = "0.4", features = ["limit", "trace"] }
tower-http = { version = "0.5", features = ["limit", "trace"] }
tower-service = "0.3"
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt", "env-filter"] }
Expand Down
3 changes: 2 additions & 1 deletion server/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ AWS_SESSION_TOKEN=$(jq -r .Credentials.SessionToken <<<"$json")
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
export AWS_SESSION_TOKEN
cargo t -- --ignored
export AWS_REGION=us-east-1
cargo t "$@" -- --ignored
7 changes: 4 additions & 3 deletions server/src/ask.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::to_dynamo_timestamp;

use super::{Backend, Local};
use crate::to_dynamo_timestamp;
use aws_sdk_dynamodb::{
error::PutItemError, model::AttributeValue, output::PutItemOutput, types::SdkError,
error::SdkError,
operation::put_item::{PutItemError, PutItemOutput},
types::AttributeValue,
};
use axum::extract::{Path, State};
use axum::response::Json;
Expand Down
7 changes: 4 additions & 3 deletions server/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashMap;

use super::{Backend, Local};
use aws_sdk_dynamodb::{
error::GetItemError, model::AttributeValue, output::GetItemOutput, types::SdkError,
error::SdkError,
operation::get_item::{GetItemError, GetItemOutput},
types::AttributeValue,
};
use axum::{
extract::{Path, State},
Expand All @@ -14,6 +14,7 @@ use http::{
StatusCode,
};
use serde_json::Value;
use std::collections::HashMap;
use ulid::Ulid;

#[allow(unused_imports)]
Expand Down
23 changes: 8 additions & 15 deletions server/src/list.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use std::collections::HashMap;

use super::{Backend, Local};
use aws_sdk_dynamodb::{
error::{QueryError, QueryErrorKind, ResourceNotFoundException},
model::AttributeValue,
output::QueryOutput,
types::SdkError,
error::SdkError,
operation::query::{QueryError, QueryOutput},
types::{error::ResourceNotFoundException, AttributeValue},
};
use aws_smithy_types::Error;
use axum::response::Json;
use axum::{
extract::{Path, State},
Expand All @@ -17,6 +13,7 @@ use http::{
header::{self, HeaderName},
StatusCode,
};
use std::collections::HashMap;
use ulid::Ulid;

#[allow(unused_imports)]
Expand Down Expand Up @@ -59,12 +56,11 @@ impl Backend {
} = &mut *local;

if !events.contains_key(eid) {
return Err(super::mint_service_error(QueryError::new(
QueryErrorKind::ResourceNotFoundException(
return Err(super::mint_service_error(
QueryError::ResourceNotFoundException(
ResourceNotFoundException::builder().build(),
),
Error::builder().build(),
)));
));
}

let qs = questions_by_eid
Expand Down Expand Up @@ -197,10 +193,7 @@ async fn list_inner(
match dynamo.list(&eid, has_secret).await {
Ok(qs) => {
trace!(%eid, n = %qs.count(), "listed questions");
let questions: Vec<_> = qs
.items()
.map(|qs| qs.iter().filter_map(serialize_question).collect())
.unwrap_or_default();
let questions: Vec<_> = qs.items().iter().filter_map(serialize_question).collect();

let max_age = if has_secret {
// hosts should be allowed to see more up-to-date views
Expand Down
17 changes: 9 additions & 8 deletions server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use aws_sdk_dynamodb::{model::AttributeValue, types::SdkError};
use aws_smithy_http::body::SdkBody;
use aws_sdk_dynamodb::{error::SdkError, types::AttributeValue};
use aws_smithy_types::body::SdkBody;
use axum::response::IntoResponse;
use axum::routing::{get, post};
use axum::Router;
use http::StatusCode;
use http_body_util::BodyExt;
use lambda_http::Error;
use std::time::SystemTime;
use std::{
Expand Down Expand Up @@ -120,8 +121,9 @@ async fn check_secret(dynamo: &Backend, eid: &Ulid, secret: &str) -> Result<(),
fn mint_service_error<E>(e: E) -> SdkError<E> {
SdkError::service_error(
e,
aws_smithy_http::operation::Response::new(
http::Response::builder().body(SdkBody::empty()).unwrap(),
aws_smithy_runtime_api::http::Response::new(
aws_smithy_runtime_api::http::StatusCode::try_from(200).unwrap(),
SdkBody::empty(),
),
)
}
Expand Down Expand Up @@ -223,9 +225,8 @@ async fn main() -> Result<(), Error> {

if cfg!(debug_assertions) {
let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 3000));
Ok(axum::Server::bind(&addr)
.serve(app.into_make_service())
.await?)
let listener = tokio::net::TcpListener::bind(&addr).await?;
Ok(axum::serve(listener, app.into_make_service()).await?)
} else {
// If we compile in release mode, use the Lambda Runtime
// To run with AWS Lambda runtime, wrap in our `LambdaLayer`
Expand Down Expand Up @@ -286,7 +287,7 @@ where
let fut = async move {
let resp = fut.await?;
let (parts, body) = resp.into_response().into_parts();
let bytes = hyper::body::to_bytes(body).await?;
let bytes = body.collect().await?.to_bytes();
let bytes: &[u8] = &bytes;
let resp: hyper::Response<lambda_http::Body> = match std::str::from_utf8(bytes) {
Ok(s) => hyper::Response::from_parts(parts, s.into()),
Expand Down
9 changes: 6 additions & 3 deletions server/src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use crate::to_dynamo_timestamp;

use super::{Backend, Local};
use aws_sdk_dynamodb::{
error::PutItemError, model::AttributeValue, output::PutItemOutput, types::SdkError,
error::SdkError,
operation::put_item::{PutItemError, PutItemOutput},
types::AttributeValue,
};
use axum::extract::State;
use axum::response::Json;
Expand Down Expand Up @@ -63,8 +65,8 @@ impl Backend {
let qs = self.list(eid, false).await.unwrap();
let qids: Vec<_> = qs
.items()
.into_iter()
.flat_map(|qs| qs.iter().filter_map(|doc| doc["id"].as_s().ok()))
.iter()
.filter_map(|doc| doc["id"].as_s().ok())
.cloned()
.collect();

Expand Down Expand Up @@ -123,6 +125,7 @@ pub(super) async fn new(
}
Err(e) => {
error!(%eid, error = %e, "dynamodb request to create event failed");
eprintln!("{e:?}");
Err(http::StatusCode::INTERNAL_SERVER_ERROR)
}
}
Expand Down
16 changes: 8 additions & 8 deletions server/src/questions.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use std::collections::HashMap;

use super::{Backend, Local};
use aws_sdk_dynamodb::{
error::BatchGetItemError,
model::{AttributeValue, KeysAndAttributes},
output::BatchGetItemOutput,
types::SdkError,
error::SdkError,
operation::batch_get_item::{BatchGetItemError, BatchGetItemOutput},
types::{AttributeValue, KeysAndAttributes},
};
use axum::{
extract::{Path, State},
Expand All @@ -17,6 +14,7 @@ use http::{
StatusCode,
};
use serde_json::Value;
use std::collections::HashMap;
use ulid::Ulid;

#[allow(unused_imports)]
Expand Down Expand Up @@ -47,7 +45,8 @@ impl Backend {
.projection_expression("id,#text,#when,who")
.expression_attribute_names("#text", "text")
.expression_attribute_names("#when", "when")
.build(),
.build()
.expect("we're building correct things"),
)
.send()
.await
Expand All @@ -74,7 +73,8 @@ impl Backend {
KeysAndAttributes::builder()
.set_keys(Some(unprocessed))
.projection_expression("text,when,who")
.build(),
.build()
.expect("we build correctly"),
)]))
};

Expand Down
17 changes: 5 additions & 12 deletions server/src/toggle.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use crate::to_dynamo_timestamp;

use super::{Backend, Local};
use crate::to_dynamo_timestamp;
use aws_sdk_dynamodb::{
error::UpdateItemError, model::AttributeValue, output::UpdateItemOutput, types::SdkError,
error::SdkError,
operation::update_item::{UpdateItemError, UpdateItemOutput},
types::AttributeValue,
};
use axum::{
extract::{Path, State},
Json,
};
use http::StatusCode;
use serde::Deserialize;
use std::{collections::HashMap, time::SystemTime};
use std::time::SystemTime;
use ulid::Ulid;

#[allow(unused_imports)]
Expand Down Expand Up @@ -64,14 +65,6 @@ impl Backend {
let mut local = local.lock().unwrap();
let Local { questions, .. } = &mut *local;

fn invert(q: &mut HashMap<&'static str, AttributeValue>, key: &'static str) {
if let AttributeValue::Bool(b) = q[key] {
q.insert(key, AttributeValue::Bool(!b));
} else {
unreachable!("all properties are bools");
}
}

let q = questions
.get_mut(qid)
.expect("toggle property on unknown question ");
Expand Down
7 changes: 3 additions & 4 deletions server/src/vote.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use super::{Backend, Local};
use aws_sdk_dynamodb::{
error::UpdateItemError,
model::{AttributeValue, ReturnValue},
output::UpdateItemOutput,
types::SdkError,
error::SdkError,
operation::update_item::{UpdateItemError, UpdateItemOutput},
types::{AttributeValue, ReturnValue},
};
use axum::extract::{Path, State};
use axum::response::Json;
Expand Down

0 comments on commit e310dd2

Please sign in to comment.