updates, sort interface
This commit is contained in:
53
hugo.go
53
hugo.go
@@ -18,7 +18,6 @@ import (
|
||||
"github.com/spf13/hugo/commands"
|
||||
)
|
||||
|
||||
// Setup function
|
||||
func Setup(c *setup.Controller) (middleware.Middleware, error) {
|
||||
commands.Execute()
|
||||
|
||||
@@ -30,11 +29,22 @@ func Setup(c *setup.Controller) (middleware.Middleware, error) {
|
||||
type handler struct{ Next middleware.Handler }
|
||||
|
||||
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
// Only handle /admin path
|
||||
if middleware.Path(r.URL.Path).Matches("/admin") {
|
||||
page := utils.ParseComponents(r)[1]
|
||||
code := 404
|
||||
var err error
|
||||
var page string
|
||||
code := 404
|
||||
|
||||
// If the length of the components string is less than one, the variable
|
||||
// page will always be "admin"
|
||||
if len(utils.ParseComponents(r)) > 1 {
|
||||
page = utils.ParseComponents(r)[1]
|
||||
} else {
|
||||
page = utils.ParseComponents(r)[0]
|
||||
}
|
||||
|
||||
// If the page isn't "assets" neither "edit", it should always put a
|
||||
// trailing slash in the path
|
||||
if page != "assets" && page != "edit" {
|
||||
if r.URL.Path[len(r.URL.Path)-1] != '/' {
|
||||
http.Redirect(w, r, r.URL.Path+"/", http.StatusTemporaryRedirect)
|
||||
@@ -42,6 +52,13 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
|
||||
}
|
||||
}
|
||||
|
||||
// If the current page is only "/admin/", redirect to "/admin/browse/contents"
|
||||
if r.URL.Path == "/admin/" {
|
||||
http.Redirect(w, r, "/admin/browse/content/", http.StatusTemporaryRedirect)
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// Serve the static assets
|
||||
if page == "assets" {
|
||||
filename := strings.Replace(r.URL.Path, "/admin/", "", 1)
|
||||
file, err := assets.Asset(filename)
|
||||
@@ -50,28 +67,36 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
|
||||
return 404, nil
|
||||
}
|
||||
|
||||
// Get the file extension ant its mime type
|
||||
extension := filepath.Ext(filename)
|
||||
mime := mime.TypeByExtension(extension)
|
||||
|
||||
header := w.Header()
|
||||
header.Set("Content-Type", mime)
|
||||
|
||||
// Write the header with the Content-Type and write the file
|
||||
// content to the buffer
|
||||
w.Header().Set("Content-Type", mime)
|
||||
w.Write(file)
|
||||
return 200, nil
|
||||
}
|
||||
|
||||
if page == "browse" {
|
||||
code, err = browse.Execute(w, r)
|
||||
}
|
||||
|
||||
if page == "edit" {
|
||||
code, err = edit.Execute(w, r)
|
||||
}
|
||||
|
||||
// If the url matches exactly with /admin/settings/ serve that page
|
||||
// page variable isn't used here to avoid people using URLs like
|
||||
// "/admin/settings/something".
|
||||
if r.URL.Path == "/admin/settings/" {
|
||||
code, err = settings.Execute(w, r)
|
||||
}
|
||||
|
||||
// Browse page
|
||||
if page == "browse" {
|
||||
code, err = browse.Execute(w, r)
|
||||
}
|
||||
|
||||
// Edit page
|
||||
if page == "edit" {
|
||||
code, err = edit.Execute(w, r)
|
||||
}
|
||||
|
||||
// Whenever the header "X-Refenerate" is true, the website should be
|
||||
// regenerated. Used in edit and settings, for example.
|
||||
if r.Header.Get("X-Regenerate") == "true" {
|
||||
commands.Execute()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user