Skip to content

Commit 9e324c7

Browse files
committed
docs: add framework wrapper example
1 parent 5445f26 commit 9e324c7

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,42 @@ e.Use(echohelmet.ContentSecurityPolicy(
155155
- **Easy Migration**: Switch between frameworks without changing security logic
156156
- **Type Safety**: Full Go type safety and IDE support
157157

158+
## Writing Your Own Framework-Specific Helmet Adapter
159+
160+
```go
161+
// MyHeaderWriter implements core.HeaderWriter for My framework contexts
162+
type MyHeaderWriter struct {
163+
ctx myframework.Context
164+
}
165+
166+
// SetHeader sets a header in the response - adopt this to your framework's context
167+
func (m *MyHeaderWriter) SetHeader(key, value string) {
168+
m.ctx.Response().Header().Set(key, value)
169+
}
170+
171+
// Next is called when the middleware is done - adopt this to your framework's context
172+
func (m *MyHeaderWriter) Next() {
173+
m.ctx.Next()
174+
}
175+
176+
// wrapMiddleware converts a core.MiddlewareFunc to myframework.MiddlewareFunc
177+
func wrapMiddleware(middleware core.MiddlewareFunc) myframework.MiddlewareFunc {
178+
return func(next myframework.HandlerFunc) myframework.HandlerFunc {
179+
return func(c myframework.Context) error {
180+
writer := &MyHeaderWriter{ctx: c}
181+
middleware(writer)
182+
return next(c)
183+
}
184+
}
185+
}
186+
187+
// You can copy/paste all of the functions from any of the existing framework-specific packages, e.g.: [echohelmet/helmet.go](echohelmet/helmet.go#L32)
188+
// NoRobotIndex applies header to protect your server from robot indexation
189+
func NoRobotIndex() BeegoMiddleware {
190+
return wrapMiddleware(core.NoRobotIndex())
191+
}
192+
```
193+
158194
## Contributing
159195

160196
Contributions are welcome! Please feel free to submit a Pull Request.

0 commit comments

Comments
 (0)