2022-06-03 04:05:36 +00:00
|
|
|
package lfo
|
|
|
|
|
|
|
|
import (
|
2022-06-03 04:56:38 +00:00
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
core "nilfm.cc/git/nirvash/archetype"
|
2022-06-05 18:09:00 +00:00
|
|
|
"strings"
|
2022-06-03 04:05:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func WithAdapter(next http.Handler, adapter core.Adapter) http.Handler {
|
2022-06-03 04:56:38 +00:00
|
|
|
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
*req = *req.WithContext(context.WithValue(req.Context(), "adapter", adapter))
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(handlerFunc)
|
2022-06-03 04:05:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 04:34:20 +00:00
|
|
|
func EnsurePageData(next http.Handler) http.Handler {
|
2022-06-05 18:09:00 +00:00
|
|
|
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
pageTitle := req.FormValue("title")
|
|
|
|
pageContent := req.FormValue("content")
|
|
|
|
|
|
|
|
if pageTitle == "" || pageContent == "" {
|
|
|
|
newUri := "/edit/"
|
|
|
|
slug := strings.Join(strings.Split(req.URL.Path, "/")[2:], "/")
|
|
|
|
newUri += slug
|
|
|
|
req.Method = http.MethodGet
|
|
|
|
http.Redirect(w, req, newUri, http.StatusSeeOther)
|
|
|
|
} else {
|
|
|
|
next.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(handlerFunc)
|
|
|
|
}
|