1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

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()
This commit is contained in:
dev 2019-05-14 10:35:41 -04:00
parent fec7fa8bfd
commit 043eb1d9e5
12 changed files with 65 additions and 24 deletions

View file

@ -4,7 +4,6 @@ import (
caddycmd "bitbucket.org/lightcodelabs/caddy2/cmd" caddycmd "bitbucket.org/lightcodelabs/caddy2/cmd"
// this is where modules get plugged in // this is where modules get plugged in
_ "bitbucket.org/lightcodelabs/caddy2/modules/caddyhttp" _ "bitbucket.org/lightcodelabs/caddy2/modules/caddyhttp"
_ "bitbucket.org/lightcodelabs/caddy2/modules/caddyhttp/caddylog" _ "bitbucket.org/lightcodelabs/caddy2/modules/caddyhttp/caddylog"
_ "bitbucket.org/lightcodelabs/caddy2/modules/caddyhttp/reverseproxy" _ "bitbucket.org/lightcodelabs/caddy2/modules/caddyhttp/reverseproxy"

View file

@ -39,6 +39,7 @@ func NewContext(ctx Context) (Context, context.CancelFunc) {
c, cancel := context.WithCancel(ctx.Context) c, cancel := context.WithCancel(ctx.Context)
wrappedCancel := func() { wrappedCancel := func() {
cancel() cancel()
for modName, modInstances := range newCtx.moduleInstances { for modName, modInstances := range newCtx.moduleInstances {
for _, inst := range modInstances { for _, inst := range modInstances {
if cu, ok := inst.(CleanerUpper); ok { if cu, ok := inst.(CleanerUpper); ok {

1
go.mod
View file

@ -6,6 +6,7 @@ require (
github.com/go-acme/lego v2.5.0+incompatible github.com/go-acme/lego v2.5.0+incompatible
github.com/klauspost/cpuid v1.2.1 github.com/klauspost/cpuid v1.2.1
github.com/mholt/certmagic v0.5.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 go.starlark.net v0.0.0-20190506145734-95b2783e7d63
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c
) )

2
go.sum
View file

@ -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 h1:1g0r1IvskvgL8rR+AcHzUA+oFmGcQlaIm4IqakufeMM=
github.com/miekg/dns v1.1.3/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= 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/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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
go.starlark.net v0.0.0-20190506145734-95b2783e7d63 h1:WVWIwnUjRw+Tf2iLM4GbAKbSteS4EOhS3z5qXOXN3Vk= go.starlark.net v0.0.0-20190506145734-95b2783e7d63 h1:WVWIwnUjRw+Tf2iLM4GbAKbSteS4EOhS3z5qXOXN3Vk=

View file

@ -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)
}

View file

@ -24,6 +24,7 @@ func init() {
Name: "http", Name: "http",
New: func() (interface{}, error) { return new(App), nil }, New: func() (interface{}, error) { return new(App), nil },
}) })
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -94,11 +95,21 @@ func (app *App) Validate() error {
return nil 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. // Start runs the app. It sets up automatic HTTPS if enabled.
func (app *App) Start() error { func (app *App) Start() error {
err := app.automaticHTTPS() err := app.automaticHTTPS()
if err != nil { if err != nil {
return fmt.Errorf("enabling automatic HTTPS: %v", err) return &AutomaticHTTPSError{internal: err}
} }
for srvName, srv := range app.Servers { for srvName, srv := range app.Servers {

View file

@ -12,7 +12,7 @@ import (
"strings" "strings"
"bitbucket.org/lightcodelabs/caddy2" "bitbucket.org/lightcodelabs/caddy2"
"bitbucket.org/lightcodelabs/caddy2/internal/caddyscript" "bitbucket.org/lightcodelabs/caddy2/pkg/caddyscript"
"go.starlark.net/starlark" "go.starlark.net/starlark"
) )

View file

@ -17,12 +17,6 @@ import (
"bitbucket.org/lightcodelabs/caddy2" "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. // CircuitBreaker defines the functionality of a circuit breaker module.
type CircuitBreaker interface { type CircuitBreaker interface {
Ok() bool Ok() bool
@ -76,7 +70,7 @@ var (
) )
// NewLoadBalancedReverseProxy returns a collection of Upstreams that are to be loadbalanced. // 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 // set defaults
if lb.NoHealthyUpstreamsMessage == "" { if lb.NoHealthyUpstreamsMessage == "" {
lb.NoHealthyUpstreamsMessage = msgNoHealthyUpstreams lb.NoHealthyUpstreamsMessage = msgNoHealthyUpstreams
@ -145,7 +139,7 @@ func NewLoadBalancedReverseProxy(lb *LoadBalanced, state *State, ctx caddy2.Cont
// nu.Target.Path = uc.HealthCheckPath // nu.Target.Path = uc.HealthCheckPath
go nu.healthChecker.ScheduleChecks(nu.Target.String()) 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) 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 is returned as a response when there are no healthy upstreams to loadbalance to.
NoHealthyUpstreamsMessage string `json:"no_healthy_upstreams_message"` 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 // ServeHTTP implements the http.Handler interface to dispatch an http request to the proper

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/starlight-go/starlight/convert"
"go.starlark.net/starlark" "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. // AttrNames defines what properties and methods are available on the HTTPRequest type.
func (r HTTPRequest) AttrNames() []string { 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() {} 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 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. // Attr defines what happens when props or methods are called on the HTTPRequest type.
func (r HTTPRequest) Attr(name string) (starlark.Value, error) { func (r HTTPRequest) Attr(name string) (starlark.Value, error) {
switch name { switch name {
case "redirect":
b := starlark.NewBuiltin("Redirect", r.Redirect)
b = b.BindReceiver(r)
return b, nil
case "tls": case "tls":
tls := new(starlark.Dict) tls := new(starlark.Dict)
tls.SetKey(starlark.String("cipher_suite"), starlark.MakeUint(uint(r.Req.TLS.CipherSuite))) tls.SetKey(starlark.String("cipher_suite"), starlark.MakeUint(uint(r.Req.TLS.CipherSuite)))

View file

@ -3,7 +3,7 @@ package caddyscript
import ( import (
"net/http" "net/http"
caddyscript "bitbucket.org/lightcodelabs/caddy2/internal/caddyscript/lib" caddyscript "bitbucket.org/lightcodelabs/caddy2/pkg/caddyscript/lib"
"go.starlark.net/starlark" "go.starlark.net/starlark"
) )