summaryrefslogtreecommitdiff
path: root/modules/caddyhttp
diff options
context:
space:
mode:
Diffstat (limited to 'modules/caddyhttp')
-rw-r--r--modules/caddyhttp/caddyhttp.go55
-rwxr-xr-xmodules/caddyhttp/reverseproxy/module.go33
-rwxr-xr-xmodules/caddyhttp/reverseproxy/upstream.go4
-rw-r--r--modules/caddyhttp/routes.go8
4 files changed, 36 insertions, 64 deletions
diff --git a/modules/caddyhttp/caddyhttp.go b/modules/caddyhttp/caddyhttp.go
index dfb2ea0..0fe9c98 100644
--- a/modules/caddyhttp/caddyhttp.go
+++ b/modules/caddyhttp/caddyhttp.go
@@ -37,16 +37,20 @@ type App struct {
Servers map[string]*Server `json:"servers"`
servers []*http.Server
+
+ ctx caddy2.Context
}
// Provision sets up the app.
-func (hc *App) Provision() error {
- for _, srv := range hc.Servers {
- err := srv.Routes.Provision()
+func (app *App) Provision(ctx caddy2.Context) error {
+ app.ctx = ctx
+
+ for _, srv := range app.Servers {
+ err := srv.Routes.Provision(ctx)
if err != nil {
return fmt.Errorf("setting up server routes: %v", err)
}
- err = srv.Errors.Routes.Provision()
+ err = srv.Errors.Routes.Provision(ctx)
if err != nil {
return fmt.Errorf("setting up server error handling routes: %v", err)
}
@@ -56,10 +60,10 @@ func (hc *App) Provision() error {
}
// Validate ensures the app's configuration is valid.
-func (hc *App) Validate() error {
+func (app *App) Validate() error {
// each server must use distinct listener addresses
lnAddrs := make(map[string]string)
- for srvName, srv := range hc.Servers {
+ for srvName, srv := range app.Servers {
for _, addr := range srv.Listen {
netw, expanded, err := parseListenAddr(addr)
if err != nil {
@@ -78,13 +82,13 @@ func (hc *App) Validate() error {
}
// Start runs the app. It sets up automatic HTTPS if enabled.
-func (hc *App) Start(handle caddy2.Handle) error {
- err := hc.automaticHTTPS(handle)
+func (app *App) Start() error {
+ err := app.automaticHTTPS()
if err != nil {
return fmt.Errorf("enabling automatic HTTPS: %v", err)
}
- for srvName, srv := range hc.Servers {
+ for srvName, srv := range app.Servers {
s := &http.Server{
ReadTimeout: time.Duration(srv.ReadTimeout),
ReadHeaderTimeout: time.Duration(srv.ReadHeaderTimeout),
@@ -110,13 +114,13 @@ func (hc *App) Start(handle caddy2.Handle) error {
}
// enable TLS
- httpPort := hc.HTTPPort
+ httpPort := app.HTTPPort
if httpPort == 0 {
httpPort = DefaultHTTPPort
}
_, port, _ := net.SplitHostPort(addr)
if len(srv.TLSConnPolicies) > 0 && port != strconv.Itoa(httpPort) {
- tlsCfg, err := srv.TLSConnPolicies.TLSConfig(handle)
+ tlsCfg, err := srv.TLSConnPolicies.TLSConfig(app.ctx)
if err != nil {
return fmt.Errorf("%s/%s: making TLS configuration: %v", network, addr, err)
}
@@ -124,7 +128,7 @@ func (hc *App) Start(handle caddy2.Handle) error {
}
go s.Serve(ln)
- hc.servers = append(hc.servers, s)
+ app.servers = append(app.servers, s)
}
}
}
@@ -133,14 +137,14 @@ func (hc *App) Start(handle caddy2.Handle) error {
}
// Stop gracefully shuts down the HTTP server.
-func (hc *App) Stop() error {
+func (app *App) Stop() error {
ctx := context.Background()
- if hc.GracePeriod > 0 {
+ if app.GracePeriod > 0 {
var cancel context.CancelFunc
- ctx, cancel = context.WithTimeout(ctx, time.Duration(hc.GracePeriod))
+ ctx, cancel = context.WithTimeout(ctx, time.Duration(app.GracePeriod))
defer cancel()
}
- for _, s := range hc.servers {
+ for _, s := range app.servers {
err := s.Shutdown(ctx)
if err != nil {
return err
@@ -149,8 +153,8 @@ func (hc *App) Stop() error {
return nil
}
-func (hc *App) automaticHTTPS(handle caddy2.Handle) error {
- tlsAppIface, err := handle.App("tls")
+func (app *App) automaticHTTPS() error {
+ tlsAppIface, err := app.ctx.App("tls")
if err != nil {
return fmt.Errorf("getting tls app: %v", err)
}
@@ -159,7 +163,7 @@ func (hc *App) automaticHTTPS(handle caddy2.Handle) error {
lnAddrMap := make(map[string]struct{})
var redirRoutes RouteList
- for srvName, srv := range hc.Servers {
+ for srvName, srv := range app.Servers {
srv.tlsApp = tlsApp
if srv.DisableAutoHTTPS {
@@ -209,7 +213,7 @@ func (hc *App) automaticHTTPS(handle caddy2.Handle) error {
if err != nil {
return fmt.Errorf("%s: invalid listener address: %v", srvName, addr)
}
- httpRedirLnAddr := joinListenAddr(netw, host, strconv.Itoa(hc.HTTPPort))
+ httpRedirLnAddr := joinListenAddr(netw, host, strconv.Itoa(app.HTTPPort))
lnAddrMap[httpRedirLnAddr] = struct{}{}
if parts := strings.SplitN(port, "-", 2); len(parts) == 2 {
@@ -217,7 +221,7 @@ func (hc *App) automaticHTTPS(handle caddy2.Handle) error {
}
redirTo := "https://{request.host}"
- httpsPort := hc.HTTPSPort
+ httpsPort := app.HTTPSPort
if httpsPort == 0 {
httpsPort = DefaultHTTPSPort
}
@@ -253,13 +257,13 @@ func (hc *App) automaticHTTPS(handle caddy2.Handle) error {
continue
}
for _, a := range addrs {
- if hc.listenerTaken(netw, a) {
+ if app.listenerTaken(netw, a) {
continue mapLoop
}
}
lnAddrs = append(lnAddrs, addr)
}
- hc.Servers["auto_https_redirects"] = &Server{
+ app.Servers["auto_https_redirects"] = &Server{
Listen: lnAddrs,
Routes: redirRoutes,
DisableAutoHTTPS: true,
@@ -269,8 +273,8 @@ func (hc *App) automaticHTTPS(handle caddy2.Handle) error {
return nil
}
-func (hc *App) listenerTaken(network, address string) bool {
- for _, srv := range hc.Servers {
+func (app *App) listenerTaken(network, address string) bool {
+ for _, srv := range app.Servers {
for _, addr := range srv.Listen {
netw, addrs, err := parseListenAddr(addr)
if err != nil || netw != network {
@@ -491,3 +495,4 @@ const (
// Interface guards
var _ HTTPInterfaces = middlewareResponseWriter{}
+var _ caddy2.App = (*App)(nil)
diff --git a/modules/caddyhttp/reverseproxy/module.go b/modules/caddyhttp/reverseproxy/module.go
index cc53bf5..1bfc9ad 100755
--- a/modules/caddyhttp/reverseproxy/module.go
+++ b/modules/caddyhttp/reverseproxy/module.go
@@ -1,8 +1,6 @@
package reverseproxy
import (
- "fmt"
-
"bitbucket.org/lightcodelabs/caddy2"
)
@@ -11,36 +9,5 @@ func init() {
caddy2.RegisterModule(caddy2.Module{
Name: "http.responders.reverse_proxy",
New: func() (interface{}, error) { return new(LoadBalanced), nil },
- OnLoad: func(instances []interface{}, _ interface{}) (interface{}, error) {
- // we don't need to do anything with prior state because healthcheckers are
- // cleaned up in OnUnload.
- s := &State{
- HealthCheckers: []*HealthChecker{},
- }
-
- for _, i := range instances {
- lb := i.(*LoadBalanced)
-
- err := NewLoadBalancedReverseProxy(lb, s)
- if err != nil {
- return nil, err
- }
- }
-
- return s, nil
- },
- OnUnload: func(state interface{}) error {
- s, ok := state.(*State)
- if !ok {
- return fmt.Errorf("proxy OnLoad: prior state not expected proxy.State type")
- }
-
- // cleanup old healthcheckers
- for _, hc := range s.HealthCheckers {
- hc.Stop()
- }
-
- return nil
- },
})
}
diff --git a/modules/caddyhttp/reverseproxy/upstream.go b/modules/caddyhttp/reverseproxy/upstream.go
index b521d46..7e429f9 100755
--- a/modules/caddyhttp/reverseproxy/upstream.go
+++ b/modules/caddyhttp/reverseproxy/upstream.go
@@ -76,7 +76,7 @@ var (
)
// NewLoadBalancedReverseProxy returns a collection of Upstreams that are to be loadbalanced.
-func NewLoadBalancedReverseProxy(lb *LoadBalanced, state *State) error {
+func NewLoadBalancedReverseProxy(lb *LoadBalanced, state *State, ctx caddy2.Context) error {
// set defaults
if lb.NoHealthyUpstreamsMessage == "" {
lb.NoHealthyUpstreamsMessage = msgNoHealthyUpstreams
@@ -115,7 +115,7 @@ func NewLoadBalancedReverseProxy(lb *LoadBalanced, state *State) error {
if uc.CircuitBreaker != nil {
if _, err := caddy2.GetModule(cbModule); err == nil {
- val, err := caddy2.LoadModule(cbModule, uc.CircuitBreaker)
+ val, err := ctx.LoadModule(cbModule, uc.CircuitBreaker)
if err == nil {
cbv, ok := val.(CircuitBreaker)
if ok {
diff --git a/modules/caddyhttp/routes.go b/modules/caddyhttp/routes.go
index cd612ac..d204939 100644
--- a/modules/caddyhttp/routes.go
+++ b/modules/caddyhttp/routes.go
@@ -29,11 +29,11 @@ type ServerRoute struct {
type RouteList []ServerRoute
// Provision sets up all the routes by loading the modules.
-func (routes RouteList) Provision() error {
+func (routes RouteList) Provision(ctx caddy2.Context) error {
for i, route := range routes {
// matchers
for modName, rawMsg := range route.Matchers {
- val, err := caddy2.LoadModule("http.matchers."+modName, rawMsg)
+ val, err := ctx.LoadModule("http.matchers."+modName, rawMsg)
if err != nil {
return fmt.Errorf("loading matcher module '%s': %v", modName, err)
}
@@ -43,7 +43,7 @@ func (routes RouteList) Provision() error {
// middleware
for j, rawMsg := range route.Apply {
- mid, err := caddy2.LoadModuleInline("middleware", "http.middleware", rawMsg)
+ mid, err := ctx.LoadModuleInline("middleware", "http.middleware", rawMsg)
if err != nil {
return fmt.Errorf("loading middleware module in position %d: %v", j, err)
}
@@ -53,7 +53,7 @@ func (routes RouteList) Provision() error {
// responder
if route.Respond != nil {
- resp, err := caddy2.LoadModuleInline("responder", "http.responders", route.Respond)
+ resp, err := ctx.LoadModuleInline("responder", "http.responders", route.Respond)
if err != nil {
return fmt.Errorf("loading responder module: %v", err)
}