Skip to content

Commit

Permalink
feat: Configure Rust functions (#430)
Browse files Browse the repository at this point in the history
* Add ability to add all custom function configuration in a separate module.

Co-authored-by: Dejan Bosanac <dejan@sensatic.net>
Co-authored-by: Jim Crossley <jcrossley3@gmail.com>

Co-authored-by: Jim Crossley <jcrossley3@gmail.com>
  • Loading branch information
dejanb and Jim Crossley committed Jul 23, 2021
1 parent ede284b commit a08b843
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 5 deletions.
5 changes: 3 additions & 2 deletions docs/guides/rust.md
Expand Up @@ -17,20 +17,21 @@ fn
├── func.yaml
├── README.md
└── src
├── config.rs
├── handler.rs
└── main.rs
```

This is a full-featured, self-contained Rust application that uses the
[actix](https://actix.rs/) web framework to listen for HTTP requests on port 8080.
[actix](https://actix.rs/) web framework to listen for HTTP requests on port 8080.

See the generated [README.md](../../templates/rust/http/README.md) for
details on building, testing, and deploying the app.

You may have noticed the `func.yaml` file. This is a configuration
file used by `func` to deploy your project as a service in your
kubernetes cluster.
kubernetes cluster.

For an event-triggered function, pass the `-t events` option to
generate an app capable of responding to
Expand Down
2 changes: 1 addition & 1 deletion pkged.go

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion templates/rust/events/README.md
Expand Up @@ -5,7 +5,8 @@ Welcome to your new Rust function project! The boilerplate
[`src/main.rs`](./src/main.rs). It's configured to invoke the `handle`
function in [`src/handler.rs`](./src/handler.rs) in response to a POST
request containing a valid `CloudEvent`. You should put your desired
behavior inside that `handle` function.
behavior inside that `handle` function. In case you need to configure
some resources for your function, you can do that in the [`configure` function](./src/config.rs).

The app will expose three endpoints:

Expand Down
26 changes: 26 additions & 0 deletions templates/rust/events/src/config.rs
@@ -0,0 +1,26 @@
use actix_web::web;

/// Run custom configuration as part of the application building
/// process.
///
/// This function should contain all custom configuration for your function application.
///
/// ```rust
/// fn configure(cfg: &mut web::ServiceConfig) {
/// let db_driver = my_db();
/// cfg.data(db_driver.clone());
/// }
/// ```
///
/// Then you can use configured resources in your function.
///
/// ```rust
/// pub async fn handle(
/// event: Event,
/// driver: web::Data<DbDriver>,
/// ) -> Result<Event, actix_web::Error> {
/// Ok(Event::default())
/// }
pub fn configure(_cfg: &mut web::ServiceConfig) {
log::info!("Configuring service");
}
2 changes: 2 additions & 0 deletions templates/rust/events/src/main.rs
@@ -1,6 +1,7 @@
use actix_web::{web, App, HttpResponse, HttpServer};
use env_logger as elog;

mod config;
mod handler;

#[actix_web::main]
Expand All @@ -16,6 +17,7 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(actix_web::middleware::Logger::default())
.configure(config::configure)
.route("/", web::post().to(handler::handle))
.route(
"/health/{_:(readiness|liveness)}",
Expand Down
3 changes: 2 additions & 1 deletion templates/rust/http/README.md
Expand Up @@ -5,7 +5,8 @@ Welcome to your new Rust function project! The boilerplate
[`src/main.rs`](./src/main.rs). It's configured to invoke the `index`
function in [`src/handler.rs`](./src/handler.rs) in response to both
GET and POST requests. You should put your desired behavior inside
that `index` function.
that `index` function. In case you need to configure
some resources for your function, you can do that in the [`configure` function](./src/config.rs).

The app will expose three endpoints:

Expand Down
26 changes: 26 additions & 0 deletions templates/rust/http/src/config.rs
@@ -0,0 +1,26 @@
use actix_web::web;

/// Run custom configuration as part of the application building
/// process.
///
/// This function should contain all custom configuration for your function application.
///
/// ```rust
/// fn configure(cfg: &mut web::ServiceConfig) {
/// let db_driver = my_db();
/// cfg.data(db_driver.clone());
/// }
/// ```
///
/// Then you can use configured resources in your function.
///
/// ```rust
/// pub async fn index(
/// req: HttpRequest,
/// driver: web::Data<DbDriver>,
/// ) -> HttpResponse {
/// HttpResponse::NoContent()
/// }
pub fn configure(_cfg: &mut web::ServiceConfig) {
log::info!("Configuring service");
}
2 changes: 2 additions & 0 deletions templates/rust/http/src/main.rs
@@ -1,6 +1,7 @@
use actix_web::{web, App, HttpResponse, HttpServer};
use env_logger as elog;

mod config;
mod handler;

#[actix_web::main]
Expand All @@ -16,6 +17,7 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(actix_web::middleware::Logger::default())
.configure(config::configure)
.route("/", web::get().to(handler::index))
.route("/", web::post().to(handler::index))
.route(
Expand Down

0 comments on commit a08b843

Please sign in to comment.