Skip to content

Commit

Permalink
Fix CI build
Browse files Browse the repository at this point in the history
  • Loading branch information
markmandel committed Sep 14, 2021
1 parent c82a406 commit 4a0c8a4
Showing 1 changed file with 47 additions and 18 deletions.
65 changes: 47 additions & 18 deletions docs/src/filters/writing_custom_filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ To extend Quilkin's code with our own custom filter, we need to do the following

> The full source code used in this example can be found [here][example]

#### 1. Import the Quilkin crate

```bash
Expand All @@ -68,6 +67,12 @@ It's not terribly important what the filter in this example does so let's write
We start with the [Filter] implementation

```rust,no_run,noplayground
# #![allow(unused)]
# fn main() {
#
// src/main.rs
use quilkin::filters::prelude::*;
struct Greet;
impl Filter for Greet {
Expand All @@ -80,11 +85,21 @@ impl Filter for Greet {
Some(ctx.into())
}
}
# }
```

Next, we implement a [FilterFactory] for it and give it a name:

```rust,no_run,noplayground
# #![allow(unused)]
# fn main() {
#
# struct Greet;
# impl Filter for Greet {}
# use quilkin::filters::Filter;
// src/main.rs
use quilkin::filters::prelude::*;
pub const NAME: &str = "greet.v1";
pub fn factory() -> DynFilterFactory {
Expand All @@ -100,9 +115,10 @@ impl FilterFactory for GreetFilterFactory {
Ok(Box::new(Greet))
}
}
# }
```

> As a convention we implement a `pub factory() -> DynFilterFactory` function, to
> As a convention we implement a `pub factory() -> DynFilterFactory` function, to
> make the next integration step easier.
#### 3. Start the proxy
Expand All @@ -112,6 +128,7 @@ Let's add a main function that does that. Quilkin relies on the [Tokio] async ru
crate and wrap our main function with it.

Add Tokio as a dependency in `Cargo.toml`.

```toml
[dependencies]
quilkin = "0.2.0"
Expand All @@ -120,12 +137,13 @@ tokio = { version = "1", features = ["full"]}

Add a main function that starts the proxy.

```rust,no_run,noplayground
```rust,no_run,noplayground,ignore
// src/main.rs
{{#include ../../../examples/quilkin-filter-example/src/main.rs:run}}
```

Now, let's try out the proxy. The following configuration starts our extended version of the proxy at port 7001
and forwards all packets to an upstream server at port 4321.
Now, let's try out the proxy. The following configuration starts our extended version of the proxy at port 7001 and
forwards all packets to an upstream server at port 4321.

```yaml
# config.yaml
Expand All @@ -138,6 +156,7 @@ static:
endpoints:
- address: 127.0.0.1:4321
```
- Start the proxy
```bash
Expand Down Expand Up @@ -167,7 +186,7 @@ The [Serde] crate is used to describe static YAML configuration in code while [P

First let's create the config for our static configuration:

###### 1. Add the yaml parsing crates to Cargo.toml:
###### 1. Add the yaml parsing crates to `Cargo.toml`:

```toml
[dependencies]
Expand All @@ -177,13 +196,16 @@ First let's create the config for our static configuration:
```

###### 2. Define a struct representing the config:
```rust,no_run,noplayground

```rust,no_run,noplayground,ignore
// src/main.rs
{{#include ../../../examples/quilkin-filter-example/src/main.rs:serde_config}}
```

###### 3. Update the `Greet` Filter to take in `greeting` as a parameter:

```rust,no_run,noplayground
```rust,no_run,noplayground,ignore
// src/main.rs
{{#include ../../../examples/quilkin-filter-example/src/main.rs:filter}}
```

Expand All @@ -196,7 +218,7 @@ Our dynamic config model contains the serialized [Protobuf] message received fro
[ProstAny][prost-any] type so the [FilterFactory] can interpret its contents anyway it wishes to. However, it usually
contains a Protobuf equivalent of the filter's static configuration.

###### 1. Add the proto parsing crates to Cargo.toml:
###### 1. Add the proto parsing crates to `Cargo.toml`:

```toml
[dependencies]
Expand All @@ -206,17 +228,18 @@ prost = "0.7"
prost-types = "0.7"
```

###### 2. Create `greet.proto`, a [Protobuf] equivalent of the [static configuration][anchor-static-config]:
###### 2. Create a [Protobuf] equivalent of the [static configuration][anchor-static-config]:

```plaintext,no_run,noplayground
```plaintext,no_run,noplayground,ignore
// src/greet.proto
{{#include ../../../examples/quilkin-filter-example/src/greet.proto:proto}}
```

###### 3. Generate Rust code from the proto file:

There are a few ways to generate [Prost] code from proto, we will use the [prost_build] crate in this example.

Add the required crates to Cargo.toml:
Add the required crates to `Cargo.toml`:

```toml
[dependencies]
Expand All @@ -229,14 +252,16 @@ prost-build = "0.7"

Add a [build script][build-script] to generate the Rust code during compilation:

```rust,no_run,noplayground
```rust,no_run,noplayground,ignore
// src/build.rs
{{#include ../../../examples/quilkin-filter-example/build.rs:build}}
```

To include the generated code, we'll use a convenience macro [include_proto], which imports the
generated code, while recreating the grpc package name as Rust modules:
To include the generated code, we'll use a convenience macro [include_proto], which imports the generated code, while
recreating the grpc package name as Rust modules:

```rust,no_run,noplayground
```rust,no_run,noplayground,ignore
// src/main.rs
{{#include ../../../examples/quilkin-filter-example/src/main.rs:include_proto}}
```

Expand All @@ -246,21 +271,25 @@ Since configuration can be provided either statically or dynamically at runtime,
between one configuration model and the other. To do this, we'll implement the [std::convert::TryFrom] trait to
convert from the [Protobuf] model to the [Serde] model.

```rust,no_run,noplayground
```rust,no_run,noplayground,ignore
// src/main.rs
{{#include ../../../examples/quilkin-filter-example/src/main.rs:TryFrom}}
```

##### Finally, update `GreetFilterFactory` to extract the greeting from the passed in configuration and forward it onto the `Greet` Filter.

```rust,no_run,noplayground
```rust,no_run,noplayground,ignore
// src/main.rs
{{#include ../../../examples/quilkin-filter-example/src/main.rs:factory}}
```

> We use the convenience function of [ConfigType::deserialize] to return the [Serde] config regardless of which type
of configuration is utilised.

And with these changes we have wired up configuration for our filter. Try it out with the following `config.yaml`:

```yaml
# config.yaml
{{#include ../../../examples/quilkin-filter-example/config.yaml:yaml}}
```

Expand Down

0 comments on commit 4a0c8a4

Please sign in to comment.