mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-02 05:33:38 +08:00
1ef7f3c4b1
It was implemented for almost a year but we'll probably never use it, especially since we'll match more than the path in the future.
36 lines
1.0 KiB
Go
36 lines
1.0 KiB
Go
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
|
|
// multiple sites on a single address, and this is what a
|
|
// virtualHost allows us to do.
|
|
type virtualHost struct {
|
|
config Config
|
|
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 {
|
|
vh.fileServer = middleware.FileServer(http.Dir(vh.config.Root), []string{vh.config.ConfigFile})
|
|
vh.compile(vh.config.Middleware)
|
|
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)
|
|
}
|
|
}
|