2015-09-26 20:03:34 +00:00
|
|
|
//go:generate go get github.com/jteeuwen/go-bindata
|
2015-09-27 08:43:36 +00:00
|
|
|
//go:generate go install github.com/jteeuwen/go-bindata/go-bindata
|
2016-06-16 16:08:10 +00:00
|
|
|
//go:generate go-bindata -prefix assets/dist -pkg assets -o routes/assets/assets.go assets/dist/...
|
2015-09-13 11:14:18 +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 (
|
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-10-18 14:10:32 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/config"
|
2016-03-06 20:37:49 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/routes/assets"
|
2016-03-06 19:18:46 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/routes/browse"
|
|
|
|
"github.com/hacdias/caddy-hugo/routes/editor"
|
2016-03-12 13:03:31 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/routes/errors"
|
2016-03-06 19:18:46 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/routes/git"
|
2016-03-06 20:37:49 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/tools/server"
|
2016-06-07 15:08:52 +00:00
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
2015-09-12 08:52:41 +00:00
|
|
|
)
|
|
|
|
|
2016-06-16 16:04:38 +00:00
|
|
|
// Hugo contais the next middleware to be run and the configuration
|
2016-02-14 10:14:28 +00:00
|
|
|
// of the current one.
|
2016-06-16 16:04:38 +00:00
|
|
|
type Hugo struct {
|
2016-06-07 15:08:52 +00:00
|
|
|
Next httpserver.Handler
|
2015-09-20 08:15:21 +00:00
|
|
|
Config *config.Config
|
|
|
|
}
|
2015-09-12 08:52:41 +00:00
|
|
|
|
2016-02-14 10:14:28 +00:00
|
|
|
// ServeHTTP is the main function of the whole plugin that routes every single
|
|
|
|
// request to its function.
|
2016-06-16 16:04:38 +00:00
|
|
|
func (h Hugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
2016-06-07 06:54:10 +00:00
|
|
|
// Only handle /{admin} path
|
2016-06-07 15:08:52 +00:00
|
|
|
if httpserver.Path(r.URL.Path).Matches(h.Config.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"
|
2016-03-06 20:37:49 +00:00
|
|
|
if len(server.ParseURLComponents(r)) > 1 {
|
|
|
|
page = server.ParseURLComponents(r)[1]
|
2015-09-18 08:47:02 +00:00
|
|
|
} else {
|
2016-03-06 20:37:49 +00:00
|
|
|
page = server.ParseURLComponents(r)[0]
|
2015-09-18 08:47:02 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 06:54:10 +00:00
|
|
|
// If the current page is only "/{admin}/", redirect to "/{admin}/browse/content/"
|
|
|
|
if r.URL.Path == h.Config.Admin+"/" {
|
|
|
|
http.Redirect(w, r, h.Config.Admin+"/browse/content/", http.StatusTemporaryRedirect)
|
2015-09-18 08:47:02 +00:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2016-06-07 06:54:10 +00:00
|
|
|
// If the url matches exactly with /{admin}/settings/ serve that page
|
2015-09-18 08:47:02 +00:00
|
|
|
// page variable isn't used here to avoid people using URLs like
|
2016-06-07 06:54:10 +00:00
|
|
|
// "/{admin}/settings/something".
|
|
|
|
if r.URL.Path == h.Config.Admin+"/settings/" {
|
2015-09-18 15:49:53 +00:00
|
|
|
var frontmatter string
|
|
|
|
|
2016-06-07 15:08:52 +00:00
|
|
|
if _, err = os.Stat(h.Config.Path + "config.yaml"); err == nil {
|
2015-09-18 15:49:53 +00:00
|
|
|
frontmatter = "yaml"
|
|
|
|
}
|
|
|
|
|
2016-06-07 15:08:52 +00:00
|
|
|
if _, err = os.Stat(h.Config.Path + "config.json"); err == nil {
|
2015-09-18 15:49:53 +00:00
|
|
|
frontmatter = "json"
|
|
|
|
}
|
|
|
|
|
2016-06-07 15:08:52 +00:00
|
|
|
if _, err = os.Stat(h.Config.Path + "config.toml"); err == nil {
|
2015-09-18 15:49:53 +00:00
|
|
|
frontmatter = "toml"
|
|
|
|
}
|
|
|
|
|
2016-06-07 06:54:10 +00:00
|
|
|
http.Redirect(w, r, h.Config.Admin+"/edit/config."+frontmatter, http.StatusTemporaryRedirect)
|
2015-09-18 15:49:53 +00:00
|
|
|
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" {
|
2016-06-07 06:54:10 +00:00
|
|
|
code, err = serveAssets(w, r, h.Config)
|
2015-09-26 21:08:12 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-03-06 13:14:05 +00:00
|
|
|
// Git API
|
2016-03-06 14:23:37 +00:00
|
|
|
if page == "git" {
|
2016-03-06 13:14:05 +00:00
|
|
|
code, err = git.ServeHTTP(w, r, h.Config)
|
|
|
|
}
|
|
|
|
|
2016-03-12 13:03:31 +00:00
|
|
|
if code != 0 && code != 200 {
|
|
|
|
code, err = errors.ServeHTTP(w, r, code, err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-06-07 06:54:10 +00:00
|
|
|
// serveAssets handles the /{admin}/assets requests
|
|
|
|
func serveAssets(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
|
|
|
filename := strings.Replace(r.URL.Path, c.Admin+"/assets", "public", 1)
|
2015-09-26 21:08:12 +00:00
|
|
|
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
|
|
|
|
}
|