2015-04-15 15:11:32 -05:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
// virtualHost represents a virtual host/server. While a Server
|
|
|
|
// is what actually binds to the address, a user may want to serve
|
2015-04-21 13:12:58 -05:00
|
|
|
// multiple sites on a single address, and this is what a
|
2015-04-15 15:11:32 -05:00
|
|
|
// virtualHost allows us to do.
|
|
|
|
type virtualHost struct {
|
2015-05-04 07:53:54 -05:00
|
|
|
config Config
|
2015-04-15 15:11:32 -05:00
|
|
|
fileServer middleware.Handler
|
|
|
|
stack middleware.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildStack builds the server's middleware stack based
|
|
|
|
// on its config. This method should be called last before
|
|
|
|
// ListenAndServe begins.
|
|
|
|
func (vh *virtualHost) buildStack() error {
|
2015-09-19 21:34:23 -05:00
|
|
|
vh.fileServer = middleware.FileServer(http.Dir(vh.config.Root), []string{vh.config.ConfigFile})
|
2016-02-17 20:11:03 -05:00
|
|
|
vh.compile(vh.config.Middleware)
|
2015-04-15 15:11:32 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// compile is an elegant alternative to nesting middleware function
|
|
|
|
// calls like handler1(handler2(handler3(finalHandler))).
|
|
|
|
func (vh *virtualHost) compile(layers []middleware.Middleware) {
|
|
|
|
vh.stack = vh.fileServer // core app layer
|
|
|
|
for i := len(layers) - 1; i >= 0; i-- {
|
|
|
|
vh.stack = layers[i](vh.stack)
|
|
|
|
}
|
|
|
|
}
|