2017-08-10 11:18:51 +00:00
|
|
|
package jekyll
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/hacdias/filemanager"
|
2017-08-14 17:35:25 +00:00
|
|
|
"github.com/hacdias/filemanager/caddy/parser"
|
2017-08-10 11:18:51 +00:00
|
|
|
"github.com/mholt/caddy"
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
2017-08-14 17:35:25 +00:00
|
|
|
func init() {
|
|
|
|
caddy.RegisterPlugin("jekyll", caddy.Plugin{
|
|
|
|
ServerType: "http",
|
|
|
|
Action: setup,
|
2017-08-10 11:18:51 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-14 17:35:25 +00:00
|
|
|
type plugin struct {
|
|
|
|
Next httpserver.Handler
|
|
|
|
Configs []*filemanager.FileManager
|
2017-08-10 11:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2017-08-10 11:18:51 +00:00
|
|
|
// 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) {
|
2017-08-10 11:18:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-08-14 17:35:25 +00:00
|
|
|
f.Configs[i].ServeHTTP(w, r)
|
2017-08-10 11:18:51 +00:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2017-08-14 17:35:25 +00:00
|
|
|
return f.Next.ServeHTTP(w, r)
|
2017-08-10 11:18:51 +00:00
|
|
|
}
|
|
|
|
|
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, "jekyll")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
|
|
|
|
return plugin{Configs: configs, Next: next}
|
2017-08-10 11:18:51 +00:00
|
|
|
})
|
|
|
|
|
2017-08-14 17:35:25 +00:00
|
|
|
return nil
|
2017-08-10 11:18:51 +00:00
|
|
|
}
|