You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
214 lines
4.8 KiB
214 lines
4.8 KiB
package caddyhugo
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"html/template"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/gorilla/websocket"
|
|
"github.com/mholt/caddy"
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
)
|
|
|
|
func init() {
|
|
plugin := CaddyHugo{}
|
|
|
|
// register a "generic" plugin, like a directive or middleware
|
|
caddy.RegisterPlugin("hugo", caddy.Plugin{
|
|
ServerType: "http",
|
|
Action: plugin.Setup,
|
|
})
|
|
|
|
// ... there are others. See the godoc.
|
|
}
|
|
|
|
type CaddyHugo struct {
|
|
ServerType string
|
|
Site *httpserver.SiteConfig
|
|
|
|
authorTmpl, adminTmpl, editTmpl *template.Template
|
|
}
|
|
|
|
func (ch CaddyHugo) Setup(c *caddy.Controller) error {
|
|
|
|
ch.Site = httpserver.GetConfig(c)
|
|
|
|
var err error
|
|
|
|
ch.authorTmpl, err = template.New("").Parse(AuthorPage)
|
|
if err != nil {
|
|
return fmt.Errorf("author template invalid: %v", err)
|
|
}
|
|
|
|
ch.adminTmpl, err = template.New("").Parse(AdminPage)
|
|
if err != nil {
|
|
return fmt.Errorf("admin template invalid: %v", err)
|
|
}
|
|
|
|
ch.editTmpl, err = template.New("").Parse(EditPage)
|
|
if err != nil {
|
|
return fmt.Errorf("edit template invalid: %v", err)
|
|
}
|
|
|
|
// add a function that wraps listeners for the HTTP server
|
|
// (it's more common for a directive to call this rather than a standalone plugin)
|
|
ch.Site.AddMiddleware(ch.Middleware(c))
|
|
|
|
return nil
|
|
}
|
|
|
|
func (ch CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware {
|
|
return func(h httpserver.Handler) httpserver.Handler {
|
|
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
if !ch.Match(r) {
|
|
return h.ServeHTTP(w, r)
|
|
}
|
|
|
|
if !ch.Auth(r) {
|
|
return http.StatusUnauthorized, errors.New("not authorized")
|
|
}
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/hugo/admin") {
|
|
return ch.Admin().ServeHTTP(w, r)
|
|
}
|
|
if strings.HasPrefix(r.URL.Path, "/hugo/author") {
|
|
return ch.AuthorHome().ServeHTTP(w, r)
|
|
}
|
|
if strings.HasPrefix(r.URL.Path, "/hugo/edit/") {
|
|
return ch.Edit(c).ServeHTTP(w, r)
|
|
}
|
|
|
|
return http.StatusNotFound, errors.New("not found")
|
|
})
|
|
}
|
|
}
|
|
|
|
func (ch CaddyHugo) Auth(r *http.Request) bool {
|
|
return true
|
|
}
|
|
|
|
func (ch CaddyHugo) Match(r *http.Request) bool {
|
|
if r.URL.Path == "/hugo" {
|
|
return true
|
|
}
|
|
|
|
return strings.HasPrefix(r.URL.Path, "/hugo/")
|
|
}
|
|
|
|
func (ch CaddyHugo) BasePath() string {
|
|
return "/hugo"
|
|
}
|
|
|
|
func (ch CaddyHugo) Admin() httpserver.Handler {
|
|
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
err := ch.adminTmpl.Execute(w, ch.TmplData(r))
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
return http.StatusOK, nil
|
|
|
|
})
|
|
}
|
|
|
|
func (ch CaddyHugo) AuthorHome() httpserver.Handler {
|
|
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
err := ch.authorTmpl.Execute(w, ch.TmplData(r))
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
return http.StatusOK, nil
|
|
})
|
|
}
|
|
|
|
func (ch CaddyHugo) Edit(c *caddy.Controller) httpserver.Handler {
|
|
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
if r.URL.Path == "/hugo/edit/new" {
|
|
return ch.NewContent(w, r)
|
|
}
|
|
if r.URL.Path == "/hugo/edit/websocket" {
|
|
return ch.DeltaWebsocket(w, r)
|
|
}
|
|
|
|
err := ch.editTmpl.Execute(w, ch.TmplData(r))
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
return http.StatusOK, nil
|
|
})
|
|
}
|
|
|
|
func (ch CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
var upgrader = websocket.Upgrader{
|
|
ReadBufferSize: 1024,
|
|
WriteBufferSize: 1024,
|
|
}
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return http.StatusBadRequest, err
|
|
}
|
|
|
|
for {
|
|
messageType, p, err := conn.ReadMessage()
|
|
if err != nil {
|
|
return http.StatusBadRequest, err
|
|
}
|
|
|
|
fmt.Println(messageType, string(p))
|
|
}
|
|
}
|
|
|
|
func (ch CaddyHugo) NewContent(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
name := r.FormValue("name")
|
|
ctype := r.FormValue("type")
|
|
|
|
if filepath.Ext(name) != ".md" {
|
|
name += ".md"
|
|
}
|
|
|
|
filename := path.Join(ch.Site.Root, "content", ctype, name)
|
|
|
|
dir := filepath.Dir(filename)
|
|
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
|
|
err = os.MkdirAll(dir, 0755)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
}
|
|
|
|
// create content
|
|
f, err := os.Create(filename)
|
|
if err != nil && !os.IsExist(err) {
|
|
fmt.Println(err)
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
// we only needed to make the file though
|
|
err = f.Close()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return http.StatusInternalServerError, err
|
|
}
|
|
|
|
// serve redirect
|
|
http.Redirect(w, r, filepath.Join("/hugo/edit/", filename), http.StatusFound)
|
|
return http.StatusFound, nil
|
|
}
|
|
|
|
func (ch CaddyHugo) TmplData(r *http.Request) interface{} {
|
|
return tmplData{ch.Site, r, ch}
|
|
}
|
|
|