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

feat: Add proper example of configuring Rust functions. #436

Merged
merged 1 commit into from Aug 1, 2021

Conversation

dejanb
Copy link
Contributor

@dejanb dejanb commented Jul 26, 2021

Changes

-:gift: As discussed in #430, here's the concrete example on how to use function configuration.

/kind feature

It uses a simple configuration struct to provide users with easy to adapt example on how to provide configuration to their functions.

@knative-prow-robot
Copy link

@dejanb: The label(s) kind/feature cannot be applied, because the repository doesn't have them.

In response to this:

Changes

-:gift: As discussed in #430, here's the concrete example on how to use function configuration.

/kind feature

It uses a simple configuration struct to provide users with easy to adapt example on how to provide configuration to their functions.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@google-cla google-cla bot added the cla: yes Indicates the PR's author has signed the CLA. label Jul 26, 2021
@knative-prow-robot knative-prow-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jul 26, 2021
@lance
Copy link
Member

lance commented Jul 26, 2021

/kind enhancement

@knative-prow-robot knative-prow-robot added kind/enhancement needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Jul 26, 2021
@knative-prow-robot knative-prow-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 27, 2021
Copy link
Member

@lance lance left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Though again, I'm not a rustafarian. Would love for @jcrossley3 to have a look.

Copy link
Contributor

@jcrossley3 jcrossley3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool idea using config to un-hardcode the "world" string. Just a few questions...

templates/rust/events/src/handler.rs Show resolved Hide resolved
@@ -29,7 +34,7 @@ mod tests {
async fn valid_input() {
let mut input = Event::default();
input.set_data("application/json", json!({"name": "bootsy"}));
let resp = handle(input).await;
let resp = handle(input, web::Data::new(HandlerConfig::default())).await;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really wish rust supported optional arguments. :)

Can we put this in a static constant local to the test module and call it config or something? Just so it won't look so gross?

Copy link
Contributor Author

@dejanb dejanb Jul 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up adding config() function to tests that wraps this and makes things a bit nicer to look at :). This is the simplest thing I could find as making static traits complicates them for the example and I would like to keep those as simple and reusable as possible. Let me know how it looks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better, thanks!

templates/rust/events/src/handler.rs Outdated Show resolved Hide resolved
Comment on lines +6 to +11
pub async fn index(req: HttpRequest, config: web::Data<HandlerConfig>) -> HttpResponse {
info!("{:#?}", req);
if req.method() == Method::GET {
HttpResponse::Ok().body("Hello!\n")
HttpResponse::Ok().body(format!("Hello {}!\n", config.name))
} else {
HttpResponse::Ok().body("Thanks!\n")
HttpResponse::Ok().body(format!("Thanks {}!\n", config.name))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kinda makes me want to add a name argument, similar to the events example, in which the config.name is only used if the name isn't passed.

Copy link
Contributor Author

@dejanb dejanb Jul 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm all for improving this example and looked into it. However, I didn't find an easy/simple way to add it. We'd need to use extractors for both query and body (depending whether get or post is used) and possibly add another struct. Maybe we should split the handler in two? We would also need to add more tests.
I would definitely like to evolve this, but maybe we should do it in a separate PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was thinking there was a FromRequest impl for String but that returns the whole body. I'm not sure introducing a Query param (or whatever) is worth it. Probably out of scope for an example. My bad!

templates/rust/http/src/handler.rs Outdated Show resolved Hide resolved
@dejanb dejanb force-pushed the rust-config-example branch 3 times, most recently from 6d6be3d to 5e617e7 Compare July 28, 2021 09:35
@jcrossley3
Copy link
Contributor

/lgtm

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Jul 28, 2021
@lance
Copy link
Member

lance commented Jul 28, 2021

Thanks for helping out with the review @jcrossley3

/lgtm

@lance
Copy link
Member

lance commented Jul 28, 2021

/approve

@lance
Copy link
Member

lance commented Jul 30, 2021

@dejanb pkged.go is such a pita. Could you please rebase this PR?

@knative-prow-robot knative-prow-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 30, 2021
@knative-prow-robot knative-prow-robot removed lgtm Indicates that a PR is ready to be merged. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Aug 1, 2021
@knative-prow-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dejanb, lance

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow-robot knative-prow-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 1, 2021
@dejanb
Copy link
Contributor Author

dejanb commented Aug 1, 2021

@dejanb pkged.go is such a pita. Could you please rebase this PR?

@lance sure, just did it. Tests look good now. Let me know if there's anything else needed.

@lkingland
Copy link
Member

/lgtm

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Aug 1, 2021
@knative-prow-robot knative-prow-robot merged commit 7656c40 into knative:main Aug 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cla: yes Indicates the PR's author has signed the CLA. kind/enhancement lgtm Indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants