filebrowser/hugo.go

71 lines
1.6 KiB
Go
Raw Normal View History

2015-09-16 12:02:19 +00:00
//go:generate go-bindata -pkg assets -o assets/assets.go templates/ assets/css/ assets/js/ assets/fonts/
2015-09-13 11:14:18 +00:00
2015-09-12 08:52:41 +00:00
package hugo
import (
2015-09-13 19:37:20 +00:00
"mime"
2015-09-12 08:52:41 +00:00
"net/http"
2015-09-13 19:37:20 +00:00
"path/filepath"
2015-09-13 11:14:18 +00:00
"strings"
2015-09-12 08:52:41 +00:00
2015-09-13 11:14:18 +00:00
"github.com/hacdias/caddy-hugo/assets"
"github.com/hacdias/caddy-hugo/browse"
2015-09-13 11:14:18 +00:00
"github.com/hacdias/caddy-hugo/edit"
2015-09-13 21:48:52 +00:00
"github.com/hacdias/caddy-hugo/settings"
"github.com/hacdias/caddy-hugo/utils"
2015-09-12 08:52:41 +00:00
"github.com/mholt/caddy/config/setup"
"github.com/mholt/caddy/middleware"
2015-09-12 21:18:29 +00:00
"github.com/spf13/hugo/commands"
2015-09-12 08:52:41 +00:00
)
2015-09-12 12:05:31 +00:00
// Setup function
2015-09-12 08:52:41 +00:00
func Setup(c *setup.Controller) (middleware.Middleware, error) {
2015-09-12 12:05:31 +00:00
commands.Execute()
2015-09-12 10:33:39 +00:00
2015-09-12 08:52:41 +00:00
return func(next middleware.Handler) middleware.Handler {
2015-09-12 17:58:10 +00:00
return &handler{Next: next}
2015-09-12 08:52:41 +00:00
}, nil
}
2015-09-12 13:08:48 +00:00
type handler struct{ Next middleware.Handler }
2015-09-12 08:52:41 +00:00
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
2015-09-13 21:48:52 +00:00
if middleware.Path(r.URL.Path).Matches("/admin") {
page := utils.ParseComponents(r)[1]
2015-09-17 14:38:04 +00:00
code := 400
var err error
2015-09-13 21:48:52 +00:00
2015-09-16 11:18:12 +00:00
if page == "assets" {
2015-09-16 12:02:19 +00:00
filename := strings.Replace(r.URL.Path, "/admin/", "", 1)
2015-09-13 21:48:52 +00:00
file, err := assets.Asset(filename)
if err != nil {
return 404, nil
}
extension := filepath.Ext(filename)
mime := mime.TypeByExtension(extension)
header := w.Header()
header.Set("Content-Type", mime)
w.Write(file)
return 200, nil
} else if page == "browse" {
2015-09-17 14:38:04 +00:00
code, err = browse.Execute(w, r)
2015-09-13 21:48:52 +00:00
} else if page == "edit" {
2015-09-17 14:38:04 +00:00
code, err = edit.Execute(w, r)
2015-09-13 21:48:52 +00:00
} else if page == "settings" {
2015-09-17 14:38:04 +00:00
code, err = settings.Execute(w, r)
2015-09-13 11:14:18 +00:00
}
2015-09-17 14:38:04 +00:00
if r.Header.Get("X-Regenerate") == "true" {
commands.Execute()
}
return code, err
2015-09-13 11:14:18 +00:00
}
2015-09-13 21:48:52 +00:00
return h.Next.ServeHTTP(w, r)
2015-09-13 11:14:18 +00:00
}