committing changes from who knows when

master
Stephen Searles 2 years ago
parent 548075696c
commit 51c4837aaf
  1. 77
      caddyhugo.go
  2. 12
      deltas.go
  3. 10
      go.mod
  4. 1073
      go.sum
  5. 103
      http.go
  6. 33
      media.go
  7. 23
      setup.go
  8. 4
      templates.go
  9. 17
      testdir/caddyfile

@ -2,7 +2,9 @@ package caddyhugo
import ( import (
"html/template" "html/template"
"log"
"net/http" "net/http"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
@ -11,24 +13,74 @@ import (
"git.stephensearles.com/stephen/caddy-hugo2/comments" "git.stephensearles.com/stephen/caddy-hugo2/comments"
"git.stephensearles.com/stephen/caddy-hugo2/media" "git.stephensearles.com/stephen/caddy-hugo2/media"
"github.com/caddyserver/caddy" "github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/caddyhttp/httpserver" "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/hugolib" "github.com/gohugoio/hugo/hugolib"
"github.com/spf13/afero" "github.com/spf13/afero"
) )
func init() { func init() {
caddy.RegisterPlugin("hugo", caddy.Plugin{ caddy.RegisterModule(&CaddyHugo{})
ServerType: "http", httpcaddyfile.RegisterHandlerDirective("hugo", parseCaddyfile)
Action: SetupCaddy, }
type SiteConfig struct {
Hosts []string
Root string
CommentsEnabled bool
CommentsPassword string
}
func (m *CaddyHugo) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.hugo",
New: func() caddy.Module { return new(CaddyHugo) },
}
}
func (m *CaddyHugo) Provision(ctx caddy.Context) error {
if m.Site.CommentsEnabled {
m.Comments = comments.WithStorage(comments.NewDiskv(path.Join(m.Site.Root, "comments")))
}
root, err := filepath.Abs(m.Site.Root)
if err != nil {
return err
}
log.Println("SETTING UP")
err = m.Setup(root)
log.Println("SET UP; err:", err)
if err != nil {
return err
}
ctx.OnCancel(func() {
m.persistAllEdits()
}) })
m.commentsSetting()
return nil
}
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
func (s *SiteConfig) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.NextBlock(0) {
if d.Val() == "comments" {
if d.NextArg() {
s.CommentsPassword = d.Val()
}
}
}
return nil
} }
// CaddyHugo implements the plugin // CaddyHugo implements the plugin for a single site
type CaddyHugo struct { type CaddyHugo struct {
ServerType string ServerType string
Site *httpserver.SiteConfig Site SiteConfig
HugoSites *hugolib.HugoSites HugoSites *hugolib.HugoSites
HugoCfg *deps.DepsCfg HugoCfg *deps.DepsCfg
@ -78,3 +130,14 @@ func (ch *CaddyHugo) TmplData(r *http.Request, docref *editSession) interface{}
} }
return &tmplData{ch.Site, r, ch, doc, docref} return &tmplData{ch.Site, r, ch, doc, docref}
} }
// parseCaddyfile unmarshals tokens from h into a new Middleware.
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var m CaddyHugo
err := m.Site.UnmarshalCaddyfile(h.Dispenser)
return &m, err
}
var (
_ caddy.Provisioner = (*CaddyHugo)(nil)
)

