wecomchan/go-scf/main.go

53 lines
1.8 KiB
Go
Raw Normal View History

2021-07-04 20:55:34 +08:00
package main
import (
"context"
2021-07-05 01:21:03 +08:00
"fmt"
2021-07-04 20:55:34 +08:00
"strings"
2021-07-05 01:21:03 +08:00
"github.com/riba2534/wecomchan/go-scf/consts"
2021-07-07 00:10:22 +08:00
"github.com/riba2534/wecomchan/go-scf/dal"
2021-07-04 20:55:34 +08:00
"github.com/riba2534/wecomchan/go-scf/service"
"github.com/riba2534/wecomchan/go-scf/utils"
"github.com/tencentyun/scf-go-lib/cloudfunction"
"github.com/tencentyun/scf-go-lib/events"
)
2021-07-05 01:21:03 +08:00
func init() {
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")
2021-07-07 00:10:22 +08:00
if consts.FUNC_NAME == "" || consts.SEND_KEY == "" || consts.WECOM_CID == "" ||
consts.WECOM_SECRET == "" || consts.WECOM_AID == "" || consts.WECOM_TOUID == "" {
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")
2021-07-07 00:10:22 +08:00
}
fmt.Println("os.env load success!")
2021-07-05 01:21:03 +08:00
}
2021-07-04 20:55:34 +08:00
func HTTPHandler(ctx context.Context, event events.APIGatewayRequest) (events.APIGatewayResponse, error) {
path := event.Path
2021-07-07 00:10:22 +08:00
fmt.Println("req->", utils.MarshalToStringParam(event))
2021-07-04 20:55:34 +08:00
var result interface{}
2021-07-05 01:21:03 +08:00
if strings.HasPrefix(path, "/"+consts.FUNC_NAME) {
result = service.WeComChanService(ctx, event)
2021-07-04 20:55:34 +08:00
} else {
// 匹配失败返回原始HTTP请求
result = event
}
return events.APIGatewayResponse{
IsBase64Encoded: false,
StatusCode: 200,
Headers: map[string]string{},
Body: utils.MarshalToStringParam(result),
}, nil
}
func main() {
2021-07-07 00:10:22 +08:00
dal.Init()
2021-07-04 20:55:34 +08:00
cloudfunction.Start(HTTPHandler)
}