summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordev <navdgo@gmail.com>2019-05-14 10:35:41 -0400
committerdev <navdgo@gmail.com>2019-05-20 14:48:26 -0400
commit043eb1d9e5db456b9b78c0423cb44716fc81a932 (patch)
treea78aee0752b03c4f3c3cb45d409107e64faa10e3
parentfec7fa8bfda713e8042b9bbf9a480c7792b78c41 (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()
-rw-r--r--cmd/caddy2/main.go1
-rw-r--r--context.go1
-rw-r--r--go.mod1
-rw-r--r--go.sum2
-rw-r--r--internal/caddyscript/lib/lib.go11
-rw-r--r--modules/caddyhttp/caddyhttp.go13
-rw-r--r--modules/caddyhttp/matchers.go2
-rwxr-xr-xmodules/caddyhttp/reverseproxy/upstream.go29
-rw-r--r--pkg/caddyscript/lib/http.go (renamed from internal/caddyscript/lib/http.go)27
-rw-r--r--pkg/caddyscript/lib/regex.go (renamed from internal/caddyscript/lib/regex.go)0
-rw-r--r--pkg/caddyscript/lib/time.go (renamed from internal/caddyscript/lib/time.go)0
-rw-r--r--pkg/caddyscript/matcherenv.go (renamed from internal/caddyscript/matcherenv.go)2
12 files changed, 65 insertions, 24 deletions
diff --git a/cmd/caddy2/main.go b/cmd/caddy2/main.go
index 482c4f5..81f670b 100644
--- a/cmd/caddy2/main.go
+++ b/cmd/caddy2/main.go
@@ -4,7 +4,6 @@ import (
caddycmd "bitbucket.org/lightcodelabs/caddy2/cmd"
// this is where modules get plugged in
-
_ "bitbucket.org/lightcodelabs/caddy2/modules/caddyhttp"
_ "bitbucket.org/lightcodelabs/caddy2/modules/caddyhttp/caddylog"
_ "bitbucket.org/lightcodelabs/caddy2/modules/caddyhttp/reverseproxy"
diff --git a/context.go b/context.go
index cfb183a..e437f08 100644
--- a/context.go
+++ b/context.go
@@ -39,6 +39,7 @@ func NewContext(ctx Context) (Context, context.CancelFunc) {
c, cancel := context.WithCancel(ctx.Context)
wrappedCancel := func() {
cancel()
+
for modName, modInstances := range newCtx.moduleInstances {
for _, inst := range modInstances {
if cu, ok := inst.(CleanerUpper); ok {
diff --git a/go.mod b/go.mod
index 89aa72e..b7ab09d 100644
--- a/go.mod
+++ b/go.mod
@@ -6,6 +6,7 @@ require (
github.com/go-acme/lego v2.5.0+incompatible
github.com/klauspost/cpuid v1.2.1
github.com/mholt/certmagic v0.5.1
+ github.com/starlight-go/starlight v0.0.0-20181207205707-b06f321544f3
go.starlark.net v0.0.0-20190506145734-95b2783e7d63
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c
)
diff --git a/go.sum b/go.sum
index 7b32c47..414f6f2 100644
--- a/go.sum
+++ b/go.sum
@@ -11,6 +11,8 @@ github.com/mholt/certmagic v0.5.1/go.mod h1:g4cOPxcjV0oFq3qwpjSA30LReKD8AoIfwAY9
github.com/miekg/dns v1.1.3 h1:1g0r1IvskvgL8rR+AcHzUA+oFmGcQlaIm4IqakufeMM=
github.com/miekg/dns v1.1.3/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/starlight-go/starlight v0.0.0-20181207205707-b06f321544f3 h1:/fBh1Ot84ILt/ociFHO98wJ9LxIMA3UG8B0unUJPFpY=
+github.com/starlight-go/starlight v0.0.0-20181207205707-b06f321544f3/go.mod h1:pxOc2ZuBV+CNlQgzq/HJ9Z9G/eoEMHFeuGohOvva4Co=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
go.starlark.net v0.0.0-20190506145734-95b2783e7d63 h1:WVWIwnUjRw+Tf2iLM4GbAKbSteS4EOhS3z5qXOXN3Vk=
diff --git a/internal/caddyscript/lib/lib.go b/internal/caddyscript/lib/lib.go
deleted file mode 100644
index 7853e7a..0000000
--- a/internal/caddyscript/lib/lib.go
+++ /dev/null
@@ -1,11 +0,0 @@
-package caddyscript
-
-import (
- "fmt"
-
- "go.starlark.net/starlark"
-)
-
-func invalidReciever(v starlark.Value, want string) (starlark.Value, error) {
- return starlark.None, fmt.Errorf("invalid receiver: receiver set to type %v, want %v", v.Type(), want)
-}
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
diff --git a/internal/caddyscript/lib/http.go b/pkg/caddyscript/lib/http.go
index 233b43f..0e42458 100644
--- a/internal/caddyscript/lib/http.go
+++ b/pkg/caddyscript/lib/http.go
@@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
+ "github.com/starlight-go/starlight/convert"
"go.starlark.net/starlark"
)
@@ -12,7 +13,7 @@ type HTTPRequest struct{ Req *http.Request }
// AttrNames defines what properties and methods are available on the HTTPRequest type.
func (r HTTPRequest) AttrNames() []string {
- return []string{"header", "query", "url", "method", "host", "tls"}
+ return []string{"header", "query", "url", "method", "host", "tls", "redirect"}
}
func (r HTTPRequest) Freeze() {}
@@ -32,9 +33,33 @@ func (r HTTPRequest) Header(thread *starlark.Thread, fn *starlark.Builtin, args
return starlark.String(r.Req.Header.Get(key)), nil
}
+// Redirect handles an http redirect from starlark code.
+func (r HTTPRequest) Redirect(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
+ var w starlark.Value
+ var req HTTPRequest
+ var newURL string
+ err := starlark.UnpackPositionalArgs(fn.Name(), args, kwargs, 3, &w, &req, &newURL)
+ if err != nil {
+ return starlark.None, fmt.Errorf("unpacking arguments: %v", err.Error())
+ }
+
+ writer := convert.FromValue(w)
+ if w, ok := writer.(http.ResponseWriter); ok {
+ http.Redirect(w, req.Req, newURL, http.StatusSeeOther)
+ return starlark.None, nil
+ }
+
+ return starlark.None, fmt.Errorf("first provided argument is not http.ResponseWriter")
+}
+
// Attr defines what happens when props or methods are called on the HTTPRequest type.
func (r HTTPRequest) Attr(name string) (starlark.Value, error) {
switch name {
+ case "redirect":
+ b := starlark.NewBuiltin("Redirect", r.Redirect)
+ b = b.BindReceiver(r)
+
+ return b, nil
case "tls":
tls := new(starlark.Dict)
tls.SetKey(starlark.String("cipher_suite"), starlark.MakeUint(uint(r.Req.TLS.CipherSuite)))
diff --git a/internal/caddyscript/lib/regex.go b/pkg/caddyscript/lib/regex.go
index b151e64..b151e64 100644
--- a/internal/caddyscript/lib/regex.go
+++ b/pkg/caddyscript/lib/regex.go
diff --git a/internal/caddyscript/lib/time.go b/pkg/caddyscript/lib/time.go
index 2ab57cc..2ab57cc 100644
--- a/internal/caddyscript/lib/time.go
+++ b/pkg/caddyscript/lib/time.go
diff --git a/internal/caddyscript/matcherenv.go b/pkg/caddyscript/matcherenv.go
index c6c8c0e..aa9d7fd 100644
--- a/internal/caddyscript/matcherenv.go
+++ b/pkg/caddyscript/matcherenv.go
@@ -3,7 +3,7 @@ package caddyscript
import (
"net/http"
- caddyscript "bitbucket.org/lightcodelabs/caddy2/internal/caddyscript/lib"
+ caddyscript "bitbucket.org/lightcodelabs/caddy2/pkg/caddyscript/lib"
"go.starlark.net/starlark"
)