diff options
author | dev <navdgo@gmail.com> | 2019-05-14 10:35:41 -0400 |
---|---|---|
committer | dev <navdgo@gmail.com> | 2019-05-20 14:48:26 -0400 |
commit | 043eb1d9e5db456b9b78c0423cb44716fc81a932 (patch) | |
tree | a78aee0752b03c4f3c3cb45d409107e64faa10e3 /modules/caddyhttp | |
parent | fec7fa8bfda713e8042b9bbf9a480c7792b78c41 (diff) |
move internal packages to pkg folder and update reverse proxy
* set automatic https error type for cert-magic failures
* add state to onload and unload methods
* update reverse proxy to use Provision() and Cleanup()
Diffstat (limited to 'modules/caddyhttp')
-rw-r--r-- | modules/caddyhttp/caddyhttp.go | 13 | ||||
-rw-r--r-- | modules/caddyhttp/matchers.go | 2 | ||||
-rwxr-xr-x | modules/caddyhttp/reverseproxy/upstream.go | 29 |
3 files changed, 34 insertions, 10 deletions
diff --git a/modules/caddyhttp/caddyhttp.go b/modules/caddyhttp/caddyhttp.go index 449d07f..379c8f2 100644 --- a/modules/caddyhttp/caddyhttp.go +++ b/modules/caddyhttp/caddyhttp.go @@ -24,6 +24,7 @@ func init() { Name: "http", New: func() (interface{}, error) { return new(App), nil }, }) + if err != nil { log.Fatal(err) } @@ -94,11 +95,21 @@ func (app *App) Validate() error { return nil } +// AutomaticHTTPSError represents an error received when attempting to enable automatic https. +type AutomaticHTTPSError struct { + internal error +} + +// Error returns the error string for automaticHTTPSError. +func (a AutomaticHTTPSError) Error() string { + return fmt.Sprintf("enabling automatic HTTPS: %v", a.internal.Error()) +} + // Start runs the app. It sets up automatic HTTPS if enabled. func (app *App) Start() error { err := app.automaticHTTPS() if err != nil { - return fmt.Errorf("enabling automatic HTTPS: %v", err) + return &AutomaticHTTPSError{internal: err} } for srvName, srv := range app.Servers { diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go index e467c84..5eb0837 100644 --- a/modules/caddyhttp/matchers.go +++ b/modules/caddyhttp/matchers.go @@ -12,7 +12,7 @@ import ( "strings" "bitbucket.org/lightcodelabs/caddy2" - "bitbucket.org/lightcodelabs/caddy2/internal/caddyscript" + "bitbucket.org/lightcodelabs/caddy2/pkg/caddyscript" "go.starlark.net/starlark" ) diff --git a/modules/caddyhttp/reverseproxy/upstream.go b/modules/caddyhttp/reverseproxy/upstream.go index 7e429f9..1217861 100755 --- a/modules/caddyhttp/reverseproxy/upstream.go +++ b/modules/caddyhttp/reverseproxy/upstream.go @@ -17,12 +17,6 @@ import ( "bitbucket.org/lightcodelabs/caddy2" ) -// State represents the global state of a loadbalancer. It is used to store -// references to health checkers. -type State struct { - HealthCheckers []*HealthChecker -} - // CircuitBreaker defines the functionality of a circuit breaker module. type CircuitBreaker interface { Ok() bool @@ -76,7 +70,7 @@ var ( ) // NewLoadBalancedReverseProxy returns a collection of Upstreams that are to be loadbalanced. -func NewLoadBalancedReverseProxy(lb *LoadBalanced, state *State, ctx caddy2.Context) error { +func NewLoadBalancedReverseProxy(lb *LoadBalanced, ctx caddy2.Context) error { // set defaults if lb.NoHealthyUpstreamsMessage == "" { lb.NoHealthyUpstreamsMessage = msgNoHealthyUpstreams @@ -145,7 +139,7 @@ func NewLoadBalancedReverseProxy(lb *LoadBalanced, state *State, ctx caddy2.Cont // nu.Target.Path = uc.HealthCheckPath go nu.healthChecker.ScheduleChecks(nu.Target.String()) - state.HealthCheckers = append(state.HealthCheckers, nu.healthChecker) + lb.HealthCheckers = append(lb.HealthCheckers, nu.healthChecker) us = append(us, nu) } @@ -178,6 +172,25 @@ type LoadBalanced struct { // NoHealthyUpstreamsMessage is returned as a response when there are no healthy upstreams to loadbalance to. NoHealthyUpstreamsMessage string `json:"no_healthy_upstreams_message"` + + // TODO :- store healthcheckers as package level state where each upstream gets a single healthchecker + // currently a healthchecker is created for each upstream defined, even if a healthchecker was previously created + // for that upstream + HealthCheckers []*HealthChecker +} + +// Cleanup stops all health checkers on a loadbalanced reverse proxy. +func (lb *LoadBalanced) Cleanup() error { + for _, hc := range lb.HealthCheckers { + hc.Stop() + } + + return nil +} + +// Provision sets up a new loadbalanced reverse proxy. +func (lb *LoadBalanced) Provision(ctx caddy2.Context) error { + return NewLoadBalancedReverseProxy(lb, ctx) } // ServeHTTP implements the http.Handler interface to dispatch an http request to the proper |