Skip to content

Commit

Permalink
完成云函数部署第一版
Browse files Browse the repository at this point in the history
  • Loading branch information
riba2534 committed Jul 4, 2021
1 parent 6afaa6b commit 017c2f5
Show file tree
Hide file tree
Showing 8 changed files with 613 additions and 16 deletions.
3 changes: 2 additions & 1 deletion go-scf/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ msg_notice
*.zip
*_test.go
*.zip
.DS_Store
.DS_Store
config.yaml
7 changes: 7 additions & 0 deletions go-scf/config.yaml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
config:
FUNC_NAME: 函数名称
SEND_KEY: 这里写你设置的send_key
WECOM_CID: 企业微信公司ID
WECOM_SECRET: 企业微信应用Secret
WECOM_AID: 企业微信应用ID
WECOM_TOUID: "@all" # 别改
13 changes: 7 additions & 6 deletions go-scf/consts/consts.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package consts

const (
SENDKEY = "set_a_sendkey"
WECOM_CID = "企业微信公司ID"
WECOM_SECRET = "企业微信应用Secret"
WECOM_AID = "企业微信应用ID"
WECOM_TOUID = "@all"
var (
FUNC_NAME string
SEND_KEY string
WECOM_CID string
WECOM_SECRET string
WECOM_AID string
WECOM_TOUID string
)

// 微信发消息API
Expand Down
2 changes: 2 additions & 0 deletions go-scf/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ go 1.16

require (
github.com/json-iterator/go v1.1.11
github.com/spf13/cast v1.3.1
github.com/spf13/viper v1.8.1
github.com/tencentyun/scf-go-lib v0.0.0-20200624065115-ba679e2ec9c9
)
565 changes: 565 additions & 0 deletions go-scf/go.sum

Large diffs are not rendered by default.

26 changes: 24 additions & 2 deletions go-scf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,41 @@ package main

import (
"context"
"fmt"
"strings"

"github.com/riba2534/wecomchan/go-scf/consts"
"github.com/riba2534/wecomchan/go-scf/service"
"github.com/riba2534/wecomchan/go-scf/utils"
"github.com/spf13/cast"
"github.com/spf13/viper"
"github.com/tencentyun/scf-go-lib/cloudfunction"
"github.com/tencentyun/scf-go-lib/events"
)

func init() {
config := viper.New()
config.SetConfigFile("config.yaml")
config.AddConfigPath(".")
err := config.ReadInConfig()
if err != nil {
panic("load config.yaml failed")
}
consts.FUNC_NAME = cast.ToString(config.Get("config.FUNC_NAME"))
consts.SEND_KEY = cast.ToString(config.Get("config.SEND_KEY"))
consts.WECOM_CID = cast.ToString(config.Get("config.WECOM_CID"))
consts.WECOM_SECRET = cast.ToString(config.Get("config.WECOM_SECRET"))
consts.WECOM_AID = cast.ToString(config.Get("config.WECOM_AID"))
consts.WECOM_TOUID = cast.ToString(config.Get("config.WECOM_TOUID"))
fmt.Println("config.yaml load success!")
}

func HTTPHandler(ctx context.Context, event events.APIGatewayRequest) (events.APIGatewayResponse, error) {
path := event.Path
fmt.Println("req:", utils.MarshalToStringParam(event))
var result interface{}
if strings.HasPrefix(path, "/wecomchan") {
result = service.WeComChanService
if strings.HasPrefix(path, "/"+consts.FUNC_NAME) {
result = service.WeComChanService(ctx, event)
} else {
// 匹配失败返回原始HTTP请求
result = event
Expand Down
12 changes: 6 additions & 6 deletions go-scf/service/wecomchan.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ import (
)

func WeComChanService(ctx context.Context, event events.APIGatewayRequest) interface{} {
accessToken, err := getAccessToken()
if err != nil {
return utils.MakeResp(-1, "get accessToken error")
}
sendKey := getQuery("sendkey", event.QueryString)
msgType := getQuery("msg_type", event.QueryString)
msg := getQuery("msg", event.QueryString)
if accessToken == "" || msgType == "" || msg == "" {
if msgType == "" || msg == "" {
return utils.MakeResp(-1, "param error")
}
if sendKey != consts.SENDKEY {
if sendKey != consts.SEND_KEY {
return utils.MakeResp(-1, "sendkey error")
}
accessToken, err := getAccessToken()
if err != nil {
return utils.MakeResp(-1, "get accessToken error")
}
if err := postWechatMsg(accessToken, msg, msgType); err != nil {
return utils.MakeResp(0, err.Error())
}
Expand Down
1 change: 0 additions & 1 deletion go-scf/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ func MarshalToStringParam(param interface{}) string {
return s
}


func MakeResp(code int, msg string) map[string]interface{} {
return map[string]interface{}{
"code": code,
Expand Down

0 comments on commit 017c2f5

Please sign in to comment.