Skip to content

Commit 9f7956e

Browse files
authored
Merge pull request #17 from penghuima/master
add events
2 parents a9b5698 + 1fa415f commit 9f7956e

File tree

7 files changed

+170
-2
lines changed

7 files changed

+170
-2
lines changed

events/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@
88

99

1010
[SLS 事件格式](https://github.com/aliyun/fc-runtime-go-sdk/blob/master/events/README_SLS.md)
11+
12+
[MNS Topic 事件格式](https://github.com/aliyun/fc-runtime-go-sdk/blob/master/events/README_MNS_Topic.md)
13+
14+
[SLS Queue 事件格式](https://github.com/aliyun/fc-runtime-go-sdk/blob/master/events/README_MNS_Queue.md)

events/README_MNS_Queue.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
## MNS Queue 触发器
2+
3+
### Event 格式
4+
5+
mns queue 触发器的 event 格式如下所示:
6+
7+
```json
8+
{
9+
"id":"c2g71017-6f65-fhcf-a814-a396fc8d****",
10+
"source":"MNS-Function-mnstrigger",
11+
"specversion":"1.0",
12+
"type":"mns:Queue:SendMessage",
13+
"datacontenttype":"application/json; charset=utf-8",
14+
"subject":"acs:mns:cn-hangzhou:164901546557****:queues/zeus",
15+
"time":"2021-04-08T06:28:17.093Z",
16+
"aliyunaccountid":"1649015465574023",
17+
"aliyunpublishtime":"2021-10-15T07:06:34.028Z",
18+
"aliyunoriginalaccountid":"164901546557****",
19+
"aliyuneventbusname":"MNS-Function-mnstrigger",
20+
"aliyunregionid":"cn-chengdu",
21+
"aliyunpublishaddr":"42.120.XX.XX",
22+
"data":{
23+
"requestId":"606EA3074344430D4C81****",
24+
"messageId":"C6DB60D1574661357FA227277445****",
25+
"messageBody":"TEST"
26+
}
27+
}
28+
```
29+
30+
### 使用示例
31+
32+
下面展示了一个简单的 FC demo,当你为该函数配置 mns queue 触发器之后,每当你向 mns queue 发送任意消息之后,就会触发该函数,并返回消息内容。
33+
34+
```go
35+
package main
36+
37+
import (
38+
"context"
39+
"encoding/json"
40+
"fmt"
41+
"github.com/aliyun/fc-runtime-go-sdk/events"
42+
"github.com/aliyun/fc-runtime-go-sdk/fc"
43+
"github.com/aliyun/fc-runtime-go-sdk/fccontext"
44+
)
45+
46+
func HandleRequest(ctx context.Context, event events.MnsQueueEvent) (string, error) {
47+
fctx, _ := fccontext.FromContext(ctx)
48+
flog := fctx.GetLogger()
49+
mes, _ := json.Marshal(event)
50+
flog.Info("event:", string(mes))
51+
return fmt.Sprintf("MessageBody:%s", *event.Data.MessageBody), nil
52+
}
53+
54+
func main() {
55+
fc.Start(HandleRequest)
56+
}
57+
```
58+

events/README_MNS_Topic.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
## MNS Topic 触发器
2+
3+
### Event 格式
4+
5+
mns topic 触发器的 event 格式如下所示:
6+
7+
- 若event格式设置为 JSON
8+
9+
```json
10+
{
11+
"TopicOwner":"topic account id",
12+
"Message":"mock mns message",
13+
"Subscriber":"subscriber account id",
14+
"PublishTime":1658235558094,
15+
"SubscriptionName":"test-5bf13c7e",
16+
"MessageMD5":"652BF0E6297840015247C3xxxxxxx",
17+
"TopicName":"fc-example",
18+
"MessageId":"3405CA51807661353B3xxxxxxxx"
19+
}
20+
```
21+
- 若event格式设置为 STREAM
22+
23+
```bash
24+
# 消息正文
25+
'hello topic'
26+
```
27+
### 使用示例
28+
29+
下面展示了一个简单的 FC demo,当你为该函数配置 mns topic 触发器之后,每当你向 mns topic 发送任意消息之后,就会触发该函数,并返回消息内容。
30+
31+
```go
32+
package main
33+
34+
import (
35+
"context"
36+
"encoding/json"
37+
"fmt"
38+
"github.com/aliyun/fc-runtime-go-sdk/events"
39+
"github.com/aliyun/fc-runtime-go-sdk/fc"
40+
"github.com/aliyun/fc-runtime-go-sdk/fccontext"
41+
)
42+
43+
func HandleRequest(ctx context.Context, event interface{}) (string, error) {
44+
fctx, _ := fccontext.FromContext(ctx)
45+
flog := fctx.GetLogger()
46+
var topicEvent events.MnsTopicEvent
47+
switch mess := event.(type) {
48+
case string:
49+
{
50+
flog.Info("event:", event)
51+
return fmt.Sprintf("MessageBody:%s", mess), nil
52+
}
53+
default:
54+
result, _ := json.Marshal(mess)
55+
_ = json.Unmarshal(result, &topicEvent)
56+
flog.Info("event:", event)
57+
return fmt.Sprintf("MessageBody:%s", *topicEvent.Message), nil
58+
}
59+
60+
}
61+
62+
func main() {
63+
fc.Start(HandleRequest)
64+
}
65+
66+
```
67+

events/README_OSS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import (
6060
"github.com/aliyun/fc-runtime-go-sdk/events"
6161
)
6262

63-
func HandleRequest(ctx context.Context, event OssEvent) (string, error) {
63+
func HandleRequest(ctx context.Context, event events.OssEvent) (string, error) {
6464
fmt.Printf("hello,the name of your bucket is %s", *event.Events[0].Oss.Bucket.Name)
6565
return fmt.Sprintf("hello,The size of the object you are manipulating is %dB",*event.Events[0].Oss.Object.Size), nil
6666
}

events/README_SLS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
"github.com/aliyun/fc-runtime-go-sdk/events"
3636
)
3737

38-
func HandleRequest(ctx context.Context, event SlsEvent) (string, error) {
38+
func HandleRequest(ctx context.Context, event events.SlsEvent) (string, error) {
3939
fmt.Printf("hello,the name of your logstoreName is %s", *event.Source.LogstoreName)
4040
return fmt.Sprintf("hello,the name of your projectName is %s",*event.Source.ProjectName), nil
4141
}

events/mns-queue.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package events
2+
3+
import "time"
4+
5+
type MnsQueueEvent struct {
6+
ID *string `json:"id"`
7+
Source *string `json:"source"`
8+
Specversion *string `json:"specversion"`
9+
Type *string `json:"type"`
10+
Datacontenttype *string `json:"datacontenttype"`
11+
Subject *string `json:"subject"`
12+
Time time.Time `json:"time"`
13+
Aliyunaccountid *string `json:"aliyunaccountid"`
14+
Aliyunpublishtime time.Time `json:"aliyunpublishtime"`
15+
Aliyunoriginalaccountid *string `json:"aliyunoriginalaccountid"`
16+
Aliyuneventbusname *string `json:"aliyuneventbusname"`
17+
Aliyunregionid *string `json:"aliyunregionid"`
18+
Aliyunpublishaddr *string `json:"aliyunpublishaddr"`
19+
Data *Data `json:"data"`
20+
}
21+
type Data struct {
22+
RequestID *string `json:"requestId"`
23+
MessageID *string `json:"messageId"`
24+
MessageBody *string `json:"messageBody"`
25+
}

events/mns-topic.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package events
2+
3+
import "time"
4+
5+
type MnsTopicEvent struct {
6+
TopicOwner *string `json:"TopicOwner"`
7+
Message *string `json:"Message"`
8+
Subscriber *string `json:"Subscriber"`
9+
PublishTime time.Time `json:"PublishTime"`
10+
SubscriptionName *string `json:"SubscriptionName"`
11+
MessageMD5 *string `json:"MessageMD5"`
12+
TopicName *string `json:"TopicName"`
13+
MessageID *string `json:"MessageId"`
14+
}

0 commit comments

Comments
 (0)