Updated Writing a Plugin: Server Type (markdown)

Matt Holt 2016-06-21 22:32:38 -06:00
parent 663be3c359
commit 830a0044be

@ -51,6 +51,10 @@ Caddy tries to avoid as much global state as possible. When Caddy loads, parses,
Each time Caddy goes through this startup phase, it will create a new caddy.Instance and Caddy will ask your server type for a new [caddy.Context](https://godoc.org/github.com/mholt/caddy#Context) value so that it can execute your server's directives without sharing global state. Read the godoc for caddy.Context for more information. Each time Caddy goes through this startup phase, it will create a new caddy.Instance and Caddy will ask your server type for a new [caddy.Context](https://godoc.org/github.com/mholt/caddy#Context) value so that it can execute your server's directives without sharing global state. Read the godoc for caddy.Context for more information.
Ultimately, your goal is to make it so that `MakeServers()` returns a list of servers that Caddy can use. How you do that is entirely up to you. The HTTP server keeps a map of configs in its context and has an exported function, `GetConfig(c Controller)` that gets a config for a certain site, designated by the [caddy.Controller](https://godoc.org/github.com/mholt/caddy#Controller) that is passed in. The config, in turn, store the list of middleware, etc -- all the information needed to set up a site. Each directive's action calls that function because each directive is passed a Controller. Then all `MakeServers()` has to do is combine those site configs into server instances and return them. (Server instances are types on which you can call `Listen()` and `Serve()`.) You should not store a a list of contexts with your server type; they will be stored with the Instance and are [accessible from Controllers](https://godoc.org/github.com/mholt/caddy#Controller.Context). Keeping references to contexts lying around will prevent them from being garbage collected if the server is stopped later.
(TODO: Expand this guide with more information...) Ultimately, your goal is to make it so that `MakeServers()` returns a list of servers that Caddy can use. How you do that is entirely up to you.
For example: the HTTP server keeps a map of site configs in its context and has an exported function, `GetConfig(c Controller)` that gets a config for a certain site, designated by the [caddy.Controller](https://godoc.org/github.com/mholt/caddy#Controller) that is passed in. The config, in turn, stores the list of middleware, etc -- all the information needed to set up a site. Each directive's action calls the GetConfig function so they can help set up the site properly. By the time `MakeServers()` is called, all it has to do is combine those site configs into server instances and return them. (Server instances are types on which you can call `Listen()` and `Serve()`.)
(TODO: We will expand this guide with more information...)