1
0
mirror of https://github.com/nikoksr/notify.git synced 2025-07-03 00:57:51 +02:00

fmt/linter ran in correct order

This commit is contained in:
gtourkas
2021-03-21 18:09:47 +02:00
parent d61e3712f5
commit 5dbd63751f
3 changed files with 112 additions and 113 deletions

View File

@ -27,7 +27,7 @@ test:
# gofumports and gci all go files # gofumports and gci all go files
fmt: fmt:
find . -name '*.go' -not -wholename './vendor/*' | while read -r file; do gofumports -w "$$file"; done find . -name '*.go' -not -wholename './vendor/*' | while read -r file; do gofumpt -w "$$file"; done
gci -w -local github.com/nikoksr/notify . gci -w -local github.com/nikoksr/notify .
.PHONY: fmt .PHONY: fmt

View File

@ -1,20 +1,19 @@
package wechat package wechat
import ( import (
"context" "context"
"fmt" "fmt"
"net/http"
"sync"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/silenceper/wechat/v2" "github.com/silenceper/wechat/v2"
"github.com/silenceper/wechat/v2/cache" "github.com/silenceper/wechat/v2/cache"
"github.com/silenceper/wechat/v2/officialaccount/config" "github.com/silenceper/wechat/v2/officialaccount/config"
"github.com/silenceper/wechat/v2/officialaccount/message" "github.com/silenceper/wechat/v2/officialaccount/message"
"github.com/silenceper/wechat/v2/util" "github.com/silenceper/wechat/v2/util"
"net/http"
"sync"
) )
// Config is the Service configuration. // Config is the Service configuration.
type Config struct { type Config struct {
AppID string AppID string
@ -37,7 +36,6 @@ type Service struct {
// New returns a new instance of a WeChat notification service. // New returns a new instance of a WeChat notification service.
func New(cfg Config) *Service { func New(cfg Config) *Service {
wc := wechat.NewWechat() wc := wechat.NewWechat()
wcCfg := &config.Config{ wcCfg := &config.Config{
AppID: cfg.AppID, AppID: cfg.AppID,
@ -66,7 +64,6 @@ func (s *Service) WaitForOneOffVerification(
addr string, addr string,
devMode bool, devMode bool,
callback func(r *http.Request, verified bool)) error { callback func(r *http.Request, verified bool)) error {
srv := &http.Server{Addr: addr} srv := &http.Server{Addr: addr}
verificationDone := &sync.WaitGroup{} verificationDone := &sync.WaitGroup{}
verificationDone.Add(1) verificationDone.Add(1)
@ -79,11 +76,12 @@ func (s *Service) WaitForOneOffVerification(
if callback != nil { if callback != nil {
callback(r, true) callback(r, true)
} }
w.Write([]byte(echoStr)) _, _ = w.Write([]byte(echoStr))
// verification done; dev mode // verification done; dev mode
verificationDone.Done() verificationDone.Done()
return return
} else { }
// perform signature check // perform signature check
timestamp := query.Get("timestamp") timestamp := query.Get("timestamp")
nonce := query.Get("nonce") nonce := query.Get("nonce")
@ -93,12 +91,12 @@ func (s *Service) WaitForOneOffVerification(
if callback != nil { if callback != nil {
callback(r, true) callback(r, true)
} }
w.Write([]byte(echoStr)) _, _ = w.Write([]byte(echoStr))
// verification done; prod mode // verification done; prod mode
verificationDone.Done() verificationDone.Done()
return return
} }
}
// verification not done (keep waiting) // verification not done (keep waiting)
if callback != nil { if callback != nil {
callback(r, false) callback(r, false)
@ -108,14 +106,16 @@ func (s *Service) WaitForOneOffVerification(
var err error var err error
go func() { go func() {
if innerErr := srv.ListenAndServe(); innerErr != http.ErrServerClosed { if innerErr := srv.ListenAndServe(); innerErr != http.ErrServerClosed {
err = errors.Wrapf(innerErr, "failed to wait for verification at '%s'", addr) err = errors.Wrapf(innerErr, "failed to start verification listener '%s'", addr)
} }
}() }()
// wait until verification is done and shutdown the server // wait until verification is done and shutdown the server
verificationDone.Wait() verificationDone.Wait()
srv.Shutdown(context.TODO()) if innerErr := srv.Shutdown(context.TODO()); innerErr != nil {
err = errors.Wrap(innerErr, "failed to shutdown verification listerer")
}
return err return err
} }
@ -128,7 +128,6 @@ func (s *Service) AddReceivers(userIDs ...string) {
// Send takes a message subject and a message content and sends them to all previously set users. // Send takes a message subject and a message content and sends them to all previously set users.
func (s *Service) Send(ctx context.Context, subject, content string) error { func (s *Service) Send(ctx context.Context, subject, content string) error {
for _, userID := range s.userIDs { for _, userID := range s.userIDs {
select { select {
case <-ctx.Done(): case <-ctx.Done():
@ -143,4 +142,3 @@ func (s *Service) Send(ctx context.Context, subject, content string) error {
return nil return nil
} }

View File

@ -2,10 +2,11 @@ package wechat
import ( import (
"context" "context"
"github.com/silenceper/wechat/v2/officialaccount/message"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"testing" "testing"
"github.com/pkg/errors"
"github.com/silenceper/wechat/v2/officialaccount/message"
"github.com/stretchr/testify/require"
) )
func TestAddReceivers(t *testing.T) { func TestAddReceivers(t *testing.T) {