filebrowser/browse/browse.go

55 lines
1.1 KiB
Go
Raw Normal View History

package browse
import (
2015-09-17 16:41:31 +00:00
"log"
"net/http"
2015-09-17 16:41:31 +00:00
"strings"
2015-09-18 12:52:10 +00:00
"text/template"
2015-09-20 08:15:21 +00:00
"github.com/hacdias/caddy-hugo/config"
2015-09-18 12:52:10 +00:00
"github.com/hacdias/caddy-hugo/utils"
2015-09-17 16:41:31 +00:00
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware/browse"
)
2015-09-20 08:15:21 +00:00
// ServeHTTP is used to serve the content of Browse page
// using Browse middleware from Caddy
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
// Removes the page main path from the URL
2015-09-17 16:41:31 +00:00
r.URL.Path = strings.Replace(r.URL.Path, "/admin/browse", "", 1)
2015-09-20 08:15:21 +00:00
// If the URL is blank now, replace it with a trailing slash
2015-09-17 16:41:31 +00:00
if r.URL.Path == "" {
r.URL.Path = "/"
}
2015-09-18 12:52:10 +00:00
functions := template.FuncMap{
2015-09-20 08:15:21 +00:00
"CanBeEdited": utils.CanBeEdited,
2015-09-19 13:25:35 +00:00
"Defined": utils.Defined,
2015-09-18 12:52:10 +00:00
}
tpl, err := utils.GetTemplate(r, functions, "browse")
2015-09-17 16:41:31 +00:00
if err != nil {
log.Print(err)
return 500, err
}
b := browse.Browse{
Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
return 404, nil
}),
Root: "./",
Configs: []browse.Config{
browse.Config{
PathScope: "/",
2015-09-20 08:15:21 +00:00
Variables: c,
2015-09-17 16:41:31 +00:00
Template: tpl,
},
},
2015-09-18 08:47:02 +00:00
IgnoreIndexes: true,
2015-09-17 16:41:31 +00:00
}
return b.ServeHTTP(w, r)
}