filebrowser/hugo.go

89 lines
2.1 KiB
Go
Raw Normal View History

2015-09-14 03:37:20 +08:00
//go:generate go-bindata -pkg assets -o assets/assets.go static/css/ static/js/ templates/
2015-09-13 19:14:18 +08:00
2015-09-12 16:52:41 +08:00
package hugo
import (
2015-09-14 03:37:20 +08:00
"mime"
2015-09-12 16:52:41 +08:00
"net/http"
2015-09-14 03:37:20 +08:00
"path/filepath"
2015-09-13 19:14:18 +08:00
"strings"
2015-09-12 16:52:41 +08:00
2015-09-13 19:14:18 +08:00
"github.com/hacdias/caddy-hugo/assets"
"github.com/hacdias/caddy-hugo/edit"
2015-09-12 16:52:41 +08:00
"github.com/mholt/caddy/config/setup"
"github.com/mholt/caddy/middleware"
2015-09-13 05:18:29 +08:00
"github.com/spf13/hugo/commands"
2015-09-12 16:52:41 +08:00
)
2015-09-13 19:14:18 +08:00
const (
mainURL string = "/admin"
contentURL string = mainURL + "/content"
browseURL string = mainURL + "/browse"
editURL string = mainURL + "/edit"
settingsURL string = mainURL + "/settings"
assetsURL string = mainURL + "/assets"
)
2015-09-12 20:05:31 +08:00
// Setup function
2015-09-12 16:52:41 +08:00
func Setup(c *setup.Controller) (middleware.Middleware, error) {
2015-09-12 20:05:31 +08:00
commands.Execute()
2015-09-12 18:33:39 +08:00
2015-09-12 16:52:41 +08:00
return func(next middleware.Handler) middleware.Handler {
2015-09-13 01:58:10 +08:00
return &handler{Next: next}
2015-09-12 16:52:41 +08:00
}, nil
}
2015-09-12 21:08:48 +08:00
type handler struct{ Next middleware.Handler }
2015-09-12 16:52:41 +08:00
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
2015-09-13 19:14:18 +08:00
if urlMatch(r, "/admin") {
return route(w, r)
2015-09-12 20:05:31 +08:00
}
2015-09-12 21:16:15 +08:00
return h.Next.ServeHTTP(w, r)
2015-09-12 20:05:31 +08:00
}
2015-09-13 19:14:18 +08:00
func route(w http.ResponseWriter, r *http.Request) (int, error) {
if urlMatch(r, contentURL) {
// content folder management
w.Write([]byte("Show Content Folder"))
} else if urlMatch(r, browseURL) {
// browse files
w.Write([]byte("Show Data Folder"))
} else if urlMatch(r, editURL) {
// edit file
return edit.Execute(w, r, strings.Replace(r.URL.Path, editURL+"/", "", 1))
} else if urlMatch(r, settingsURL) {
// edit settings
w.Write([]byte("Settings Page"))
} else if urlMatch(r, assetsURL) {
// assets like css, javascript and images
2015-09-14 03:37:20 +08:00
filename := strings.Replace(r.URL.Path, assetsURL, "static", 1)
file, err := assets.Asset(filename)
2015-09-13 19:14:18 +08:00
if err != nil {
return 404, nil
}
2015-09-14 03:37:20 +08:00
extension := filepath.Ext(filename)
mime := mime.TypeByExtension(extension)
header := w.Header()
header.Set("Content-Type", mime)
2015-09-13 19:14:18 +08:00
w.Write(file)
} else if r.URL.Path == mainURL || r.URL.Path == mainURL+"/" {
// dashboard
w.Write([]byte("Dashboard"))
} else {
return 404, nil
}
return 200, nil
}
func urlMatch(r *http.Request, str string) bool {
return middleware.Path(r.URL.Path).Matches(str)
}