You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+143-1Lines changed: 143 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -139,7 +139,149 @@ Keep reading to learn how to customize your Hooks server with different plugins
139
139
140
140
### Advanced
141
141
142
-
TODO
142
+
This section will go into a more advanced and detailed example of how to setup a Hooks server with custom plugins, authentication, and more. This section also assumes you already have the `hooks-ruby` gem installed via a bundler Gemfile as shown in the [Installation](#installation-) section above.
143
+
144
+
Before we get into the details, here is a high-level overview of the steps involved:
145
+
146
+
1. **Define your configuration**: Create a `hooks.yml` file to define your global configuration, including the directories for your plugins and endpoints.
147
+
2. **Create your endpoint configurations**: Define your webhook endpoints in the `config/endpoints` directory, specifying the path and the handler plugin to use.
148
+
3. **Implement your handler plugins**: Create custom handler plugins in the `plugins/handlers` directory to process incoming webhook requests.
149
+
4. **Implement authentication plugins (optional)**: If you want to secure your webhook endpoints, create custom authentication plugins in the `plugins/auth` directory. Note that you might just be able to get away with using the built-in authentication plugins ([`hmac`](lib/hooks/plugins/auth/hmac.rb), or [`shared_secret`](lib/hooks/plugins/auth/shared_secret.rb)) for most use cases.
150
+
151
+
This example will assume the following directory structure:
First, create a `hooks.yml` file in the `config` directory. This file will define your global configuration for the Hooks server, including the directories for your plugins and endpoints. Here is an example of a minimal configuration file:
173
+
174
+
```yaml
175
+
# file: config/hooks.yml
176
+
handler_plugin_dir: ./plugins/handlers
177
+
auth_plugin_dir: ./plugins/auth
178
+
179
+
# Available endpoints
180
+
# Each endpoint configuration file should be placed in the endpoints directory
181
+
endpoints_dir: ./config/endpoints
182
+
183
+
log_level: debug
184
+
185
+
# Path configuration
186
+
root_path: /webhooks # Base path for all webhook endpoints (e.g. /webhooks/hello)
187
+
health_path: /health
188
+
version_path: /version
189
+
190
+
# Runtime behavior
191
+
environment: development # or production
192
+
```
193
+
194
+
#### 2. Create your endpoint configurations
195
+
196
+
Endpoint configurations are defined in the `config/endpoints` directory. Each endpoint configuration file should specify the path for the webhook endpoint and the handler plugin to use. Here is an example of two endpoint configuration files:
197
+
198
+
> Note: You can also define auth plugins for each endpoint if you want to secure them. For this example, the `/hello` endpoint will not have authentication, while the `/goodbye` endpoint will use a custom authentication plugin.
199
+
200
+
```yaml
201
+
# file: config/endpoints/hello.yml
202
+
path: /hello # becomes /webhooks/hello based on the root_path in hooks.yml
203
+
handler: HelloHandler # This is a custom handler plugin you would define in the plugins/handlers
204
+
```
205
+
206
+
```yaml
207
+
# file: config/endpoints/goodbye.yml
208
+
path: /goodbye # becomes /webhooks/goodbye based on the root_path in hooks.yml
209
+
handler: GoodbyeHandler # This is another custom handler plugin you would define in the plugins/handlers
210
+
211
+
auth:
212
+
type: Goodbye # This is a custom authentication plugin you would define in the plugins/auth
213
+
secret_env_key: GOODBYE_API_KEY # the name of the environment variable containing the secret
214
+
header: Authorization
215
+
```
216
+
217
+
#### 3. Implement your handler plugins
218
+
219
+
Create custom handler plugins in the `plugins/handlers` directory to process incoming webhook requests. Here is an example of a simple handler plugin for the `/hello` endpoint:
220
+
221
+
```ruby
222
+
# file: plugins/handlers/hello_handler.rb
223
+
class HelloHandler < Hooks::Plugins::Handlers::Base
224
+
def call(payload:, headers:, config:)
225
+
# Process the incoming webhook - optionally use the payload and headers
226
+
# to perform some action or validation
227
+
# For this example, we will just return a success message
228
+
{
229
+
message: "webhook processed successfully",
230
+
handler: "HelloHandler",
231
+
timestamp: Time.now.iso8601
232
+
}
233
+
end
234
+
end
235
+
```
236
+
237
+
And another handler plugin for the `/goodbye` endpoint:
238
+
239
+
```ruby
240
+
# file: plugins/handlers/goodbye_handler.rb
241
+
class GoodbyeHandler < Hooks::Plugins::Handlers::Base
If you want to secure your webhook endpoints, you can create custom authentication plugins in the `plugins/auth` directory. Here is an example of a simple authentication plugin for the `/goodbye` endpoint:
256
+
257
+
```ruby
258
+
# file: plugins/auth/goodbye.rb
259
+
# this is a custom authentication plugin for the Goodbye endpoint
260
+
# it is extremely simple and just checks if the Authorization header matches a secret for example purposes
261
+
module Hooks
262
+
module Plugins
263
+
module Auth
264
+
class Goodbye < Base
265
+
def self.valid?(payload:, headers:, config:)
266
+
# get the secret from environment variable as configured with secret_env_key
What these steps have done is set up a Hooks server that listens for incoming webhook requests at `/webhooks/hello` and `/webhooks/goodbye`. The `/hello` endpoint will respond with a success message without any authentication, while the `/goodbye` endpoint will require a valid `Authorization` header that matches the secret defined in the environment variable `GOODBYE_API_KEY`. Before the `/goodbye` endpoint enters the defined handler, it will first check the authentication plugin to ensure the request is valid.
0 commit comments