lfo/middleware.go (raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
package lfo import ( "context" "net/http" core "nilfm.cc/git/nirvash/archetype" "strings" ) 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 WithFileManager(next http.Handler, fileManager core.FileManager) http.Handler { handlerFunc := func(w http.ResponseWriter, req *http.Request) { *req = *req.WithContext(context.WithValue(req.Context(), "file-manager", fileManager)) next.ServeHTTP(w, req) } return http.HandlerFunc(handlerFunc) } func EnsurePageData(next http.Handler, adapter core.Adapter) http.Handler { handlerFunc := func(w http.ResponseWriter, req *http.Request) { pageTitle := req.FormValue("title") pageContent := req.FormValue("content") newSlug := req.FormValue("slug") if pageTitle == "" || pageContent == "" || (adapter.EditableSlugs() && newSlug == "") { newUri := "/edit/" slug := strings.Join(strings.Split(req.URL.Path, "/")[2:], "/") newUri += slug newUri += "?no-empty=1" req.Method = http.MethodGet http.Redirect(w, req, newUri, http.StatusSeeOther) } else { next.ServeHTTP(w, req) } } return http.HandlerFunc(handlerFunc) } func SanitizeFormMap(next http.Handler) http.Handler { handlerFunc := func(w http.ResponseWriter, req *http.Request) { delete(req.PostForm, "csrfToken") next.ServeHTTP(w, req) } return http.HandlerFunc(handlerFunc) } func FormMapToAdapterConfig(next http.Handler) http.Handler { handlerFunc := func(w http.ResponseWriter, req *http.Request) { cfg := make(map[core.ConfigOption]string) for k, arr := range req.PostForm { v := strings.Join(arr, "") optNameAndType := strings.Split(k, ":") optName := optNameAndType[0] optType := optNameAndType[1] cfg[core.ConfigOption{ Name: optName, Type: optType, }] = v } *req = *req.WithContext(context.WithValue(req.Context(), "config", cfg)) next.ServeHTTP(w, req) } return http.HandlerFunc(handlerFunc) } func FormMapToBuildOptions(next http.Handler) http.Handler { handlerFunc := func(w http.ResponseWriter, req *http.Request) { options := make(map[core.BuildOption]string) for k, arr := range req.PostForm { v := strings.Join(arr, "") optNameAndType := strings.Split(k, ":") optName := optNameAndType[0] optType := optNameAndType[1] options[core.BuildOption{ Name: optName, Type: optType, }] = v } *req = *req.WithContext(context.WithValue(req.Context(), "build-options", options)) next.ServeHTTP(w, req) } return http.HandlerFunc(handlerFunc) } func PrepareForUpload(next http.Handler, fileManager core.FileManager) http.Handler { handlerFunc := func(w http.ResponseWriter, req *http.Request) { req.ParseMultipartForm(fileManager.MaxUploadMB() << 20) next.ServeHTTP(w, req) } return http.HandlerFunc(handlerFunc) } |