Compare commits

...
2 Commits
3 changed files with 24 additions and 16 deletions
+18 -6
View File
@@ -8,6 +8,7 @@ import (
"html/template" "html/template"
"io" "io"
"net/http" "net/http"
"strconv"
"sync" "sync"
"time" "time"
@@ -68,6 +69,7 @@ func (cs *Service) User(r *http.Request) (string, bool) {
func (cs *Service) ServeComments(post string, w http.ResponseWriter, r *http.Request) error { func (cs *Service) ServeComments(post string, w http.ResponseWriter, r *http.Request) error {
user, allowed := cs.User(r) user, allowed := cs.User(r)
if !allowed { if !allowed {
fmt.Fprintf(w, "<a href='/login'>click here to enter your name and the comments password</a>")
w.WriteHeader(401) w.WriteHeader(401)
return nil return nil
} }
@@ -174,17 +176,27 @@ func (d *diskvStorage) nextKey(post string) (string, error) {
d.keyMtx.Lock() d.keyMtx.Lock()
defer d.keyMtx.Unlock() defer d.keyMtx.Unlock()
var i uint64
prefix := post + "-" prefix := post + "-"
lastKey := prefix
keys := d.KeysPrefix(prefix, nil) keys := d.KeysPrefix(prefix, nil)
for range keys { for key := range keys {
i++ lastKey = key
} }
key := prefix + fmt.Sprint(i) if lastKey == prefix {
err := d.WriteStream(key, &bytes.Buffer{}, true) return prefix + "1", nil
}
minusPrefix := lastKey[len(prefix):]
lastIdx, err := strconv.Atoi(minusPrefix)
if err != nil {
return "", fmt.Errorf("comment key %q seems malformed", lastKey)
}
key := prefix + fmt.Sprint(lastIdx+1)
err = d.WriteStream(key, &bytes.Buffer{}, true)
return key, err return key, err
} }
+5 -9
View File
@@ -119,10 +119,6 @@ func (ch *CaddyHugo) Auth(r *http.Request) bool {
return true return true
} }
func (ch *CaddyHugo) CommentAuth(r *http.Request) bool {
return true
}
func (ch *CaddyHugo) Match(r *http.Request) bool { func (ch *CaddyHugo) Match(r *http.Request) bool {
if strings.HasPrefix(r.URL.Path, "/media/") { if strings.HasPrefix(r.URL.Path, "/media/") {
return true return true
@@ -259,13 +255,13 @@ func (ch *CaddyHugo) commentsLogin(r *http.Request, w http.ResponseWriter) (int,
_, ok := ch.Comments.User(r) _, ok := ch.Comments.User(r)
if !ok { if !ok {
w.Header().Set("WWW-Authenticate", `Basic realm="Give 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."`)
return 401, nil 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())
return 200, nil
} }
if strings.HasSuffix(r.Referer(), "/comments") { http.Redirect(w, r, r.Referer(), http.StatusFound)
http.Redirect(w, r, r.Referer(), http.StatusFound)
}
return 200, nil return 200, nil
} }
+1 -1
View File
@@ -38,7 +38,7 @@ func (ch *CaddyHugo) commentsSetting(c *caddy.Controller) {
if c.Val() == "hugo" { if c.Val() == "hugo" {
for c.NextBlock() { for c.NextBlock() {
if c.Val() == "comments" { if c.Val() == "comments" {
ch.Comments = comments.Default() ch.Comments = comments.WithStorage(comments.NewDiskv(path.Join(ch.Site.Root, "comments")))
if c.NextArg() { if c.NextArg() {
ch.Comments.Password = c.Val() ch.Comments.Password = c.Val()
} }