diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..182c69c --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,68 @@ +output: + print-issued-lines: true + +run: + timeout: 5m + allow-parallel-runners: true + concurrency: 2 + issue-exit-code: 1 + skip-dirs: + - vendor + +linters-settings: + govet: + check-shadowing: true + golint: + min-confidence: 0.1 + maligned: + suggest-new: true + goconst: + min-len: 2 + min-occurrences: 2 + misspell: + locale: US + lll: + line-length: 140 + gocritic: + enabled-tags: + - performance + - style + - experimental + +linters: + fast: false + disable-all: true + enable: + - megacheck + - sqlclosecheck + - govet + - unconvert + - megacheck + - gas + - dupl + - misspell + - unused + - typecheck + - ineffassign + - stylecheck + - gochecknoinits + - exportloopref + - gocritic + - nakedret + - gosimple + - prealloc + - gosec + +issues: + exclude-rules: + - text: "at least one file in a package should have a package comment" + linters: + - stylecheck + - text: "should have a package comment, unless it's in another file for this package" + linters: + - golint + - path: _test\.go + linters: + - gosec + - dupl + exclude-use-default: false \ No newline at end of file diff --git a/Makefile b/Makefile index b12863b..59010bb 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,11 @@ endif include helm/Makefile +lint: + @golangci-lint run --issues-exit-code 1 \ + --print-issued-lines=true \ + --config .golangci.yml ./... + run: @go run ./cmd/erida/main.go diff --git a/cmd/stress/main.go b/cmd/stress/main.go index 463eb5a..35cab9c 100644 --- a/cmd/stress/main.go +++ b/cmd/stress/main.go @@ -31,7 +31,7 @@ func parseRecipients(recipients string) []string { } } - return parsed + return rcpt } // following code is a stress test for erida @@ -42,6 +42,7 @@ func main() { var ( wg sync.WaitGroup reqNumber = 0 + batch = 10 interval, err = time.ParseDuration(secondsInterval) ) if err != nil { @@ -54,7 +55,8 @@ func main() { for { select { case <-time.After(interval * time.Second): - issueRequestInParallel(&wg, reqNumber) + issueRequestInParallel(&wg, reqNumber, batch) + reqNumber += batch case <-ctx.Done(): return } @@ -69,10 +71,8 @@ func main() { wg.Wait() } -func issueRequestInParallel(wg *sync.WaitGroup, reqNumber int) { - var requests = 10 - - for i := 0; i < requests; i++ { +func issueRequestInParallel(wg *sync.WaitGroup, reqNumber, batch int) { + for i := 0; i < batch; i++ { wg.Add(1) go func(rn int) { @@ -80,8 +80,6 @@ func issueRequestInParallel(wg *sync.WaitGroup, reqNumber int) { issueRequest(rn) }(reqNumber + i) } - - reqNumber += requests } func issueRequest(reqNumber int) { diff --git a/internal/client.go b/internal/client.go index 5d09552..4a7d8e7 100644 --- a/internal/client.go +++ b/internal/client.go @@ -48,7 +48,7 @@ func takeUsernames(recipients []string, recipientType string) []string { return usernames } -func convertToRecipientWay(username string, recipientType string) string { +func convertToRecipientWay(username, recipientType string) string { if _, ok := operators[recipientType]; !ok { return username } diff --git a/internal/config.go b/internal/config.go index 5b2912e..f76ccde 100644 --- a/internal/config.go +++ b/internal/config.go @@ -3,15 +3,22 @@ package internal import "fmt" type Config struct { - RelayHost string `env:"SERVER_HOST" env-default:"localhost"` - RelayPort string `env:"SERVER_PORT" env-default:"25"` + + // ServerHost and ServerPort are used for listening incoming connections. + // By default, it allows the server to respond to requests from any + // available network interface. + ServerHost string `env:"SERVER_HOST" env-default:"0.0.0.0"` + ServerPort string `env:"SERVER_PORT" env-default:"25"` Host string `env:"SMTP_HOST" env-default:"smtp.gmail.com"` Port string `env:"SMTP_PORT" env-default:"587"` User string `env:"SMTP_USER"` Pass string `env:"SMTP_PASS"` StartTLS bool `env:"SMTP_TLS" env-default:"true"` Auth bool `env:"SMTP_AUTH" env-default:"true"` - SlackToken string `env:"SLACK_TOKEN" env-required:"true"` + + // Slack token for sending notifications. + // https://api.slack.com/authentication/token-types#granular_bot + SlackToken string `env:"SLACK_TOKEN"` } func (c *Config) Insecure() bool { @@ -19,7 +26,7 @@ func (c *Config) Insecure() bool { } func (c *Config) RelayAddr() string { - return fmt.Sprintf("%s:%s", c.RelayHost, c.RelayPort) + return fmt.Sprintf("%s:%s", c.ServerHost, c.ServerPort) } func (c *Config) SMTPAddr() string { diff --git a/internal/mail_client.go b/internal/mail_client.go index 7210b26..80f2bf0 100644 --- a/internal/mail_client.go +++ b/internal/mail_client.go @@ -59,7 +59,7 @@ func (c *mailClient) startTLS() error { } if err := c.StartTLS(&tls.Config{ - InsecureSkipVerify: c.cfg.Insecure(), + InsecureSkipVerify: c.cfg.Insecure(), // #nosec G402 ServerName: c.cfg.Host, }); err != nil { return fmt.Errorf("failed to start tls: %w", err) diff --git a/internal/server.go b/internal/server.go index f4598e5..630a552 100644 --- a/internal/server.go +++ b/internal/server.go @@ -97,7 +97,7 @@ func NewServer( s.ReadTimeout = 10 * time.Second s.WriteTimeout = 10 * time.Second s.Addr = cfg.RelayAddr() - s.Domain = cfg.RelayHost + s.Domain = cfg.ServerHost s.AllowInsecureAuth = cfg.Insecure() return s } diff --git a/internal/server_test.go b/internal/server_test.go index 7609ac8..becc364 100644 --- a/internal/server_test.go +++ b/internal/server_test.go @@ -29,10 +29,10 @@ func TestServer(t *testing.T) { require.NoError(t, mockedServer.Start()) server := NewServer(&Config{ - RelayHost: relayHost, - RelayPort: strconv.Itoa(relayPort), - Host: host, - Port: strconv.Itoa(port), + ServerHost: relayHost, + ServerPort: strconv.Itoa(relayPort), + Host: host, + Port: strconv.Itoa(port), // Mocked server doesn't support TLS and AUTH, disable them. // https://github.com/mocktools/go-smtp-mock/issues/76 @@ -76,7 +76,7 @@ World! assert.Equal( t, - strings.Replace(string(body), "\n", "\r\n", -1), + strings.ReplaceAll(string(body), "\n", "\r\n"), msg.MsgRequest(), ) assert.Equal(t, "250 Received", msg.MsgResponse()) diff --git a/internal/slack_client.go b/internal/slack_client.go index c84b1c3..9e815f9 100644 --- a/internal/slack_client.go +++ b/internal/slack_client.go @@ -76,7 +76,7 @@ func (c *slackClient) sendMessageConcurrent( wg.Wait() }() - var errors []error + var errors = make([]error, 0) for err := range errorsChan { errors = append(errors, err) }