diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..93497a1 --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,50 @@ +name: Go + +on: + push: + branches: [ master,release-* ] + pull_request: + branches: [ master,release-* ] + +jobs: + build: + name: Build + runs-on: ubuntu-latest + services: + redis: + image: redis + ports: + - 6379:6379 + options: --entrypoint redis-server + memcached: + image: memcached + ports: + - 11211:11211 + steps: + + - name: Set up Go 1.x + uses: actions/setup-go@v2 + with: + go-version: ^1.13 + id: go + + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + + - name: Get dependencies + run: | + go get -v -t -d ./... + if [ -f Gopkg.toml ]; then + curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh + dep ensure + fi + + - name: Lint Go Code + run: | + export PATH=$PATH:$(go env GOPATH)/bin # temporary fix. See https://github.com/actions/setup-go/issues/14 + go get -u golang.org/x/lint/golint + go vet ./... + golint -set_exit_status $(go list ./...) + + - name: Test + run: go test -v -race ./... diff --git a/README.md b/README.md index 2e567f9..ef55760 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # WeChat SDK for Go -[![Build Status](https://travis-ci.org/silenceper/wechat.svg?branch=release-1.3)](https://travis-ci.org/silenceper/wechat) +[![Build Status](https://travis-ci.org/silenceper/wechat.svg?branch=release-2.0)](https://travis-ci.org/silenceper/wechat) [![Go Report Card](https://goreportcard.com/badge/github.com/silenceper/wechat)](https://goreportcard.com/report/github.com/silenceper/wechat) [![GoDoc](http://godoc.org/github.com/silenceper/wechat?status.svg)](http://godoc.org/github.com/silenceper/wechat) @@ -44,6 +44,8 @@ server.Send() ``` +## 文档 +[Wechat SDK 2.0 文档](http://silenceper.com/wechat) ## 目录说明 @@ -55,6 +57,10 @@ server.Send() - work:企业微信 - aispeech:智能对话 +## 如何贡献 +- 提交issue,描述需要贡献的内容 +- 完成更改后,提交PR + ## License diff --git a/go.mod b/go.mod index e0bd97c..4446b66 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,7 @@ go 1.14 require ( github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b - github.com/davecgh/go-spew v1.1.1 // indirect github.com/gomodule/redigo v1.8.1 - github.com/kr/pretty v0.1.0 // indirect + github.com/sirupsen/logrus v1.6.0 golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 - gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect ) diff --git a/go.sum b/go.sum index 9eddd20..088389b 100644 --- a/go.sum +++ b/go.sum @@ -6,11 +6,13 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gomodule/redigo v1.8.1 h1:Abmo0bI7Xf0IhdIPc7HZQzZcShdnmxeoVuDDtIQp8N8= github.com/gomodule/redigo v1.8.1/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8= +github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= +github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw= @@ -19,11 +21,11 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2eP golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/officialaccount/server/server.go b/officialaccount/server/server.go index a3ac17e..b8addfa 100644 --- a/officialaccount/server/server.go +++ b/officialaccount/server/server.go @@ -12,6 +12,7 @@ import ( "github.com/silenceper/wechat/v2/officialaccount/context" "github.com/silenceper/wechat/v2/officialaccount/message" + log "github.com/sirupsen/logrus" "github.com/silenceper/wechat/v2/util" ) @@ -22,7 +23,7 @@ type Server struct { Writer http.ResponseWriter Request *http.Request - debug bool + skipValidate bool openID string @@ -46,14 +47,15 @@ func NewServer(context *context.Context) *Server { return srv } -// SetDebug set debug field -func (srv *Server) SetDebug(debug bool) { - srv.debug = debug +// SkipValidate set skip validate +func (srv *Server) SkipValidate(skip bool) { + srv.skipValidate = skip } //Serve 处理微信的请求消息 func (srv *Server) Serve() error { if !srv.Validate() { + log.Error("Validate Signature Failed.") return fmt.Errorf("请求校验失败") } @@ -68,22 +70,21 @@ func (srv *Server) Serve() error { return err } - //debug - if srv.debug { - fmt.Println("request msg = ", string(srv.requestRawXMLMsg)) - } + //debug print request msg + log.Debugf("request msg =%s", string(srv.requestRawXMLMsg)) return srv.buildResponse(response) } //Validate 校验请求是否合法 func (srv *Server) Validate() bool { - if srv.debug { + if srv.skipValidate { return true } timestamp := srv.Query("timestamp") nonce := srv.Query("nonce") signature := srv.Query("signature") + log.Debugf("validate signature, timestamp=%s, nonce=%s", timestamp, nonce) return signature == util.Signature(srv.Token, timestamp, nonce) } @@ -223,6 +224,7 @@ func (srv *Server) buildResponse(reply *message.Reply) (err error) { //Send 将自定义的消息发送 func (srv *Server) Send() (err error) { replyMsg := srv.responseMsg + log.Debugf("response msg =%+v", replyMsg) if srv.isSafeMode { //安全模式下对消息进行加密 var encryptedMsg []byte diff --git a/wechat.go b/wechat.go index 0bfd550..d3d2443 100644 --- a/wechat.go +++ b/wechat.go @@ -1,6 +1,7 @@ package wechat import ( + "os" "github.com/silenceper/wechat/v2/cache" "github.com/silenceper/wechat/v2/miniprogram" miniConfig "github.com/silenceper/wechat/v2/miniprogram/config" @@ -10,8 +11,23 @@ import ( openConfig "github.com/silenceper/wechat/v2/openplatform/config" "github.com/silenceper/wechat/v2/pay" payConfig "github.com/silenceper/wechat/v2/pay/config" + log "github.com/sirupsen/logrus" ) +func init() { + + // Log as JSON instead of the default ASCII formatter. + log.SetFormatter(&log.TextFormatter{}) + + // Output to stdout instead of the default stderr + // Can be any io.Writer, see below for File example + log.SetOutput(os.Stdout) + + // Only log the warning severity or above. + log.SetLevel(log.DebugLevel) + +} + // Wechat struct type Wechat struct { cache cache.Cache