summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/reverseproxy/module.go
blob: cc53bf5364e3e193ec514d82349a9799d13af5b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package reverseproxy

import (
	"fmt"

	"bitbucket.org/lightcodelabs/caddy2"
)

// Register caddy module.
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
		},
	})
}