2015-05-05 01:04:17 +08:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
2015-09-29 11:16:40 +08:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2015-05-05 01:04:17 +08:00
|
|
|
"github.com/mholt/caddy/config/parse"
|
2015-09-29 11:16:40 +08:00
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-05-05 01:04:17 +08:00
|
|
|
"github.com/mholt/caddy/server"
|
|
|
|
)
|
|
|
|
|
2015-09-29 11:16:40 +08:00
|
|
|
// Controller is given to the setup function of middlewares which
|
2015-10-15 14:07:26 +08:00
|
|
|
// gives them access to be able to read tokens and set config. Each
|
|
|
|
// virtualhost gets their own server config and dispenser.
|
2015-05-05 01:04:17 +08:00
|
|
|
type Controller struct {
|
|
|
|
*server.Config
|
|
|
|
parse.Dispenser
|
2015-10-15 14:07:26 +08:00
|
|
|
|
|
|
|
// OncePerServerBlock is a function that executes f
|
|
|
|
// exactly once per server block, no matter how many
|
|
|
|
// hosts are associated with it.
|
2015-10-15 13:45:28 +08:00
|
|
|
OncePerServerBlock func(f func())
|
2015-05-05 01:04:17 +08:00
|
|
|
}
|
2015-09-29 11:16:40 +08:00
|
|
|
|
|
|
|
// NewTestController creates a new *Controller for
|
|
|
|
// the input specified, with a filename of "Testfile"
|
|
|
|
//
|
|
|
|
// Used primarily for testing but needs to be exported so
|
|
|
|
// add-ons can use this as a convenience.
|
|
|
|
func NewTestController(input string) *Controller {
|
|
|
|
return &Controller{
|
2015-10-14 07:49:53 +08:00
|
|
|
Config: &server.Config{
|
|
|
|
Root: ".",
|
|
|
|
},
|
2015-09-29 11:16:40 +08:00
|
|
|
Dispenser: parse.NewDispenser("Testfile", strings.NewReader(input)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmptyNext is a no-op function that can be passed into
|
|
|
|
// middleware.Middleware functions so that the assignment
|
|
|
|
// to the Next field of the Handler can be tested.
|
|
|
|
//
|
|
|
|
// Used primarily for testing but needs to be exported so
|
|
|
|
// add-ons can use this as a convenience.
|
|
|
|
var EmptyNext = middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// SameNext does a pointer comparison between next1 and next2.
|
|
|
|
//
|
|
|
|
// Used primarily for testing but needs to be exported so
|
|
|
|
// add-ons can use this as a convenience.
|
|
|
|
func SameNext(next1, next2 middleware.Handler) bool {
|
|
|
|
return fmt.Sprintf("%v", next1) == fmt.Sprintf("%v", next2)
|
|
|
|
}
|