Initial commit: Open sourcing all of the Maple Open Technologies code.
This commit is contained in:
commit
755d54a99d
2010 changed files with 448675 additions and 0 deletions
|
|
@ -0,0 +1,29 @@
|
|||
// codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/interface/http/middleware/url.go
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// URLProcessorMiddleware Middleware will split the full URL path into slash-sperated parts and save to
|
||||
// the context to flow downstream in the app for this particular request.
|
||||
func (mid *middleware) URLProcessorMiddleware(fn http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// Split path into slash-separated parts, for example, path "/foo/bar"
|
||||
// gives p==["foo", "bar"] and path "/" gives p==[""]. Our API starts with
|
||||
// "/api", as a result we will start the array slice at "1".
|
||||
p := strings.Split(r.URL.Path, "/")[1:]
|
||||
|
||||
// log.Println(p) // For debugging purposes only.
|
||||
|
||||
// Open our program's context based on the request and save the
|
||||
// slash-seperated array from our URL path.
|
||||
ctx := r.Context()
|
||||
ctx = context.WithValue(ctx, "url_split", p)
|
||||
|
||||
// Flow to the next middleware.
|
||||
fn(w, r.WithContext(ctx))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue