Skip to content

Commit

Permalink
云函数配置改为从环境变量中读取
Browse files Browse the repository at this point in the history
  • Loading branch information
riba2534 committed Jul 7, 2021
1 parent c8d3f08 commit 8398398
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 607 deletions.
3 changes: 2 additions & 1 deletion go-scf/build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
set -ex

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o main && upx -9 main

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main
zip main.zip main
7 changes: 0 additions & 7 deletions go-scf/config.yaml.example

This file was deleted.

4 changes: 3 additions & 1 deletion go-scf/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var (

// 微信发消息API
const (
WeComMsgSendURL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s"
// https://work.weixin.qq.com/api/doc/90000/90135/90236
WeComMsgSendURL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s"
// https://work.weixin.qq.com/api/doc/90000/90135/91039
WeComAccessTokenURL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s"
)
2 changes: 0 additions & 2 deletions go-scf/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@ 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
)
576 changes: 2 additions & 574 deletions go-scf/go.sum

Large diffs are not rendered by default.

27 changes: 9 additions & 18 deletions go-scf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,23 @@ import (
"github.com/riba2534/wecomchan/go-scf/dal"
"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"))
consts.FUNC_NAME = utils.GetEnvDefault("FUNC_NAME", "")
consts.SEND_KEY = utils.GetEnvDefault("SEND_KEY", "")
consts.WECOM_CID = utils.GetEnvDefault("WECOM_CID", "")
consts.WECOM_SECRET = utils.GetEnvDefault("WECOM_SECRET", "")
consts.WECOM_AID = utils.GetEnvDefault("WECOM_AID", "")
consts.WECOM_TOUID = utils.GetEnvDefault("WECOM_TOUID", "@all")
if consts.FUNC_NAME == "" || consts.SEND_KEY == "" || consts.WECOM_CID == "" ||
consts.WECOM_SECRET == "" || consts.WECOM_AID == "" || consts.WECOM_TOUID == "" {
fmt.Println("config.yaml is None, please check")
panic("config.yaml param error")
fmt.Printf("os.env load Fail, please check your os env.\nFUNC_NAME=%s\nSEND_KEY=%s\nWECOM_CID=%s\nWECOM_SECRET=%s\nWECOM_AID=%s\nWECOM_TOUID=%s\n", consts.FUNC_NAME, consts.SEND_KEY, consts.WECOM_CID, consts.WECOM_SECRET, consts.WECOM_AID, consts.WECOM_TOUID)
panic("os.env param error")
}
fmt.Println("config.yaml load success!")
fmt.Println("os.env load success!")
}

func HTTPHandler(ctx context.Context, event events.APIGatewayRequest) (events.APIGatewayResponse, error) {
Expand Down
10 changes: 7 additions & 3 deletions go-scf/service/wecomchan.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ func WeComChanService(ctx context.Context, event events.APIGatewayRequest) map[s
if sendKey != consts.SEND_KEY {
return utils.MakeResp(-1, "sendkey error")
}
if err := postWechatMsg(dal.AccessToken, msg, msgType); err != nil {
toUser := getQuery("to_user", event.QueryString)
if toUser == "" {
toUser = consts.WECOM_TOUID
}
if err := postWechatMsg(dal.AccessToken, msg, msgType, toUser); err != nil {
return utils.MakeResp(0, err.Error())
}
return utils.MakeResp(0, "success")
}

func postWechatMsg(accessToken, msg, msgType string) error {
func postWechatMsg(accessToken, msg, msgType, toUser string) error {
content := &model.WechatMsg{
ToUser: consts.WECOM_TOUID,
ToUser: toUser,
AgentId: consts.WECOM_AID,
MsgType: msgType,
DuplicateCheckInterval: 600,
Expand Down
14 changes: 13 additions & 1 deletion go-scf/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package utils

import jsoniter "github.com/json-iterator/go"
import (
"os"

jsoniter "github.com/json-iterator/go"
)

func MarshalToStringParam(param interface{}) string {
s, err := jsoniter.MarshalToString(param)
Expand All @@ -16,3 +20,11 @@ func MakeResp(code int, msg string) map[string]interface{} {
"msg": msg,
}
}

func GetEnvDefault(key, defVal string) string {
val, ex := os.LookupEnv(key)
if !ex {
return defVal
}
return val
}

0 comments on commit 8398398

Please sign in to comment.