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:
parent
fec7fa8bfd
commit
043eb1d9e5
12 changed files with 65 additions and 24 deletions
|
@ -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"
|
||||
|
|
|
@ -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 {
|
||||
|
|
1
go.mod
1
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
|
||||
)
|
||||
|
|
2
go.sum
2
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=
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)))
|
|
@ -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"
|
||||
)
|
||||
|
Loading…
Reference in a new issue