2016-06-21 15:01:46 +00:00
|
|
|
//go:generate go get github.com/jteeuwen/go-bindata
|
|
|
|
//go:generate go install github.com/jteeuwen/go-bindata/go-bindata
|
2016-06-21 15:35:42 +00:00
|
|
|
//go:generate go-bindata -debug -pkg hugo -prefix "assets" -o binary.go assets/...
|
2016-06-21 15:01:46 +00:00
|
|
|
|
2016-02-14 10:14:28 +00:00
|
|
|
// Package hugo makes the bridge between the static website generator Hugo
|
|
|
|
// and the webserver Caddy, also providing an administrative user interface.
|
2015-10-16 18:34:27 +00:00
|
|
|
package hugo
|
2015-09-12 08:52:41 +00:00
|
|
|
|
|
|
|
import (
|
2016-06-28 13:59:33 +00:00
|
|
|
"log"
|
2016-06-22 17:27:12 +00:00
|
|
|
"mime"
|
2015-09-12 08:52:41 +00:00
|
|
|
"net/http"
|
2016-06-21 15:35:42 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2015-09-12 08:52:41 +00:00
|
|
|
|
2016-06-21 14:28:15 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager"
|
2016-06-28 16:12:02 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager/assets"
|
|
|
|
"github.com/hacdias/caddy-filemanager/directory"
|
2016-06-28 13:59:33 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager/utils/variables"
|
|
|
|
"github.com/hacdias/caddy-hugo/utils/commands"
|
2016-06-07 15:08:52 +00:00
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
2015-09-12 08:52:41 +00:00
|
|
|
)
|
|
|
|
|
2016-06-28 13:59:33 +00:00
|
|
|
// Hugo is hugo
|
2016-06-16 16:04:38 +00:00
|
|
|
type Hugo struct {
|
2016-06-21 14:28:15 +00:00
|
|
|
Next httpserver.Handler
|
|
|
|
Config *Config
|
2016-06-28 13:59:33 +00:00
|
|
|
FileManager *filemanager.FileManager
|
2015-09-20 08:15:21 +00:00
|
|
|
}
|
2015-09-12 08:52:41 +00:00
|
|
|
|
2016-06-28 16:12:02 +00:00
|
|
|
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
|
2016-06-16 16:04:38 +00:00
|
|
|
func (h Hugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
2016-06-28 16:12:02 +00:00
|
|
|
// If the site matches the baseURL
|
2016-06-21 15:35:42 +00:00
|
|
|
if httpserver.Path(r.URL.Path).Matches(h.Config.BaseURL) {
|
2016-06-28 16:12:02 +00:00
|
|
|
// Serve the hugo assets
|
2016-06-22 17:27:12 +00:00
|
|
|
if httpserver.Path(r.URL.Path).Matches(h.Config.BaseURL + AssetsURL) {
|
2016-06-28 16:12:02 +00:00
|
|
|
return serveAssets(w, r, h.Config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serve the filemanager assets
|
|
|
|
if httpserver.Path(r.URL.Path).Matches(h.Config.BaseURL + assets.BaseURL) {
|
|
|
|
return h.FileManager.ServeHTTP(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 == h.Config.BaseURL+"/settings/" || r.URL.Path == h.Config.BaseURL+"/settings" {
|
|
|
|
var frontmatter string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if _, err = os.Stat(h.Config.Root + "config.yaml"); err == nil {
|
|
|
|
frontmatter = "yaml"
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = os.Stat(h.Config.Root + "config.json"); err == nil {
|
|
|
|
frontmatter = "json"
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = os.Stat(h.Config.Root + "config.toml"); err == nil {
|
|
|
|
frontmatter = "toml"
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, h.Config.BaseURL+"/config."+frontmatter, http.StatusTemporaryRedirect)
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Method == http.MethodPost && r.Header.Get("archetype") != "" {
|
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if directory.CanBeEdited(r.URL.Path) && r.Method == http.MethodPut {
|
|
|
|
code, err := h.FileManager.ServeHTTP(w, r)
|
|
|
|
|
2016-06-28 21:20:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
2016-06-28 16:12:02 +00:00
|
|
|
if r.Header.Get("Regenerate") == "true" {
|
2016-06-28 21:20:42 +00:00
|
|
|
RunHugo(h.Config, false)
|
2016-06-28 16:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.Header.Get("Schedule") != "" {
|
|
|
|
|
|
|
|
}
|
2016-06-21 15:35:42 +00:00
|
|
|
|
2016-06-28 16:12:02 +00:00
|
|
|
return code, err
|
2016-06-21 15:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return h.FileManager.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2016-06-28 13:59:33 +00:00
|
|
|
return h.Next.ServeHTTP(w, r)
|
2016-06-21 15:35:42 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 13:59:33 +00:00
|
|
|
// RunHugo is used to run the static website generator
|
|
|
|
func RunHugo(c *Config, force bool) {
|
|
|
|
os.RemoveAll(c.Root + "public")
|
2016-06-21 15:35:42 +00:00
|
|
|
|
2016-06-28 13:59:33 +00:00
|
|
|
// Prevent running if watching is enabled
|
|
|
|
if b, pos := variables.StringInSlice("--watch", c.Args); b && !force {
|
|
|
|
if len(c.Args) > pos && c.Args[pos+1] != "false" {
|
|
|
|
return
|
|
|
|
}
|
2016-06-21 15:35:42 +00:00
|
|
|
|
2016-06-28 13:59:33 +00:00
|
|
|
if len(c.Args) == pos+1 {
|
|
|
|
return
|
2016-06-21 15:35:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-28 13:59:33 +00:00
|
|
|
if err := commands.Run(c.Hugo, c.Args, c.Root); err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2016-06-21 15:35:42 +00:00
|
|
|
}
|
2016-06-22 17:27:12 +00:00
|
|
|
|
2016-06-28 13:59:33 +00:00
|
|
|
// serveAssets provides the needed assets for the front-end
|
|
|
|
func serveAssets(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
|
2016-06-22 17:27:12 +00:00
|
|
|
// gets the filename to be used with Assets function
|
2016-06-28 13:59:33 +00:00
|
|
|
filename := strings.Replace(r.URL.Path, c.BaseURL+AssetsURL, "public", 1)
|
2016-06-22 17:27:12 +00:00
|
|
|
file, err := Asset(filename)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusNotFound, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the file extension and its mimetype
|
|
|
|
extension := filepath.Ext(filename)
|
|
|
|
mediatype := mime.TypeByExtension(extension)
|
|
|
|
|
|
|
|
// Write the header with the Content-Type and write the file
|
|
|
|
// content to the buffer
|
|
|
|
w.Header().Set("Content-Type", mediatype)
|
|
|
|
w.Write(file)
|
|
|
|
return 200, nil
|
|
|
|
}
|