From afdefd0632fcf067da50222390755a3a3ac0c0ed Mon Sep 17 00:00:00 2001 From: zhiyang7 Date: Mon, 30 Aug 2021 10:57:10 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8DPOST=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E4=BC=A0=E5=8F=82=EF=BC=8C=E5=85=BC=E5=AE=B9serverchan?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E4=BC=A0=E5=8F=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go-wecomchan/go.mod | 1 + go-wecomchan/go.sum | 2 ++ go-wecomchan/wecomchan.go | 38 ++++++++++++++++++++++++-------------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/go-wecomchan/go.mod b/go-wecomchan/go.mod index 388cf93..88016bf 100644 --- a/go-wecomchan/go.mod +++ b/go-wecomchan/go.mod @@ -3,3 +3,4 @@ module go/wecomchan go 1.16 require github.com/go-redis/redis/v8 v8.10.0 +require github.com/julienschmidt/httprouter v1.3.0 diff --git a/go-wecomchan/go.sum b/go-wecomchan/go.sum index 97595ea..44249e0 100644 --- a/go-wecomchan/go.sum +++ b/go-wecomchan/go.sum @@ -22,6 +22,8 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= +github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/go-wecomchan/wecomchan.go b/go-wecomchan/wecomchan.go index d808b7a..ba96920 100644 --- a/go-wecomchan/wecomchan.go +++ b/go-wecomchan/wecomchan.go @@ -15,6 +15,7 @@ import ( "time" "github.com/go-redis/redis/v8" + "github.com/julienschmidt/httprouter" ) /*------------------------------- 环境变量配置 begin -------------------------------*/ @@ -236,22 +237,29 @@ func InitJsonData(msgType string) JsonData { func main() { // 设置日志内容显示文件名和行号 log.SetFlags(log.LstdFlags | log.Lshortfile) - wecomChan := func(res http.ResponseWriter, req *http.Request) { + wecomChan := func(res http.ResponseWriter, req *http.Request, ps httprouter.Params) { + _ = req.ParseForm() + sendkey := req.Form.Get("sendkey") + urlpath := ps.ByName("urlpath") + if len(sendkey) == 0 && len(urlpath) > 6 && urlpath != "wecomchan" { + sendkey = urlpath[1 : len(urlpath)-5] + } + if sendkey != Sendkey { + log.Panicln("sendkey 错误,请检查") + } + req.ParseForm() + msgContent := req.Form.Get("msg") + if len(msgContent) == 0 { + msgContent = req.Form.Get("title") + "\n" + req.Form.Get("desc") + } + msgType := req.Form.Get("msg_type") + log.Println("mes_type=", msgType) + // 默认mediaId为空 + mediaId := "" // 获取token accessToken := GetAccessToken() // 默认token有效 tokenValid := true - - _ = req.ParseForm() - sendkey := req.FormValue("sendkey") - if sendkey != Sendkey { - log.Panicln("sendkey 错误,请检查") - } - msgContent := req.FormValue("msg") - msgType := req.FormValue("msg_type") - log.Println("mes_type=", msgType) - // 默认mediaId为空 - mediaId := "" if msgType != "image" { log.Println("消息类型不是图片") } else { @@ -297,6 +305,8 @@ func main() { res.Header().Set("Content-type", "application/json") _, _ = res.Write([]byte(postStatus)) } - http.HandleFunc("/wecomchan", wecomChan) - log.Fatal(http.ListenAndServe(":8080", nil)) + router := httprouter.New() + router.GET("/*urlpath", wecomChan) + router.POST("/*urlpath", wecomChan) + log.Fatal(http.ListenAndServe(":8080", router)) }