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-30 19:39:07 +00:00
|
|
|
//go:generate go-bindata -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-30 19:35:50 +00:00
|
|
|
"bytes"
|
2016-06-29 19:39:29 +00:00
|
|
|
"io/ioutil"
|
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"
|
2016-06-30 19:35:50 +00:00
|
|
|
"reflect"
|
2016-06-21 15:35:42 +00:00
|
|
|
"strings"
|
2016-06-29 19:39:29 +00:00
|
|
|
"time"
|
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-30 19:35:50 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager/frontmatter"
|
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"
|
2016-06-29 19:39:29 +00:00
|
|
|
"github.com/robfig/cron"
|
2016-06-30 19:35:50 +00:00
|
|
|
"github.com/spf13/hugo/parser"
|
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"
|
|
|
|
}
|
|
|
|
|
2016-07-01 15:10:47 +00:00
|
|
|
http.Redirect(w, r, h.FileManager.Configs[0].AbsoluteURL+"/config."+frontmatter, http.StatusTemporaryRedirect)
|
2016-06-28 16:12:02 +00:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Method == http.MethodPost && r.Header.Get("archetype") != "" {
|
2016-07-05 16:54:54 +00:00
|
|
|
if !h.FileManager.Configs[0].CheckToken(r) {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
2016-06-29 13:57:40 +00:00
|
|
|
filename := r.Header.Get("Filename")
|
|
|
|
archetype := r.Header.Get("archetype")
|
|
|
|
|
|
|
|
if !strings.HasSuffix(filename, ".md") && !strings.HasSuffix(filename, ".markdown") {
|
|
|
|
return h.FileManager.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = strings.Replace(r.URL.Path, h.Config.BaseURL+"/content/", "", 1) + filename
|
|
|
|
filename = filepath.Clean(filename)
|
|
|
|
|
|
|
|
args := []string{"new", filename, "--kind", archetype}
|
|
|
|
|
|
|
|
if err := commands.Run(h.Config.Hugo, args, h.Config.Root); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, nil
|
2016-06-28 16:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if directory.CanBeEdited(r.URL.Path) && r.Method == http.MethodPut {
|
2016-07-05 16:54:54 +00:00
|
|
|
// NOTE: File Manager already checks the security token
|
2016-06-28 16:12:02 +00:00
|
|
|
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-30 19:35:50 +00:00
|
|
|
code, err = h.Schedule(w, r)
|
2016-06-28 16:12:02 +00:00
|
|
|
}
|
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 {
|
2016-07-02 22:29:47 +00:00
|
|
|
log.Println(err)
|
2016-06-28 13:59:33 +00:00
|
|
|
}
|
2016-06-21 15:35:42 +00:00
|
|
|
}
|
2016-06-22 17:27:12 +00:00
|
|
|
|
2016-06-29 09:46:55 +00:00
|
|
|
// Schedule schedules a post to be published later
|
2016-06-30 19:35:50 +00:00
|
|
|
func (h Hugo) Schedule(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
t, err := time.Parse("2006-01-02T15:04", r.Header.Get("Schedule"))
|
2016-06-29 19:39:29 +00:00
|
|
|
|
2016-06-30 19:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
2016-06-29 19:39:29 +00:00
|
|
|
|
|
|
|
scheduler := cron.New()
|
2016-06-30 19:35:50 +00:00
|
|
|
scheduler.AddFunc(t.Format("05 04 15 02 01 *"), func() {
|
2016-06-29 19:39:29 +00:00
|
|
|
filename := r.URL.Path
|
2016-06-30 19:35:50 +00:00
|
|
|
filename = strings.Replace(filename, h.Config.BaseURL, h.Config.Root, 1)
|
2016-06-29 19:39:29 +00:00
|
|
|
filename = filepath.Clean(filename)
|
|
|
|
|
|
|
|
raw, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-30 19:35:50 +00:00
|
|
|
buffer := bytes.NewBuffer(raw)
|
|
|
|
page, err := parser.ReadFrom(buffer)
|
2016-06-29 19:39:29 +00:00
|
|
|
|
2016-06-30 19:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2016-06-29 19:39:29 +00:00
|
|
|
|
2016-06-30 19:35:50 +00:00
|
|
|
content := strings.TrimSpace(string(page.Content()))
|
|
|
|
front, err := frontmatter.Unmarshal(page.FrontMatter())
|
2016-06-29 19:39:29 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2016-06-30 19:35:50 +00:00
|
|
|
log.Println(err)
|
2016-06-29 19:39:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-30 19:35:50 +00:00
|
|
|
kind := reflect.TypeOf(front)
|
|
|
|
|
|
|
|
if kind == reflect.TypeOf(map[interface{}]interface{}{}) {
|
|
|
|
delete(front.(map[interface{}]interface{}), "draft")
|
|
|
|
delete(front.(map[interface{}]interface{}), "Draft")
|
|
|
|
} else {
|
|
|
|
delete(front.(map[string]interface{}), "draft")
|
|
|
|
delete(front.(map[string]interface{}), "Draft")
|
|
|
|
}
|
|
|
|
|
|
|
|
fm, _, err := directory.ParseFrontMatter(front, h.FileManager.Configs[0].FrontMatter)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2016-06-29 19:39:29 +00:00
|
|
|
|
|
|
|
f := new(bytes.Buffer)
|
2016-06-30 19:35:50 +00:00
|
|
|
f.Write(fm)
|
|
|
|
f.Write([]byte(content))
|
2016-06-29 19:39:29 +00:00
|
|
|
file := f.Bytes()
|
|
|
|
|
|
|
|
if err = ioutil.WriteFile(filename, file, 0666); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-30 19:35:50 +00:00
|
|
|
RunHugo(h.Config, false)
|
|
|
|
})
|
|
|
|
scheduler.Start()
|
2016-06-29 19:39:29 +00:00
|
|
|
|
2016-06-29 09:46:55 +00:00
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|