@@ -10,7 +10,7 @@ import (
10
10
11
11
cloudevents "github.com/cloudevents/sdk-go/v2"
12
12
13
- "github.com/gorilla/mux "
13
+ "github.com/go-chi/chi/v5 "
14
14
"k8s.io/klog/v2"
15
15
16
16
ofctx "github.com/OpenFunction/functions-framework-go/context"
@@ -30,7 +30,7 @@ const (
30
30
type Runtime struct {
31
31
port string
32
32
pattern string
33
- handler * mux. Router
33
+ handler * chi. Mux
34
34
}
35
35
36
36
func NewKnativeRuntime (port string , pattern string ) * Runtime {
@@ -40,7 +40,7 @@ func NewKnativeRuntime(port string, pattern string) *Runtime {
40
40
return & Runtime {
41
41
port : port ,
42
42
pattern : pattern ,
43
- handler : mux .NewRouter (),
43
+ handler : chi .NewRouter (),
44
44
}
45
45
}
46
46
@@ -61,11 +61,10 @@ func (r *Runtime) RegisterOpenFunction(
61
61
ctx .InitDaprClientIfNil ()
62
62
}
63
63
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 ) {
66
65
rm := runtime .NewRuntimeManager (ctx , prePlugins , postPlugins )
67
66
// 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 () ))
69
68
rm .FuncContext .SetNativeContext (_ctx )
70
69
rm .FuncContext .SetSyncRequest (w , r .WithContext (_ctx ))
71
70
defer RecoverPanicHTTP (w , "Function panic" )
@@ -84,12 +83,17 @@ func (r *Runtime) RegisterOpenFunction(
84
83
default :
85
84
return
86
85
}
87
- })
86
+ }
88
87
89
- // add methods matcher if provided
90
88
methods := rf .GetFunctionMethods ()
89
+ // Register the synchronous function (based on Knaitve runtime)
91
90
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 )
93
97
}
94
98
95
99
return nil
@@ -101,20 +105,24 @@ func (r *Runtime) RegisterHTTPFunction(
101
105
postPlugins []plugin.Plugin ,
102
106
rf * functions.RegisteredFunction ,
103
107
) error {
104
- route := r . handler . HandleFunc ( rf . GetPath (), func (w http.ResponseWriter , r * http.Request ) {
108
+ fn := func (w http.ResponseWriter , r * http.Request ) {
105
109
rm := runtime .NewRuntimeManager (ctx , prePlugins , postPlugins )
106
110
// 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 () ))
108
112
rm .FuncContext .SetNativeContext (_ctx )
109
113
rm .FuncContext .SetSyncRequest (w , r .WithContext (_ctx ))
110
114
defer RecoverPanicHTTP (w , "Function panic" )
111
115
rm .FunctionRunWrapperWithHooks (rf .GetHTTPFunction ())
112
- })
116
+ }
113
117
114
- // add methods matcher if any
115
118
methods := rf .GetFunctionMethods ()
116
119
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 )
118
126
}
119
127
120
128
return nil
@@ -150,8 +158,8 @@ func (r *Runtime) RegisterCloudEventFunction(
150
158
// function to extract Vars and add into ctx
151
159
withVars := func (next http.Handler ) http.Handler {
152
160
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 ))
155
163
})
156
164
}
157
165
r .handler .Handle (rf .GetPath (), withVars (handleFn ))
0 commit comments