2016-06-05 22:51:56 -05:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mholt/caddy"
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-06-06 16:31:03 -05:00
|
|
|
caddy.RegisterPlugin("proxy", caddy.Plugin{
|
2016-06-05 22:51:56 -05:00
|
|
|
ServerType: "http",
|
|
|
|
Action: setup,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup configures a new Proxy middleware instance.
|
|
|
|
func setup(c *caddy.Controller) error {
|
2017-04-17 10:58:47 -05:00
|
|
|
upstreams, err := NewStaticUpstreams(c.Dispenser, httpserver.GetConfig(c).Host())
|
2016-06-05 22:51:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-20 12:44:20 -05:00
|
|
|
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
|
2016-06-05 22:51:56 -05:00
|
|
|
return Proxy{Next: next, Upstreams: upstreams}
|
|
|
|
})
|
2017-04-02 15:58:15 -05:00
|
|
|
|
|
|
|
// Register shutdown handlers.
|
|
|
|
for _, upstream := range upstreams {
|
|
|
|
c.OnShutdown(upstream.Stop)
|
|
|
|
}
|
|
|
|
|
2016-06-05 22:51:56 -05:00
|
|
|
return nil
|
|
|
|
}
|