Skip to content

Commit

Permalink
Add some examples to http_server input
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffail authored and lucasoares committed Mar 8, 2024
1 parent b89b33c commit 35eee03
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
54 changes: 53 additions & 1 deletion internal/impl/io/input_http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,59 @@ You can access these metadata fields using [function interpolation](/docs/config
).
Description("Customise messages returned via [synchronous responses](/docs/guides/sync_responses).").
Advanced(),
)
).
Example(
"Path Switching",
"This example shows an `http_server` input that captures all requests and processes them by switching on that path:", `
input:
http_server:
path: /
allowed_verbs: [ GET, POST ]
sync_response:
headers:
Content-Type: application/json
processors:
- switch:
- check: '@http_server_request_path == "/foo"'
processors:
- mapping: |
root.title = "You Got Fooed!"
root.result = content().string().uppercase()
- check: '@http_server_request_path == "/bar"'
processors:
- mapping: 'root.title = "Bar Is Slow"'
- sleep: # Simulate a slow endpoint
duration: 1s
`).
Example(
"Mock OAuth 2.0 Server",
"This example shows an `http_server` input that mocks an OAuth 2.0 Client Credentials flow server at the endpoint `/oauth2_test`:", `
input:
http_server:
path: /oauth2_test
allowed_verbs: [ GET, POST ]
sync_response:
headers:
Content-Type: application/json
processors:
- log:
message: "Received request"
level: INFO
fields_mapping: |
root = @
root.body = content().string()
- mapping: |
root.access_token = "MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI3"
root.token_type = "Bearer"
root.expires_in = 3600
- sync_response: {}
- mapping: 'root = deleted()'
`)
}

func init() {
Expand Down
69 changes: 69 additions & 0 deletions website/docs/components/inputs/http_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,75 @@ If HTTPS is enabled, the following fields are added as well:

You can access these metadata fields using [function interpolation](/docs/configuration/interpolation#bloblang-queries).

## Examples

<Tabs defaultValue="Path Switching" values={[
{ label: 'Path Switching', value: 'Path Switching', },
{ label: 'Mock OAuth 2.0 Server', value: 'Mock OAuth 2.0 Server', },
]}>

<TabItem value="Path Switching">

This example shows an `http_server` input that captures all requests and processes them by switching on that path:

```yaml
input:
http_server:
path: /
allowed_verbs: [ GET, POST ]
sync_response:
headers:
Content-Type: application/json
processors:
- switch:
- check: '@http_server_request_path == "/foo"'
processors:
- mapping: |
root.title = "You Got Fooed!"
root.result = content().string().uppercase()
- check: '@http_server_request_path == "/bar"'
processors:
- mapping: 'root.title = "Bar Is Slow"'
- sleep: # Simulate a slow endpoint
duration: 1s
```

</TabItem>
<TabItem value="Mock OAuth 2.0 Server">

This example shows an `http_server` input that mocks an OAuth 2.0 Client Credentials flow server at the endpoint `/oauth2_test`:

```yaml
input:
http_server:
path: /oauth2_test
allowed_verbs: [ GET, POST ]
sync_response:
headers:
Content-Type: application/json
processors:
- log:
message: "Received request"
level: INFO
fields_mapping: |
root = @
root.body = content().string()
- mapping: |
root.access_token = "MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI3"
root.token_type = "Bearer"
root.expires_in = 3600
- sync_response: {}
- mapping: 'root = deleted()'
```

</TabItem>
</Tabs>

## Fields

### `address`
Expand Down

0 comments on commit 35eee03

Please sign in to comment.