mirror of
https://github.com/caddyserver/caddy.git
synced 2025-07-09 03:04:57 -04:00
Updated Writing Middleware (markdown)
parent
58f837e941
commit
2c51142b4f
@ -1,6 +1,6 @@
|
||||
This page describes how to write middleware for Caddy, which adds to its capabilities when serving requests. You should know Go and be familiar with its [net/http](https://golang.org/pkg/net/http) package. It may help to have written at least a simple web app before or have a basic understanding of how middleware works in general.
|
||||
|
||||
Caddy's middleware packages are independent and can be used in any Go program. Keep that in mind when writing middleware: it's not just for Caddy.
|
||||
Caddy's middleware packages are independent and can be used in any Go program. Keep that in mind when writing middleware: it doesn't have to be just for Caddy.
|
||||
|
||||
## The `middleware` Package
|
||||
|
||||
@ -25,14 +25,14 @@ func (h *MyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, erro
|
||||
return h.Next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// These allow the middleware stack to be traversed like a singly-linked list
|
||||
// These allow the middleware stack to be traversed like a singly-linked list (the API uses this)
|
||||
func (h *MyHandler) GetNext() middleware.Handler { return h.Next }
|
||||
func (h *MyHandler) SetNext(next middleware.Handler) { h.Next = next }
|
||||
```
|
||||
|
||||
That's all there is to it. Put your middleware logic in the ServeHTTP method, and add any properties that your middleware may need to access to the struct type you defined your middleware to be.
|
||||
|
||||
Be careful to use pointer receivers.
|
||||
Be sure to use pointer receivers.
|
||||
|
||||
## Return Values and Writing Responses
|
||||
|
||||
@ -50,11 +50,11 @@ Also note that the status code return value is for the client's benefit, and the
|
||||
|
||||
Now that your general-purpose middleware handler is working, it's time to integrate it with Caddy. Caddy parses middleware configuration out of a configuration file, so Caddy provides a parse facility for you to easily set up your middleware based on what the user typed in the text file.
|
||||
|
||||
In the `setup` package, create a file to set up your middleware. Caddy will use this when the server starts to link it and get everything running. You'll create a setup function after the same name as the middleware. You can use this as a template:
|
||||
In your package, create a `Setup` function which will be used to set up your middleware. It must parse the Caddyfile and fill in values as needed. Caddy will use this when the server starts to chain it in, start any necessary services, and get everything running. For example:
|
||||
|
||||
```go
|
||||
// MyMiddleware configures a new MyMiddleware middleware instance.
|
||||
func MyMiddleware(c *Controller) (middleware.Middleware, error) {
|
||||
// Setup configures a new MyMiddleware middleware instance.
|
||||
func Setup(c *Controller) (middleware.Middleware, error) {
|
||||
for c.Next() {
|
||||
// do parsing
|
||||
}
|
||||
@ -65,8 +65,8 @@ func MyMiddleware(c *Controller) (middleware.Middleware, error) {
|
||||
// This function will be called every time the server
|
||||
// is started. Create and return your middleware
|
||||
// struct here to ensure it gets the latest config.
|
||||
// Return a POINTER - very important!
|
||||
return &mymiddleware.MyType{
|
||||
// Return a pointer - very important!
|
||||
return &MyType{
|
||||
Next: next,
|
||||
// ...
|
||||
}
|
||||
@ -85,6 +85,4 @@ rewrite /bar /baz
|
||||
|
||||
creates just 1 struct but that struct holds 2 rules, for example, in a field like this: `Rules []Rule`.
|
||||
|
||||
Once that's all set up, register your middleware in [config/directives.go](https://github.com/mholt/caddy/blob/master/config/directives.go). The ordering is very important, so be sure to get it right!
|
||||
|
||||
Once it's registered, you can run Caddy with your new directive in a Caddyfile and try it out.
|
||||
Once that's all set up, you'll need to register your middleware so Caddy knows about it. You can look in the [config/directives.go](https://github.com/mholt/caddy/blob/master/config/directives.go) file to see the list of standard directives and think about where in the chain your middleware goes. The ordering is very important, so be sure to get it right! Follow the remaining steps in [Extending Caddy](https://github.com/mholt/caddy/wiki/Extending-Caddy) to learn how to test your middleware and make it available to others.
|
Loading…
x
Reference in New Issue
Block a user