Skip to content

Commit

Permalink
feat: rename variable, change default config values, linting
Browse files Browse the repository at this point in the history
  • Loading branch information
fadyat committed Nov 12, 2023
1 parent bec0120 commit 2530d09
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 21 deletions.
68 changes: 68 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 6 additions & 8 deletions cmd/stress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func parseRecipients(recipients string) []string {
}
}

return parsed
return rcpt
}

// following code is a stress test for erida
Expand All @@ -42,6 +42,7 @@ func main() {
var (
wg sync.WaitGroup
reqNumber = 0
batch = 10
interval, err = time.ParseDuration(secondsInterval)
)
if err != nil {
Expand All @@ -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
}
Expand All @@ -69,19 +71,15 @@ 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) {
defer wg.Done()
issueRequest(rn)
}(reqNumber + i)
}

reqNumber += requests
}

func issueRequest(reqNumber int) {
Expand Down
2 changes: 1 addition & 1 deletion internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
15 changes: 11 additions & 4 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,30 @@ 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 {
return !c.StartTLS
}

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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/mail_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 5 additions & 5 deletions internal/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion internal/slack_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 2530d09

Please sign in to comment.