@ -90,7 +90,7 @@ func (ch *CaddyHugo) ClearConfirmed(lowestPending uint64) {
} }
} }
func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int, error) { func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) error {
var upgrader = websocket.Upgrader{ var upgrader = websocket.Upgrader{
ReadBufferSize: 1024, ReadBufferSize: 1024,
WriteBufferSize: 1024, WriteBufferSize: 1024,
@ -99,7 +99,7 @@ func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int
conn, err := upgrader.Upgrade(w, r, nil) conn, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return http.StatusBadRequest, err return err
} }
conn.SetReadDeadline(time.Time{}) conn.SetReadDeadline(time.Time{})
@ -107,7 +107,7 @@ func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int
doc, err := ch.editSession(docNameFromEditRequest(r)) doc, err := ch.editSession(docNameFromEditRequest(r))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return http.StatusBadRequest, err return err
} }
return ch.handleDeltaConn(conn, doc) return ch.handleDeltaConn(conn, doc)
@ -122,7 +122,7 @@ func (ch *CaddyHugo) Message(deltas ...acedoc.Delta) Message {
} }
} }
func (ch *CaddyHugo) handleDeltaConn(conn DeltaConn, doc *editSession) (int, error) { func (ch *CaddyHugo) handleDeltaConn(conn DeltaConn, doc *editSession) error {
const idlePing = 15 * time.Second const idlePing = 15 * time.Second
const idlePingShort = 1 * time.Millisecond const idlePingShort = 1 * time.Millisecond
@ -210,7 +210,7 @@ func (ch *CaddyHugo) handleDeltaConn(conn DeltaConn, doc *editSession) (int, err
select { select {
case err := <-errCh: case err := <-errCh:
fmt.Println("error handling websocket connection:", err) fmt.Println("error handling websocket connection:", err)
return 500, err return err
default: default:
} }
@ -223,7 +223,7 @@ func (ch *CaddyHugo) handleDeltaConn(conn DeltaConn, doc *editSession) (int, err
case <-wroteMessagesCh: case <-wroteMessagesCh:
resetTimer(idlePing) resetTimer(idlePing)
case <-doneCh: case <-doneCh:
return 200, nil return nil
} }
} }
} }

@ -6,13 +6,17 @@ require (
git.stephensearles.com/stephen/acedoc v0.0.0-20170928122432-96da2793a59d git.stephensearles.com/stephen/acedoc v0.0.0-20170928122432-96da2793a59d
git.stephensearles.com/stephen/idleshut v0.0.0-20180107224249-cde7779f51c8 git.stephensearles.com/stephen/idleshut v0.0.0-20180107224249-cde7779f51c8
github.com/PuerkitoBio/goquery v1.5.0 github.com/PuerkitoBio/goquery v1.5.0
github.com/caddyserver/caddy v1.0.3 github.com/caddyserver/caddy v1.0.5
github.com/caddyserver/caddy/v2 v2.1.1
github.com/gobuffalo/envy v1.7.0 // indirect github.com/gobuffalo/envy v1.7.0 // indirect
github.com/gohugoio/hugo v0.58.1 github.com/gohugoio/hugo v0.74.4-0.20200822075643-d39636a5fc6b
github.com/gorilla/websocket v1.4.1 github.com/gorilla/websocket v1.4.1
github.com/lucas-clemente/quic-go v0.18.0 // indirect
github.com/miekg/dns v1.1.30 // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/peterbourgon/diskv v2.0.1+incompatible github.com/peterbourgon/diskv v2.0.1+incompatible
github.com/spf13/afero v1.2.2 github.com/spf13/afero v1.2.2
github.com/spf13/viper v1.4.0 github.com/spf13/viper v1.6.1
github.com/tajtiattila/metadata v0.0.0-20180130123038-1ef25f4c37ea github.com/tajtiattila/metadata v0.0.0-20180130123038-1ef25f4c37ea
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de // indirect
) )

1073
go.sum

File diff suppressed because it is too large Load Diff

