1 /* 2 Package middleware contains utilities for Goji Middleware authors. 3 4 Unless you are writing middleware for your application, you should avoid 5 importing this package. Instead, use the abstractions provided by your 6 middleware package. 7 */ 8 package middleware 9 10 import ( 11 "context" 12 "net/http" 13 14 "goji.io" 15 "goji.io/internal" 16 ) 17 18 /* 19 Pattern returns the most recently matched Pattern, or nil if no pattern was 20 matched. 21 */ 22 func Pattern(ctx context.Context) goji.Pattern { 23 p := ctx.Value(internal.Pattern) 24 if p == nil { 25 return nil 26 } 27 return p.(goji.Pattern) 28 } 29 30 /* 31 SetPattern returns a new context in which the given Pattern is used as the most 32 recently matched pattern. 33 */ 34 func SetPattern(ctx context.Context, p goji.Pattern) context.Context { 35 return context.WithValue(ctx, internal.Pattern, p) 36 } 37 38 /* 39 Handler returns the handler corresponding to the most recently matched Pattern, 40 or nil if no pattern was matched. 41 42 The handler returned by this function is the one that will be dispatched to at 43 the end of the middleware stack. If the returned Handler is nil, http.NotFound 44 will be used instead. 45 */ 46 func Handler(ctx context.Context) http.Handler { 47 h := ctx.Value(internal.Handler) 48 if h == nil { 49 return nil 50 } 51 return h.(http.Handler) 52 } 53 54 /* 55 SetHandler returns a new context in which the given Handler was most recently 56 matched and which consequently will be dispatched to. 57 */ 58 func SetHandler(ctx context.Context, h http.Handler) context.Context { 59 return context.WithValue(ctx, internal.Handler, h) 60 } 61