@@ -10,7 +10,7 @@ import (
1010
1111 cloudevents "github.com/cloudevents/sdk-go/v2"
1212
13- "github.com/gorilla/mux "
13+ "github.com/go-chi/chi/v5 "
1414 "k8s.io/klog/v2"
1515
1616 ofctx "github.com/OpenFunction/functions-framework-go/context"
@@ -30,7 +30,7 @@ const (
3030type Runtime struct {
3131 port string
3232 pattern string
33- handler * mux. Router
33+ handler * chi. Mux
3434}
3535
3636func NewKnativeRuntime (port string , pattern string ) * Runtime {
@@ -40,7 +40,7 @@ func NewKnativeRuntime(port string, pattern string) *Runtime {
4040 return & Runtime {
4141 port : port ,
4242 pattern : pattern ,
43- handler : mux .NewRouter (),
43+ handler : chi .NewRouter (),
4444 }
4545}
4646
@@ -61,11 +61,10 @@ func (r *Runtime) RegisterOpenFunction(
6161 ctx .InitDaprClientIfNil ()
6262 }
6363
64- // Register the synchronous function (based on Knaitve runtime)
65- route := r .handler .HandleFunc (rf .GetPath (), func (w http.ResponseWriter , r * http.Request ) {
64+ fn := func (w http.ResponseWriter , r * http.Request ) {
6665 rm := runtime .NewRuntimeManager (ctx , prePlugins , postPlugins )
6766 // save the Vars into the context
68- _ctx := ofctx .CtxWithVars (r .Context (), ofctx .Vars ( r ))
67+ _ctx := ofctx .CtxWithVars (r .Context (), ofctx .URLParamsFromCtx ( r . Context () ))
6968 rm .FuncContext .SetNativeContext (_ctx )
7069 rm .FuncContext .SetSyncRequest (w , r .WithContext (_ctx ))
7170 defer RecoverPanicHTTP (w , "Function panic" )
@@ -84,12 +83,17 @@ func (r *Runtime) RegisterOpenFunction(
8483 default :
8584 return
8685 }
87- })
86+ }
8887
89- // add methods matcher if provided
9088 methods := rf .GetFunctionMethods ()
89+ // Register the synchronous function (based on Knaitve runtime)
9190 if len (methods ) > 0 {
92- route .Methods (methods ... )
91+ // add methods matcher if provided
92+ for _ , method := range methods {
93+ r .handler .MethodFunc (method , rf .GetPath (), fn )
94+ }
95+ } else {
96+ r .handler .HandleFunc (rf .GetPath (), fn )
9397 }
9498
9599 return nil
@@ -101,20 +105,24 @@ func (r *Runtime) RegisterHTTPFunction(
101105 postPlugins []plugin.Plugin ,
102106 rf * functions.RegisteredFunction ,
103107) error {
104- route := r . handler . HandleFunc ( rf . GetPath (), func (w http.ResponseWriter , r * http.Request ) {
108+ fn := func (w http.ResponseWriter , r * http.Request ) {
105109 rm := runtime .NewRuntimeManager (ctx , prePlugins , postPlugins )
106110 // save the Vars into the context
107- _ctx := ofctx .CtxWithVars (r .Context (), ofctx .Vars ( r ))
111+ _ctx := ofctx .CtxWithVars (r .Context (), ofctx .URLParamsFromCtx ( r . Context () ))
108112 rm .FuncContext .SetNativeContext (_ctx )
109113 rm .FuncContext .SetSyncRequest (w , r .WithContext (_ctx ))
110114 defer RecoverPanicHTTP (w , "Function panic" )
111115 rm .FunctionRunWrapperWithHooks (rf .GetHTTPFunction ())
112- })
116+ }
113117
114- // add methods matcher if any
115118 methods := rf .GetFunctionMethods ()
116119 if len (methods ) > 0 {
117- route .Methods (methods ... )
120+ // add methods matcher if provided
121+ for _ , method := range methods {
122+ r .handler .MethodFunc (method , rf .GetPath (), fn )
123+ }
124+ } else {
125+ r .handler .HandleFunc (rf .GetPath (), fn )
118126 }
119127
120128 return nil
@@ -150,8 +158,8 @@ func (r *Runtime) RegisterCloudEventFunction(
150158 // function to extract Vars and add into ctx
151159 withVars := func (next http.Handler ) http.Handler {
152160 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
153- ctx := ofctx .CtxWithVars (r .Context (), ofctx .Vars ( r ))
154- next .ServeHTTP (w , r .WithContext (ctx ))
161+ _ctx := ofctx .CtxWithVars (r .Context (), ofctx .URLParamsFromCtx ( r . Context () ))
162+ next .ServeHTTP (w , r .WithContext (_ctx ))
155163 })
156164 }
157165 r .handler .Handle (rf .GetPath (), withVars (handleFn ))
0 commit comments