-
Notifications
You must be signed in to change notification settings - Fork 19
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 a dedicated lambda
bin
#218
Open
rustworthy
wants to merge
6
commits into
jonhoo:main
Choose a base branch
from
rustworthy:release-not-special
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
301761d
Factor out build app core logic to lib
rustworthy a61d1f0
Ty clippy
rustworthy 5682625
Update deploy docs
rustworthy 7488ece
Add --bin lambda to CI jobs
rustworthy 812bfe2
Allow Backend::local unused
rustworthy 15ff917
Add default run to Cargo.toml
rustworthy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,7 +68,7 @@ jobs: | |
- name: Install zig for cargo-lambda | ||
run: sudo snap install zig --classic --beta | ||
|
||
- run: cargo lambda build --release --arm64 | ||
- run: cargo lambda build --release --arm64 --bin lambda | ||
working-directory: ./server | ||
|
||
- uses: hashicorp/tfc-workflows-github/actions/[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,7 +66,7 @@ jobs: | |
- name: Install zig for cargo-lambda | ||
run: sudo snap install zig --classic --beta | ||
|
||
- run: cargo lambda build --release --arm64 | ||
- run: cargo lambda build --release --arm64 --bin lambda | ||
working-directory: ./server | ||
|
||
- uses: hashicorp/tfc-workflows-github/actions/[email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use axum::response::IntoResponse; | ||
use http_body_util::BodyExt; | ||
use lambda_http::Error; | ||
use std::{future::Future, pin::Pin}; | ||
use tower::Layer; | ||
use tower_http::trace::TraceLayer; | ||
use tower_service::Service; | ||
use tracing_subscriber::EnvFilter; | ||
use wewerewondering_api::build_app; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
tracing_subscriber::fmt() | ||
.with_env_filter(EnvFilter::from_default_env()) | ||
.without_time(/* cloudwatch does that */) | ||
.init(); | ||
|
||
let app = build_app().await; | ||
// To run with AWS Lambda runtime, wrap in our `LambdaLayer` | ||
let app = tower::ServiceBuilder::new() | ||
.layer(TraceLayer::new_for_http()) | ||
.layer(LambdaLayer) | ||
.service(app); | ||
|
||
lambda_http::run(app).await | ||
} | ||
|
||
#[derive(Clone, Copy)] | ||
pub struct LambdaLayer; | ||
|
||
impl<S> Layer<S> for LambdaLayer { | ||
type Service = LambdaService<S>; | ||
|
||
fn layer(&self, inner: S) -> Self::Service { | ||
LambdaService { inner } | ||
} | ||
} | ||
|
||
pub struct LambdaService<S> { | ||
inner: S, | ||
} | ||
|
||
impl<S> Service<lambda_http::Request> for LambdaService<S> | ||
where | ||
S: Service<axum::http::Request<axum::body::Body>>, | ||
S::Response: axum::response::IntoResponse + Send + 'static, | ||
S::Error: std::error::Error + Send + Sync + 'static, | ||
S::Future: Send + 'static, | ||
{ | ||
type Response = lambda_http::Response<lambda_http::Body>; | ||
type Error = lambda_http::Error; | ||
type Future = | ||
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>; | ||
|
||
fn poll_ready( | ||
&mut self, | ||
cx: &mut std::task::Context<'_>, | ||
) -> std::task::Poll<Result<(), Self::Error>> { | ||
self.inner.poll_ready(cx).map_err(Into::into) | ||
} | ||
|
||
fn call(&mut self, req: lambda_http::Request) -> Self::Future { | ||
let (parts, body) = req.into_parts(); | ||
let body = match body { | ||
lambda_http::Body::Empty => axum::body::Body::default(), | ||
lambda_http::Body::Text(t) => t.into(), | ||
lambda_http::Body::Binary(v) => v.into(), | ||
}; | ||
|
||
let request = axum::http::Request::from_parts(parts, body); | ||
|
||
let fut = self.inner.call(request); | ||
let fut = async move { | ||
let resp = fut.await?; | ||
let (parts, body) = resp.into_response().into_parts(); | ||
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()), | ||
Err(_) => hyper::Response::from_parts(parts, bytes.into()), | ||
}; | ||
Ok(resp) | ||
}; | ||
|
||
Box::pin(fut) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
use aws_sdk_dynamodb::types::AttributeValue; | ||
use axum::routing::{get, post}; | ||
use axum::Router; | ||
use std::collections::HashMap; | ||
use std::sync::{Arc, Mutex}; | ||
use std::time::Duration; | ||
use tower_http::limit::RequestBodyLimitLayer; | ||
use ulid::Ulid; | ||
|
||
mod ask; | ||
mod event; | ||
mod list; | ||
mod new; | ||
mod questions; | ||
mod toggle; | ||
mod utils; | ||
mod vote; | ||
|
||
pub use ask::Question; | ||
pub use utils::to_dynamo_timestamp; | ||
pub use vote::UpDown; | ||
|
||
#[cfg(debug_assertions)] | ||
const SEED: &str = include_str!("test.json"); | ||
|
||
const QUESTIONS_EXPIRE_AFTER_DAYS: u64 = 30; | ||
const QUESTIONS_TTL: Duration = Duration::from_secs(QUESTIONS_EXPIRE_AFTER_DAYS * 24 * 60 * 60); | ||
|
||
const EVENTS_EXPIRE_AFTER_DAYS: u64 = 60; | ||
const EVENTS_TTL: Duration = Duration::from_secs(EVENTS_EXPIRE_AFTER_DAYS * 24 * 60 * 60); | ||
|
||
#[derive(Clone, Debug, Default)] | ||
pub struct Local { | ||
pub events: HashMap<Ulid, String>, | ||
pub questions: HashMap<Ulid, HashMap<&'static str, AttributeValue>>, | ||
pub questions_by_eid: HashMap<Ulid, Vec<Ulid>>, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum Backend { | ||
Dynamo(aws_sdk_dynamodb::Client), | ||
Local(Arc<Mutex<Local>>), | ||
} | ||
|
||
impl Backend { | ||
async fn local() -> Self { | ||
Backend::Local(Arc::new(Mutex::new(Local::default()))) | ||
} | ||
|
||
/// Instantiate a DynamoDB backend. | ||
/// | ||
/// If `USE_DYNAMODB` is set to "local", the `AWS_ENDPOINT_URL` will be taken | ||
/// from the environment with the "http://localhost:8000" fallback , the `AWS_DEFAULT_REGION` | ||
/// will be pulled from the environment as well and will default to "us-east-1", | ||
/// as for the credentials - the [test credentials](https://docs.rs/aws-config/latest/aws_config/struct.ConfigLoader.html#method.test_credentials) | ||
/// will be used to sign requests. | ||
/// | ||
/// This spares setting those environment variables (including `AWS_ACCESS_KEY_ID` | ||
/// and `AWS_SECRET_ACCESS_KEY`) via the command line or configuration files, | ||
/// and allows to run the application against a local dynamodb instance with just: | ||
/// ```sh | ||
/// USE_DYNAMODB=local cargo run | ||
/// ``` | ||
/// While the entire test suite can be run with: | ||
/// ```sh | ||
/// USE_DYNAMODB=local cargo t -- --include-ignored | ||
/// ``` | ||
/// | ||
/// This also allows us to use the local instance of DynamoDB which is running | ||
/// in a container on the same network, in which case the database will be accessible | ||
/// under `http://<dynamodb_container_name>:<port>`. This facilitates the setup of | ||
/// local API Gateway with SAM, since the `sam local start-api` command will launch our | ||
/// back-end app in a docker container. | ||
/// | ||
/// If more customization is needed (say, you want to set some specific credentials | ||
/// rather than rely on those test creds generated by the `aws_config` crate), | ||
/// set `USE_DYNAMODB` to e.g. "custom", and set the environment variables to whatever | ||
/// values you need or let them be picked up from your `~/.aws` files | ||
/// (see [`aws_config::load_from_env`](https://docs.rs/aws-config/latest/aws_config/fn.load_from_env.html)) | ||
pub async fn dynamo() -> Self { | ||
let config = if std::env::var("USE_DYNAMODB") | ||
.ok() | ||
.is_some_and(|v| v == "local") | ||
{ | ||
aws_config::from_env() | ||
.endpoint_url( | ||
std::env::var("AWS_ENDPOINT_URL") | ||
.ok() | ||
.unwrap_or("http://localhost:8000".into()), | ||
) | ||
.region(aws_config::Region::new( | ||
std::env::var("AWS_DEFAULT_REGION") | ||
.ok() | ||
.unwrap_or("us-east-1".into()), | ||
)) | ||
.test_credentials() | ||
.load() | ||
.await | ||
} else { | ||
aws_config::load_from_env().await | ||
}; | ||
Backend::Dynamo(aws_sdk_dynamodb::Client::new(&config)) | ||
} | ||
} | ||
|
||
pub async fn build_app() -> Router { | ||
#[cfg(not(debug_assertions))] | ||
let backend = Backend::dynamo().await; | ||
|
||
#[cfg(debug_assertions)] | ||
let backend = { | ||
use rand::prelude::SliceRandom; | ||
|
||
let mut backend = if std::env::var_os("USE_DYNAMODB").is_some() { | ||
Backend::dynamo().await | ||
} else { | ||
Backend::local().await | ||
}; | ||
|
||
// to aid in development, seed the backend with a test event and related | ||
// questions, and auto-generate user votes over time | ||
let qids = crate::utils::seed(&mut backend).await; | ||
let cheat = backend.clone(); | ||
tokio::spawn(async move { | ||
let mut interval = tokio::time::interval(Duration::from_secs(1)); | ||
interval.tick().await; | ||
loop { | ||
interval.tick().await; | ||
let qid = qids | ||
.choose(&mut rand::thread_rng()) | ||
.expect("there _are_ some questions for our test event"); | ||
let _ = cheat.vote(qid, vote::UpDown::Up).await; | ||
} | ||
}); | ||
|
||
backend | ||
}; | ||
|
||
Router::new() | ||
.route("/api/event", post(new::new)) | ||
.route("/api/event/{eid}", post(ask::ask)) | ||
.route("/api/event/{eid}", get(event::event)) | ||
.route("/api/event/{eid}/questions", get(list::list)) | ||
.route("/api/event/{eid}/questions/{secret}", get(list::list_all)) | ||
.route( | ||
"/api/event/{eid}/questions/{secret}/{qid}/toggle/{property}", | ||
post(toggle::toggle), | ||
) | ||
.route("/api/vote/{qid}/{updown}", post(vote::vote)) | ||
.route("/api/questions/{qids}", get(questions::questions)) | ||
.layer(RequestBodyLimitLayer::new(1024)) | ||
.with_state(backend) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
use super::{Backend, Local}; | ||
use crate::utils; | ||
use aws_sdk_dynamodb::{ | ||
error::SdkError, | ||
operation::query::{QueryError, QueryOutput}, | ||
|
@@ -59,7 +60,7 @@ impl Backend { | |
} = &mut *local; | ||
|
||
if !events.contains_key(eid) { | ||
return Err(super::mint_service_error( | ||
return Err(utils::mint_service_error( | ||
QueryError::ResourceNotFoundException( | ||
ResourceNotFoundException::builder().build(), | ||
), | ||
|
@@ -125,7 +126,7 @@ async fn list_inner( | |
) { | ||
let has_secret = if let Some(secret) = secret { | ||
debug!("list questions with admin access"); | ||
if let Err(e) = super::check_secret(&dynamo, &eid, &secret).await { | ||
if let Err(e) = utils::check_secret(&dynamo, &eid, &secret).await { | ||
// a bad secret will not turn good and | ||
// events are unlikely to re-appear with the same Ulid | ||
return ( | ||
|
@@ -138,7 +139,7 @@ async fn list_inner( | |
trace!("list questions with guest access"); | ||
// ensure that the event exists: | ||
// this is _just_ so give 404s for old events so clients stop polling | ||
if let Err(e) = super::get_secret(&dynamo, &eid).await { | ||
if let Err(e) = utils::get_secret(&dynamo, &eid).await { | ||
// events are unlikely to re-appear with the same Ulid | ||
return ( | ||
AppendHeaders([(header::CACHE_CONTROL, "max-age=86400")]), | ||
|
@@ -236,7 +237,7 @@ async fn list_inner( | |
let votes = q["votes"].as_u64().expect("votes is a number") as f64; | ||
// max so that even if vote count somehow got to 0, count it as 1 | ||
let votes = votes.max(1.); | ||
let exp = (-1. * dt as f64).exp_m1() + 1.; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clippy yelled at me |
||
let exp = (-1. * dt).exp_m1() + 1.; | ||
Score(exp * votes / (1. - exp)) | ||
}; | ||
questions.sort_by_cached_key(|q| std::cmp::Reverse(score(q))); | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lambda=trace
might be a bit ambiguous, butlambda
here is the app's name