@@ -10,7 +10,7 @@ import (
10
10
daprruntime "github.com/dapr/dapr/pkg/runtime"
11
11
dapr "github.com/dapr/go-sdk/service/common"
12
12
grpcsvc "github.com/dapr/go-sdk/service/grpc"
13
- httpsvc "github.com/dapr/go-sdk/service/grpc "
13
+ httpsvc "github.com/dapr/go-sdk/service/http "
14
14
"k8s.io/klog/v2"
15
15
16
16
ofctx "github.com/OpenFunction/functions-framework-go/context"
@@ -25,6 +25,7 @@ const (
25
25
)
26
26
27
27
type Runtime struct {
28
+ protocol string
28
29
port string
29
30
pattern string
30
31
handler dapr.Service
@@ -49,24 +50,23 @@ func NewAsyncRuntime(port string, pattern string) (*Runtime, error) {
49
50
}, nil
50
51
}
51
52
52
- var newService func ( address string ) ( s dapr.Service , err error )
53
+ var handler dapr.Service
53
54
protocol := daprruntime .Protocol (os .Getenv (protocolEnvVar ))
54
55
switch protocol {
55
56
case daprruntime .HTTPProtocol :
56
- newService = httpsvc .NewService
57
- case daprruntime .GRPCProtocol :
58
- newService = grpcsvc .NewService
57
+ handler = httpsvc .NewService (fmt .Sprintf (":%s" , port ))
59
58
default :
60
59
protocol = daprruntime .GRPCProtocol
61
- newService = grpcsvc .NewService
62
- }
63
- handler , err := newService ( fmt . Sprintf ( ":%s " , port ) )
64
- if err != nil {
65
- klog . Errorf ( "failed to create dapr %v service: %v \n " , protocol , err )
66
- return nil , err
60
+ service , err : = grpcsvc .NewService ( fmt . Sprintf ( ":%s" , port ))
61
+ if err != nil {
62
+ klog . Errorf ( "failed to create dapr grpc service: %v \n " , err )
63
+ return nil , err
64
+ }
65
+ handler = service
67
66
}
68
67
69
68
return & Runtime {
69
+ protocol : string (protocol ),
70
70
port : port ,
71
71
pattern : pattern ,
72
72
handler : handler ,
@@ -75,7 +75,7 @@ func NewAsyncRuntime(port string, pattern string) (*Runtime, error) {
75
75
}
76
76
77
77
func (r * Runtime ) Start (ctx context.Context ) error {
78
- klog .Infof ("Async Function serving grpc : listening on port %s" , r .port )
78
+ klog .Infof ("Async Function serving %s : listening on port %s" , r . protocol , r .port )
79
79
klog .Fatal (r .handler .Start ())
80
80
return nil
81
81
}
@@ -121,6 +121,7 @@ func (r *Runtime) RegisterOpenFunction(
121
121
input .Uri = input .ComponentName
122
122
funcErr = r .handler .AddBindingInvocationHandler (input .Uri , func (c context.Context , in * dapr.BindingEvent ) (out []byte , err error ) {
123
123
rm := runtime .NewRuntimeManager (ctx , prePlugins , postPlugins )
124
+ rm .FuncContext .SetNativeContext (c )
124
125
rm .FuncContext .SetEvent (n , in )
125
126
rm .FunctionRunWrapperWithHooks (rf .GetOpenFunctionFunction ())
126
127
@@ -141,8 +142,12 @@ func (r *Runtime) RegisterOpenFunction(
141
142
PubsubName : input .ComponentName ,
142
143
Topic : input .Uri ,
143
144
}
145
+ if r .protocol == string (daprruntime .HTTPProtocol ) {
146
+ sub .Route = fmt .Sprintf ("/%s" , input .Uri )
147
+ }
144
148
funcErr = r .handler .AddTopicEventHandler (sub , func (c context.Context , e * dapr.TopicEvent ) (retry bool , err error ) {
145
149
rm := runtime .NewRuntimeManager (ctx , prePlugins , postPlugins )
150
+ rm .FuncContext .SetNativeContext (c )
146
151
rm .FuncContext .SetEvent (n , e )
147
152
rm .FunctionRunWrapperWithHooks (rf .GetOpenFunctionFunction ())
148
153
0 commit comments