@ -2,9 +2,9 @@ package caddyhugo
import ( import (
"encoding/base64" "encoding/base64"
"errors"
"fmt" "fmt"
"io" "io"
"net"
"net/http" "net/http"
"os" "os"
"path" "path"
@ -12,26 +12,25 @@ import (
"strings" "strings"
"git.stephensearles.com/stephen/caddy-hugo2/assets" "git.stephensearles.com/stephen/caddy-hugo2/assets"
"github.com/caddyserver/caddy" "github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
"github.com/spf13/afero" "github.com/spf13/afero"
) )
func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, w http.ResponseWriter, r *http.Request) (int, error) { func (ch *CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
if !ch.Match(r) { if !ch.Match(r) {
p := path.Join(ch.Dir, "public", r.URL.Path) p := path.Join(ch.Dir, "public", r.URL.Path)
http.ServeFile(w, r, p) http.ServeFile(w, r, p)
return 200, nil return nil
} }
if ch.Comments != nil && strings.HasSuffix(r.URL.Path, "/comments") { if ch.Comments != nil && strings.HasSuffix(r.URL.Path, "/comments") {
docName := docNameFromCommentRequest(r) docName := docNameFromCommentRequest(r)
err := ch.Comments.ServeComments(docName, w, r) err := ch.Comments.ServeComments(docName, w, r)
if err != nil { if err != nil {
return 500, fmt.Errorf("couldn't load comments:", err) return fmt.Errorf("couldn't load comments:", err)
} }
return 200, nil return nil
} }
if r.URL.Path == "/login" { if r.URL.Path == "/login" {
@ -41,32 +40,31 @@ func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, w http.ResponseW
if strings.HasPrefix(r.URL.Path, "/hugo/publish") { if strings.HasPrefix(r.URL.Path, "/hugo/publish") {
err := ch.Publish() err := ch.Publish()
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) return err
return http.StatusInternalServerError, nil
} }
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
return http.StatusFound, nil return nil
} }
if strings.HasPrefix(r.URL.Path, "/hugo/simplemde.css") { if strings.HasPrefix(r.URL.Path, "/hugo/simplemde.css") {
w.Write(assets.MustAsset("simplemde/dist/simplemde.min.css")) w.Write(assets.MustAsset("simplemde/dist/simplemde.min.css"))
return http.StatusOK, nil return nil
} }
if strings.HasPrefix(r.URL.Path, "/hugo/simplemde.js") { if strings.HasPrefix(r.URL.Path, "/hugo/simplemde.js") {
w.Write(assets.MustAsset("simplemde/debug/simplemde.js")) w.Write(assets.MustAsset("simplemde/debug/simplemde.js"))
return http.StatusOK, nil return nil
} }
if strings.HasPrefix(r.URL.Path, "/hugo/vue.js") { if strings.HasPrefix(r.URL.Path, "/hugo/vue.js") {
w.Write(assets.MustAsset("js/vue.js")) w.Write(assets.MustAsset("js/vue.js"))
return http.StatusOK, nil return nil
} }
if strings.HasPrefix(r.URL.Path, "/hugo/moment.js") { if strings.HasPrefix(r.URL.Path, "/hugo/moment.js") {
w.Write(assets.MustAsset("js/moment.js")) w.Write(assets.MustAsset("js/moment.js"))
return http.StatusOK, nil return nil
} }
if strings.HasPrefix(r.URL.Path, "/hugo/font-awesome.css") { if strings.HasPrefix(r.URL.Path, "/hugo/font-awesome.css") {
w.Write(assets.MustAsset("css/font-awesome.min.css")) w.Write(assets.MustAsset("css/font-awesome.min.css"))
return http.StatusOK, nil return nil
} }
if strings.HasPrefix(r.URL.Path, "/hugo/admin") { if strings.HasPrefix(r.URL.Path, "/hugo/admin") {
return ch.Admin().ServeHTTP(w, r) return ch.Admin().ServeHTTP(w, r)
@ -91,38 +89,25 @@ func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, w http.ResponseW
} }
if strings.HasPrefix(r.URL.Path, "/hugo/fs/") { if strings.HasPrefix(r.URL.Path, "/hugo/fs/") {
printTree(afero.NewOsFs(), w, ch.Dir) printTree(afero.NewOsFs(), w, ch.Dir)
return 200, nil return nil
} }
return next.ServeHTTP(w, r) return next.ServeHTTP(w, r)
} }
func (ch *CaddyHugo) ServeNewContent(w http.ResponseWriter, r *http.Request) (int, error) { func (ch *CaddyHugo) ServeNewContent(w http.ResponseWriter, r *http.Request) error {
name := r.FormValue("name") name := r.FormValue("name")
ctype := r.FormValue("type") ctype := r.FormValue("type")
filename, err := ch.NewContent(name, ctype) filename, err := ch.NewContent(name, ctype)
if err != nil { if err != nil {
fmt.Println("error creating new content:", err) fmt.Println("error creating new content:", err)
return http.StatusInternalServerError, err return err
} }
// serve redirect // serve redirect
http.Redirect(w, r, filepath.Join("/hugo/edit/", "content", filename), http.StatusFound) http.Redirect(w, r, filepath.Join("/hugo/edit/", "content", filename), http.StatusFound)
return http.StatusFound, nil return nil
}
func (ch *CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware {
return func(next httpserver.Handler) httpserver.Handler {
host := ch.Site.Addr.Host
hostport := net.JoinHostPort(ch.Site.Addr.Host, ch.Site.Addr.Port)
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
if r.Host != host && r.Host != hostport {
return next.ServeHTTP(w, r)
}
return ch.ServeHTTPWithNext(next, w, r)
})
}
} }
func (ch *CaddyHugo) Auth(r *http.Request) bool { func (ch *CaddyHugo) Auth(r *http.Request) bool {
@ -131,11 +116,6 @@ func (ch *CaddyHugo) Auth(r *http.Request) bool {
} }
func (ch *CaddyHugo) Match(r *http.Request) bool { func (ch *CaddyHugo) Match(r *http.Request) bool {
host := ch.Site.Addr.Host
hostport := net.JoinHostPort(ch.Site.Addr.Host, ch.Site.Addr.Port)
if r.Host != host && r.Host != hostport {
return false
}
if strings.HasPrefix(r.URL.Path, "/media/") { if strings.HasPrefix(r.URL.Path, "/media/") {
return true return true
} }
@ -155,34 +135,34 @@ func (ch *CaddyHugo) Match(r *http.Request) bool {
return strings.HasPrefix(r.URL.Path, "/hugo/") return strings.HasPrefix(r.URL.Path, "/hugo/")
} }
func (ch *CaddyHugo) Admin() httpserver.Handler { func (ch *CaddyHugo) Admin() caddyhttp.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
err := ch.adminTmpl.Execute(w, ch.TmplData(r, nil)) err := ch.adminTmpl.Execute(w, ch.TmplData(r, nil))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return http.StatusInternalServerError, err return err
} }
return http.StatusOK, nil return nil
}) })
} }
func (ch *CaddyHugo) AuthorHome() httpserver.Handler { func (ch *CaddyHugo) AuthorHome() caddyhttp.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
td := ch.TmplData(r, nil) td := ch.TmplData(r, nil)
err := ch.authorTmpl.Execute(w, td) err := ch.authorTmpl.Execute(w, td)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return http.StatusInternalServerError, err return err
} }
return http.StatusOK, nil return nil
}) })
} }
func (ch *CaddyHugo) Edit() httpserver.Handler { func (ch *CaddyHugo) Edit() caddyhttp.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
if r.URL.Path == "/hugo/edit/new" { if r.URL.Path == "/hugo/edit/new" {
return ch.ServeNewContent(w, r) return ch.ServeNewContent(w, r)
} }
@ -194,32 +174,31 @@ func (ch *CaddyHugo) Edit() httpserver.Handler {
doc, err := ch.editSession(docNameFromEditRequest(r)) doc, err := ch.editSession(docNameFromEditRequest(r))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return http.StatusNotFound, err http.Error(w, err.Error(), http.StatusNotFound)
return err
} }
err = ch.editTmpl.Execute(w, ch.TmplData(r, doc)) err = ch.editTmpl.Execute(w, ch.TmplData(r, doc))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return http.StatusInternalServerError, err return err
} }
return http.StatusOK, nil return nil
}) })
} }
func (ch *CaddyHugo) serveDraft(w http.ResponseWriter, r *http.Request) (int, error) { func (ch *CaddyHugo) serveDraft(w http.ResponseWriter, r *http.Request) error {
pathSegments := strings.SplitN(r.URL.Path, "/", 5) pathSegments := strings.SplitN(r.URL.Path, "/", 5)
if len(pathSegments) < 4 { if len(pathSegments) < 4 {
return errors.New("not found")
return http.StatusNotFound, nil
} }
encoded := pathSegments[3] encoded := pathSegments[3]
nameBytes, err := base64.RawURLEncoding.DecodeString(encoded) nameBytes, err := base64.RawURLEncoding.DecodeString(encoded)
if err != nil { if err != nil {
return http.StatusNotFound, err return errors.New("not found")
} }
name := string(nameBytes) name := string(nameBytes)
@ -229,7 +208,7 @@ func (ch *CaddyHugo) serveDraft(w http.ResponseWriter, r *http.Request) (int, er
docref, ok := ch.docs[ch.docFilename(name)] docref, ok := ch.docs[ch.docFilename(name)]
if !ok { if !ok {
return http.StatusNotFound, fmt.Errorf("draft not found") return fmt.Errorf("draft not found")
} }
r.URL.Path = strings.ToLower(r.URL.Path) r.URL.Path = strings.ToLower(r.URL.Path)
@ -240,13 +219,13 @@ func (ch *CaddyHugo) serveDraft(w http.ResponseWriter, r *http.Request) (int, er
page := ch.HugoSites.GetContentPage(ch.docFilename(name)) page := ch.HugoSites.GetContentPage(ch.docFilename(name))
if page == nil { if page == nil {
fmt.Fprintf(w, "can't find %q to display a draft", name) fmt.Fprintf(w, "can't find %q to display a draft", name)
return 404, nil return fmt.Errorf("draft not found")
} }
r.URL.Path = page.RelPermalink() r.URL.Path = page.RelPermalink()
http.FileServer(aferoHTTP{afero.NewBasePathFs(docref.tmpfs, path.Join(ch.Dir, "public"))}).ServeHTTP(w, r) http.FileServer(aferoHTTP{afero.NewBasePathFs(docref.tmpfs, path.Join(ch.Dir, "public"))}).ServeHTTP(w, r)
return 200, nil return nil
} }
func printTree(fs afero.Fs, w io.Writer, dir string) { func printTree(fs afero.Fs, w io.Writer, dir string) {
@ -328,9 +307,9 @@ func (a aferoHTTP) Open(name string) (http.File, error) {
return af, err return af, err
} }
func (ch *CaddyHugo) commentsLogin(r *http.Request, w http.ResponseWriter) (int, error) { func (ch *CaddyHugo) commentsLogin(r *http.Request, w http.ResponseWriter) error {
if ch.Comments == nil { if ch.Comments == nil {
return 200, nil return nil
} }
_, ok := ch.Comments.User(r) _, ok := ch.Comments.User(r)
@ -338,10 +317,10 @@ func (ch *CaddyHugo) commentsLogin(r *http.Request, w http.ResponseWriter) (int,
w.Header().Set("WWW-Authenticate", `Basic realm="Log in with your name and the password. Ask Dan or Stephen for the password."`) w.Header().Set("WWW-Authenticate", `Basic realm="Log in with your name and the password. Ask Dan or Stephen for the password."`)
w.WriteHeader(401) w.WriteHeader(401)
fmt.Fprintf(w, "<html><body>Log in with your name and the password. Ask Dan or Stephen for the password. <a href=%q>go back</a></body></html>", r.Referer()) fmt.Fprintf(w, "<html><body>Log in with your name and the password. Ask Dan or Stephen for the password. <a href=%q>go back</a></body></html>", r.Referer())
return 200, nil return nil
} }
http.Redirect(w, r, r.Referer(), http.StatusFound) http.Redirect(w, r, r.Referer(), http.StatusFound)
return 200, nil return nil
} }

@ -18,7 +18,12 @@ func (ch *CaddyHugo) ReferencedMedia() map[string]map[page.Page]struct{} {
found := map[string]map[page.Page]struct{}{} found := map[string]map[page.Page]struct{}{}
for _, pg := range ch.HugoSites.Pages() { for _, pg := range ch.HugoSites.Pages() {
r := bytes.NewBufferString(string(pg.Render())) renderOutput, err := pg.Render()
if err != nil {
continue
}
r := bytes.NewBufferString(string(renderOutput))
doc, err := goquery.NewDocumentFromReader(r) doc, err := goquery.NewDocumentFromReader(r)
if err != nil { if err != nil {
continue continue
@ -43,16 +48,16 @@ func (ch *CaddyHugo) ReferencedMedia() map[string]map[page.Page]struct{} {
return found return found
} }
func (ch *CaddyHugo) uploadMedia(w http.ResponseWriter, r *http.Request) (int, error) { func (ch *CaddyHugo) uploadMedia(w http.ResponseWriter, r *http.Request) error {
if ch.Media == nil { if ch.Media == nil {
http.NotFound(w, r) http.NotFound(w, r)
return 404, nil return nil
} }
mr, err := r.MultipartReader() mr, err := r.MultipartReader()
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return 400, nil return nil
} }
for { for {
@ -62,7 +67,7 @@ func (ch *CaddyHugo) uploadMedia(w http.ResponseWriter, r *http.Request) (int, e
} }
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return 400, nil return err
} }
name := part.FileName() name := part.FileName()
@ -70,18 +75,18 @@ func (ch *CaddyHugo) uploadMedia(w http.ResponseWriter, r *http.Request) (int, e
err = ch.Media.ReceiveNewMedia(name, part) err = ch.Media.ReceiveNewMedia(name, part)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return 500, nil return nil
} }
} }
} }
return 200, nil return nil
} }
func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int, error) { func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) error {
if ch.Media == nil { if ch.Media == nil {
http.NotFound(w, r) http.NotFound(w, r)
return 404, nil return nil
} }
referenced := ch.ReferencedMedia() referenced := ch.ReferencedMedia()
@ -114,7 +119,7 @@ func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int
mm, err := ch.Media.Walk() mm, err := ch.Media.Walk()
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return 500, nil return err
} }
for _, m := range media.Set(mm).ByDate() { for _, m := range media.Set(mm).ByDate() {
@ -167,15 +172,15 @@ func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int
} }
} }
</script></body><html>`) </script></body><html>`)
return 200, nil return nil
} }
func (ch *CaddyHugo) serveMedia(w http.ResponseWriter, r *http.Request) (int, error) { func (ch *CaddyHugo) serveMedia(w http.ResponseWriter, r *http.Request) error {
if ch.Media == nil { if ch.Media == nil {
http.NotFound(w, r) http.NotFound(w, r)
return 404, nil return nil
} }
ch.Media.ServeHTTP(w, r) ch.Media.ServeHTTP(w, r)
return 200, nil return nil
} }

