filebrowser/caddy/hugo/hugo.go

52 lines
1.1 KiB
Go
Raw Normal View History

package hugo
import (
"net/http"
"github.com/hacdias/filemanager"
2017-08-14 17:35:25 +00:00
"github.com/hacdias/filemanager/caddy/parser"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
2017-08-14 17:35:25 +00:00
func init() {
caddy.RegisterPlugin("hugo", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
2017-08-14 17:35:25 +00:00
type plugin struct {
Next httpserver.Handler
Configs []*filemanager.FileManager
}
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
2017-08-14 17:35:25 +00:00
func (f plugin) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
for i := range f.Configs {
// Checks if this Path should be handled by File Manager.
2017-08-14 17:35:25 +00:00
if !httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) {
continue
}
2017-08-14 17:35:25 +00:00
f.Configs[i].ServeHTTP(w, r)
2017-07-25 10:57:27 +00:00
return 0, nil
}
2017-08-14 17:35:25 +00:00
return f.Next.ServeHTTP(w, r)
}
2017-08-14 17:35:25 +00:00
// setup configures a new FileManager middleware instance.
func setup(c *caddy.Controller) error {
configs, err := parser.Parse(c, "hugo")
if err != nil {
return err
}
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
return plugin{Configs: configs, Next: next}
})
2017-08-14 17:35:25 +00:00
return nil
}