-
Notifications
You must be signed in to change notification settings - Fork 365
Removed 'static bound requirements for the lambda_http crate and create a shared resources example for the crate #333
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,39 @@ | ||
use lambda_http::{ | ||
handler, | ||
lambda_runtime::{self, Context, Error}, | ||
IntoResponse, Request, RequestExt, Response, | ||
}; | ||
|
||
struct SharedClient { | ||
name: &'static str, | ||
} | ||
|
||
impl SharedClient { | ||
fn response(&self, req_id: String, first_name: &str) -> String { | ||
format!("{}: Client ({}) invoked by {}.", req_id, self.name, first_name) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
// Create the "client" and a reference to it, so that we can pass this into the handler closure below. | ||
let shared_client = SharedClient { | ||
name: "random_client_name_1", | ||
}; | ||
let shared_client_ref = &shared_client; | ||
|
||
// Define a closure here that makes use of the shared client. | ||
let handler_func_closure = move |event: Request, ctx: Context| async move { | ||
Ok(match event.query_string_parameters().get("first_name") { | ||
Some(first_name) => shared_client_ref.response(ctx.request_id, first_name).into_response(), | ||
_ => Response::builder() | ||
.status(400) | ||
.body("Empty first name".into()) | ||
.expect("failed to render response"), | ||
}) | ||
}; | ||
|
||
// Pass the closure to the runtime here. | ||
lambda_runtime::run(handler(handler_func_closure)).await?; | ||
Ok(()) | ||
} |
This file contains hidden or 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
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.
Can you give more context on what this is for? I'm not sure I understand.
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.
Sure, if you look down below it's because I added the explicit lifetime to the
Adapter
struct, because it holds aHandler
that requires a lifetime parameter. Because the'a
lifetime is unused inAdapter
, otherwise, the compiler complains about the unused lifetime parameter we supplied to it. The PhantomData is just a zero-sized compiler dummy value that holds the'a
lifetime and "uses" the lifetime to make the compiler happy.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.
Ah, okay. That makes sense now thank you.