@ -3,20 +3,19 @@ package caddyhugo
import ( import (
"fmt" "fmt"
"html/template" "html/template"
"log"
"os" "os"
"path" "path"
"path/filepath"
"git.stephensearles.com/stephen/caddy-hugo2/comments" "git.stephensearles.com/stephen/caddy-hugo2/comments"
"git.stephensearles.com/stephen/caddy-hugo2/media" "git.stephensearles.com/stephen/caddy-hugo2/media"
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
"github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/hugofs"
) )
var eventHookCounter uint64 var eventHookCounter uint64
/*
func SetupCaddy(c *caddy.Controller) error { func SetupCaddy(c *caddy.Controller) error {
ch := &CaddyHugo{} ch := &CaddyHugo{}
@ -36,18 +35,13 @@ func SetupCaddy(c *caddy.Controller) error {
return err return err
} }
*/
func (ch *CaddyHugo) commentsSetting(c *caddy.Controller) { func (ch *CaddyHugo) commentsSetting() {
for c.NextLine() { if ch.Site.CommentsEnabled {
if c.Val() == "hugo" { ch.Comments = comments.WithStorage(comments.NewDiskv(path.Join(ch.Site.Root, "comments")))
for c.NextBlock() { if ch.Site.CommentsPassword != "" {
if c.Val() == "comments" { ch.Comments.Password = ch.Site.CommentsPassword
ch.Comments = comments.WithStorage(comments.NewDiskv(path.Join(ch.Site.Root, "comments")))
if c.NextArg() {
ch.Comments.Password = c.Val()
}
}
}
} }
} }
} }
@ -55,6 +49,7 @@ func (ch *CaddyHugo) commentsSetting(c *caddy.Controller) {
func (ch *CaddyHugo) Setup(dir string) error { func (ch *CaddyHugo) Setup(dir string) error {
var err error var err error
log.Println("setting up caddy-hugo in", dir)
ch.Dir = dir ch.Dir = dir
ch.docs = make(map[string]*editSession) ch.docs = make(map[string]*editSession)
ch.confirmingToClient = make(map[uint64]struct{}) ch.confirmingToClient = make(map[uint64]struct{})

@ -10,8 +10,6 @@ import (
"strings" "strings"
"git.stephensearles.com/stephen/acedoc" "git.stephensearles.com/stephen/acedoc"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
) )
func (t *tmplData) Content() ([]Content, error) { func (t *tmplData) Content() ([]Content, error) {
@ -63,7 +61,7 @@ func (t *tmplData) contentTypes(dir string) ([]string, error) {
} }
type tmplData struct { type tmplData struct {
Site *httpserver.SiteConfig Site SiteConfig
R *http.Request R *http.Request
*CaddyHugo *CaddyHugo
Doc *acedoc.Document Doc *acedoc.Document

@ -1,14 +1,17 @@
localhost:8080 { {
order hugo last
}
http://localhost:8080 {
hugo { hugo {
comments test comments test
} }
root ./testsite root ./testdir/testsite
errors { * } log
pprof
} }
localhost:8081, localhost:8082 { http://localhost:8081, http://localhost:8082 {
root ./testsite2 root ./testdir/testsite2
hugo hugo
errors { * } log
} }

Loading…
Cancel
Save