filebrowser/hugo.go

210 lines
5.4 KiB
Go
Raw Normal View History

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
// Package hugo makes the bridge between the static website generator Hugo
// and the webserver Caddy, also providing an administrative user interface.
package hugo
2015-09-12 08:52:41 +00:00
import (
"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"
"strings"
"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-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"
"github.com/robfig/cron"
"github.com/spf13/cast"
2015-09-12 08:52:41 +00:00
)
2016-06-28 13:59:33 +00:00
// Hugo is hugo
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.
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") != "" {
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 {
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-29 09:46:55 +00:00
code, err = Schedule(w, r, h.Config)
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 {
log.Panic(err)
}
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
func Schedule(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
// TODO: this
t := cast.ToTime(r.Header.Get("Date"))
scheduler := cron.New()
scheduler.AddFunc(t.In(time.Now().Location()).Format("05 04 15 02 01 *"), func() {
filename := r.URL.Path
filename = strings.Replace(filename, c.BaseURL, c.Root, 1)
filename = filepath.Clean(filename)
raw, err := ioutil.ReadFile(filename)
if err != nil {
log.Println(err)
return
}
})
scheduler.Start()
/*
// Set draft to false
data.Content["draft"] = false
// Converts the frontmatter in JSON
jsonFrontmatter, err := json.Marshal(data.Content)
if err != nil {
return
}
// Indents the json
frontMatterBuffer := new(bytes.Buffer)
json.Indent(frontMatterBuffer, jsonFrontmatter, "", " ")
// Generates the final file
f := new(bytes.Buffer)
f.Write(frontMatterBuffer.Bytes())
f.Write([]byte(mainContent))
file := f.Bytes()
// Write the file
if err = ioutil.WriteFile(filename, file, 0666); err != nil {
return
}
go hugo.Run(c, false)
*/
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
}