Updated instructions/helps

Matt Holt 2015-05-05 10:28:00 -06:00
parent 3b696f13c7
commit 7374ad9a49

@ -1,14 +1,14 @@
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. 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. (Learn how.) 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's not just for Caddy.
## The `middleware` Package ## The `middleware` Package
Caddy has defined [a few important types](http://godoc.org/github.com/mholt/caddy/middleware) in its middleware package. The two that you will need to implement are middleware.Handler and middleware.Middleware. (The other types are for use by Caddy and are slated to be moved out of the package soon.) Take a quick look at their documentation; it will help. Caddy has defined [a few important types](http://godoc.org/github.com/mholt/caddy/middleware) in its middleware package. The two that you will need to implement are middleware.Handler and middleware.Middleware. Take a quick look at their documentation; it will help.
## Basic Handler ## Basic Handler
middleware.Handler is an interface exactly like http.Handler, except that the ServeHTTP method returns (int, error). The int is the HTTP status code to respond with, and the error is an error that should be handled and/or logged. Read the godoc for more details about these return values. This method signature follows [a recommendation from the Go Blog](http://blog.golang.org/error-handling-and-go) for error handling related to middleware. middleware.Handler is an interface exactly like http.Handler, except that the ServeHTTP method returns `(int, error)`. The int is the HTTP status code to respond with, and the error is an error that should be handled and/or logged. Read the godoc for more details about these return values. This method signature follows [a recommendation from the Go Blog](http://blog.golang.org/error-handling-and-go) for error handling related to middleware.
Handlers are usually a struct with at least one field, the next Handler in the chain: Handlers are usually a struct with at least one field, the next Handler in the chain:
@ -42,4 +42,30 @@ Also note that the status code return value is for the client's benefit, and the
## Integrating with Caddy ## Integrating with Caddy
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. 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:
```go
// MyMiddleware configures a new MyMiddleware middleware instance.
func MyMiddleware(c *Controller) (middleware.Middleware, error) {
for c.Next() {
// do parsing
}
// set up your middleware type
myType := mymiddleware.MyType{/* ... */}
return func(next middleware.Handler) middleware.Handler {
// chain in the next handler and return it
myType.Next = next
return myType
}, nil
}
```
The Controller gives you access to the server.Config and some really helpful parsing utilities to easily get values out of the Caddyfile.
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.