2015-09-26 20:03:34 +00:00
|
|
|
//go:generate go get github.com/jteeuwen/go-bindata
|
2015-09-27 08:38:20 +00:00
|
|
|
//go:generate go install github.com/jteeuwen/go-bindata
|
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-18 15:49:53 +00:00
|
|
|
"os"
|
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"
|
2015-09-15 20:40:46 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/browse"
|
2015-09-20 08:15:21 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/config"
|
2015-09-18 15:49:53 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/editor"
|
2015-09-15 20:40:46 +00:00
|
|
|
"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-19 13:25:35 +00:00
|
|
|
// Setup configures the middleware
|
2015-09-12 08:52:41 +00:00
|
|
|
func Setup(c *setup.Controller) (middleware.Middleware, error) {
|
2015-09-20 08:15:21 +00:00
|
|
|
config, _ := config.ParseHugo(c)
|
2015-09-20 21:00:25 +00:00
|
|
|
utils.RunHugo(config)
|
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-20 08:15:21 +00:00
|
|
|
return &CaddyHugo{Next: next, Config: config}
|
2015-09-12 08:52:41 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-09-20 08:15:21 +00:00
|
|
|
// CaddyHugo main type
|
|
|
|
type CaddyHugo struct {
|
|
|
|
Next middleware.Handler
|
|
|
|
Config *config.Config
|
|
|
|
}
|
2015-09-12 08:52:41 +00:00
|
|
|
|
2015-09-20 08:15:21 +00:00
|
|
|
func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
2015-09-18 08:47:02 +00:00
|
|
|
// Only handle /admin path
|
2015-09-13 21:48:52 +00:00
|
|
|
if middleware.Path(r.URL.Path).Matches("/admin") {
|
2015-09-17 14:38:04 +00:00
|
|
|
var err error
|
2015-09-18 08:47:02 +00:00
|
|
|
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]
|
|
|
|
}
|
2015-09-13 21:48:52 +00:00
|
|
|
|
2015-09-18 08:47:02 +00:00
|
|
|
// If the page isn't "assets" neither "edit", it should always put a
|
|
|
|
// trailing slash in the path
|
2015-09-17 20:26:06 +00:00
|
|
|
if page != "assets" && page != "edit" {
|
|
|
|
if r.URL.Path[len(r.URL.Path)-1] != '/' {
|
|
|
|
http.Redirect(w, r, r.URL.Path+"/", http.StatusTemporaryRedirect)
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-18 08:47:02 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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/" {
|
2015-09-18 15:49:53 +00:00
|
|
|
var frontmatter string
|
|
|
|
|
|
|
|
if _, err := os.Stat("config.yaml"); err == nil {
|
|
|
|
frontmatter = "yaml"
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat("config.json"); err == nil {
|
|
|
|
frontmatter = "json"
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat("config.toml"); err == nil {
|
|
|
|
frontmatter = "toml"
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/admin/edit/config."+frontmatter, http.StatusTemporaryRedirect)
|
|
|
|
return 0, nil
|
2015-09-18 08:47:02 +00:00
|
|
|
}
|
|
|
|
|
2015-09-26 21:08:12 +00:00
|
|
|
// Serve the static assets
|
|
|
|
if page == "assets" {
|
|
|
|
return serveAssets(w, r)
|
|
|
|
}
|
|
|
|
|
2015-09-18 08:47:02 +00:00
|
|
|
// Browse page
|
2015-09-17 20:26:06 +00:00
|
|
|
if page == "browse" {
|
2015-09-20 08:15:21 +00:00
|
|
|
code, err = browse.ServeHTTP(w, r, h.Config)
|
2015-09-17 20:26:06 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 08:47:02 +00:00
|
|
|
// Edit page
|
2015-09-17 20:26:06 +00:00
|
|
|
if page == "edit" {
|
2015-09-20 08:15:21 +00:00
|
|
|
code, err = editor.ServeHTTP(w, r, h.Config)
|
2015-09-17 20:26:06 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 08:47:02 +00:00
|
|
|
// Whenever the header "X-Refenerate" is true, the website should be
|
|
|
|
// regenerated. Used in edit and settings, for example.
|
2015-09-17 14:38:04 +00:00
|
|
|
if r.Header.Get("X-Regenerate") == "true" {
|
2015-09-20 21:00:25 +00:00
|
|
|
utils.RunHugo(h.Config)
|
2015-09-17 14:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2015-09-26 21:08:12 +00:00
|
|
|
|
|
|
|
func serveAssets(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
filename := strings.Replace(r.URL.Path, "/admin/", "", 1)
|
|
|
|
file, err := assets.Asset(filename)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 404, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the file extension ant its mime type
|
|
|
|
extension := filepath.Ext(filename)
|
|
|
|
mime := mime.TypeByExtension(extension)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|