mirror of
https://github.com/caddyserver/caddy.git
synced 2025-05-31 16:39:06 +08:00
Major refactoring of middleware and parser in progress
This commit is contained in:
@ -3,11 +3,12 @@ package proxy
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/mholt/caddy/middleware"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/mholt/caddy/middleware"
|
||||
)
|
||||
|
||||
var errUnreachable = errors.New("Unreachable backend")
|
||||
@ -21,8 +22,8 @@ type Proxy struct {
|
||||
// An upstream manages a pool of proxy upstream hosts. Select should return a
|
||||
// suitable upstream host, or nil if no such hosts are available.
|
||||
type Upstream interface {
|
||||
// The path this upstream host should be routed on
|
||||
From() string
|
||||
//The path this upstream host should be routed on
|
||||
from() string
|
||||
// Selects an upstream host to be routed to.
|
||||
Select() *UpstreamHost
|
||||
}
|
||||
@ -54,7 +55,7 @@ func (uh *UpstreamHost) Down() bool {
|
||||
func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
|
||||
for _, upstream := range p.Upstreams {
|
||||
if middleware.Path(r.URL.Path).Matches(upstream.From()) {
|
||||
if middleware.Path(r.URL.Path).Matches(upstream.from()) {
|
||||
var replacer middleware.Replacer
|
||||
start := time.Now()
|
||||
requestHost := r.Host
|
||||
@ -119,14 +120,3 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
|
||||
return p.Next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// New creates a new instance of proxy middleware.
|
||||
func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||
if upstreams, err := newStaticUpstreams(c); err == nil {
|
||||
return func(next middleware.Handler) middleware.Handler {
|
||||
return Proxy{Next: next, Upstreams: upstreams}
|
||||
}, nil
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,14 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"github.com/mholt/caddy/middleware"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type staticUpstream struct {
|
||||
from string
|
||||
type StaticUpstream struct {
|
||||
From string
|
||||
Hosts HostPool
|
||||
Policy Policy
|
||||
|
||||
@ -24,127 +20,11 @@ type staticUpstream struct {
|
||||
}
|
||||
}
|
||||
|
||||
func newStaticUpstreams(c middleware.Controller) ([]Upstream, error) {
|
||||
var upstreams []Upstream
|
||||
|
||||
for c.Next() {
|
||||
upstream := &staticUpstream{
|
||||
from: "",
|
||||
Hosts: nil,
|
||||
Policy: &Random{},
|
||||
FailTimeout: 10 * time.Second,
|
||||
MaxFails: 1,
|
||||
}
|
||||
var proxyHeaders http.Header
|
||||
if !c.Args(&upstream.from) {
|
||||
return upstreams, c.ArgErr()
|
||||
}
|
||||
to := c.RemainingArgs()
|
||||
if len(to) == 0 {
|
||||
return upstreams, c.ArgErr()
|
||||
}
|
||||
|
||||
for c.NextBlock() {
|
||||
switch c.Val() {
|
||||
case "policy":
|
||||
if !c.NextArg() {
|
||||
return upstreams, c.ArgErr()
|
||||
}
|
||||
switch c.Val() {
|
||||
case "random":
|
||||
upstream.Policy = &Random{}
|
||||
case "round_robin":
|
||||
upstream.Policy = &RoundRobin{}
|
||||
case "least_conn":
|
||||
upstream.Policy = &LeastConn{}
|
||||
default:
|
||||
return upstreams, c.ArgErr()
|
||||
}
|
||||
case "fail_timeout":
|
||||
if !c.NextArg() {
|
||||
return upstreams, c.ArgErr()
|
||||
}
|
||||
if dur, err := time.ParseDuration(c.Val()); err == nil {
|
||||
upstream.FailTimeout = dur
|
||||
} else {
|
||||
return upstreams, err
|
||||
}
|
||||
case "max_fails":
|
||||
if !c.NextArg() {
|
||||
return upstreams, c.ArgErr()
|
||||
}
|
||||
if n, err := strconv.Atoi(c.Val()); err == nil {
|
||||
upstream.MaxFails = int32(n)
|
||||
} else {
|
||||
return upstreams, err
|
||||
}
|
||||
case "health_check":
|
||||
if !c.NextArg() {
|
||||
return upstreams, c.ArgErr()
|
||||
}
|
||||
upstream.HealthCheck.Path = c.Val()
|
||||
upstream.HealthCheck.Interval = 30 * time.Second
|
||||
if c.NextArg() {
|
||||
if dur, err := time.ParseDuration(c.Val()); err == nil {
|
||||
upstream.HealthCheck.Interval = dur
|
||||
} else {
|
||||
return upstreams, err
|
||||
}
|
||||
}
|
||||
case "proxy_header":
|
||||
var header, value string
|
||||
if !c.Args(&header, &value) {
|
||||
return upstreams, c.ArgErr()
|
||||
}
|
||||
if proxyHeaders == nil {
|
||||
proxyHeaders = make(map[string][]string)
|
||||
}
|
||||
proxyHeaders.Add(header, value)
|
||||
}
|
||||
}
|
||||
|
||||
upstream.Hosts = make([]*UpstreamHost, len(to))
|
||||
for i, host := range to {
|
||||
if !strings.HasPrefix(host, "http") {
|
||||
host = "http://" + host
|
||||
}
|
||||
uh := &UpstreamHost{
|
||||
Name: host,
|
||||
Conns: 0,
|
||||
Fails: 0,
|
||||
FailTimeout: upstream.FailTimeout,
|
||||
Unhealthy: false,
|
||||
ExtraHeaders: proxyHeaders,
|
||||
CheckDown: func(upstream *staticUpstream) UpstreamHostDownFunc {
|
||||
return func(uh *UpstreamHost) bool {
|
||||
if uh.Unhealthy {
|
||||
return true
|
||||
}
|
||||
if uh.Fails >= upstream.MaxFails &&
|
||||
upstream.MaxFails != 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}(upstream),
|
||||
}
|
||||
if baseUrl, err := url.Parse(uh.Name); err == nil {
|
||||
uh.ReverseProxy = NewSingleHostReverseProxy(baseUrl)
|
||||
} else {
|
||||
return upstreams, err
|
||||
}
|
||||
upstream.Hosts[i] = uh
|
||||
}
|
||||
|
||||
if upstream.HealthCheck.Path != "" {
|
||||
go upstream.healthCheckWorker(nil)
|
||||
}
|
||||
upstreams = append(upstreams, upstream)
|
||||
}
|
||||
return upstreams, nil
|
||||
func (u *StaticUpstream) from() string {
|
||||
return u.From
|
||||
}
|
||||
|
||||
func (u *staticUpstream) healthCheck() {
|
||||
func (u *StaticUpstream) healthCheck() {
|
||||
for _, host := range u.Hosts {
|
||||
hostUrl := host.Name + u.HealthCheck.Path
|
||||
if r, err := http.Get(hostUrl); err == nil {
|
||||
@ -157,7 +37,7 @@ func (u *staticUpstream) healthCheck() {
|
||||
}
|
||||
}
|
||||
|
||||
func (u *staticUpstream) healthCheckWorker(stop chan struct{}) {
|
||||
func (u *StaticUpstream) HealthCheckWorker(stop chan struct{}) {
|
||||
ticker := time.NewTicker(u.HealthCheck.Interval)
|
||||
u.healthCheck()
|
||||
for {
|
||||
@ -172,11 +52,7 @@ func (u *staticUpstream) healthCheckWorker(stop chan struct{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func (u *staticUpstream) From() string {
|
||||
return u.from
|
||||
}
|
||||
|
||||
func (u *staticUpstream) Select() *UpstreamHost {
|
||||
func (u *StaticUpstream) Select() *UpstreamHost {
|
||||
pool := u.Hosts
|
||||
if len(pool) == 1 {
|
||||
if pool[0].Down() {
|
||||
|
Reference in New Issue
Block a user