mirror of
https://hacklab.nilfm.cc/nirvash
synced 2024-10-22 14:01:49 +00:00
36 lines
No EOL
943 B
Go
36 lines
No EOL
943 B
Go
package lfo
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"net/http"
|
|
core "nilfm.cc/git/nirvash/archetype"
|
|
)
|
|
|
|
func WithAdapter(next http.Handler, adapter core.Adapter) http.Handler {
|
|
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)
|
|
}
|
|
|
|
func EnsurePageData(next http.Handler) http.Handler {
|
|
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)
|
|
} |