Skip to content

Commit

Permalink
🛠️ add golang ci lint and fix lint errors (#56)
Browse files Browse the repository at this point in the history
* add golang ci lint

* fix golang-ci output

* update golang ci

* add golangci config file

* fix linting issue
  • Loading branch information
ludusrusso authored Apr 21, 2021
1 parent 6358751 commit c298278
Show file tree
Hide file tree
Showing 16 changed files with 179 additions and 87 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/golang.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- master
pull_request:
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.29
51 changes: 51 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
linters-settings:
golint:
min-confidence: 0
goimports:
local-prefixes: github.com/clastix/capsule
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 2
linters:
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- errcheck
- goconst
- gocritic
- gofmt
- goimports
- golint
- goprintffuncname
- gosimple
- govet
- ineffassign
- misspell
- nolintlint
- rowserrcheck
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace

issues:
exclude:
- Using the variable on range scope .* in function literal

service:
golangci-lint-version: 1.33.x

run:
skip-files:
- "zz_.*\\.go$"
12 changes: 6 additions & 6 deletions cmd/api/admin_api/api.go → cmd/api/adminapi/api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package admin_api
package adminapi

import (
"context"
Expand All @@ -11,11 +11,11 @@ import (
"kannon.gyozatech.dev/internal/domains"
)

type adminApiService struct {
type adminAPIService struct {
dm domains.DomainManager
}

func (s *adminApiService) GetDomains(ctx context.Context, in *emptypb.Empty) (*pb.GetDomainsResponse, error) {
func (s *adminAPIService) GetDomains(ctx context.Context, in *emptypb.Empty) (*pb.GetDomainsResponse, error) {
domains, err := s.dm.GetAllDomains()
if err != nil {
return nil, err
Expand All @@ -28,7 +28,7 @@ func (s *adminApiService) GetDomains(ctx context.Context, in *emptypb.Empty) (*p
return &res, nil
}

func (s *adminApiService) CreateDomain(ctx context.Context, in *pb.CreateDomainRequest) (*pb.Domain, error) {
func (s *adminAPIService) CreateDomain(ctx context.Context, in *pb.CreateDomainRequest) (*pb.Domain, error) {
domain, err := s.dm.CreateDomain(in.Domain)
if err != nil {
return nil, err
Expand All @@ -37,7 +37,7 @@ func (s *adminApiService) CreateDomain(ctx context.Context, in *pb.CreateDomainR
return dbDomainToProtoDomain(domain), nil
}

func (s *adminApiService) RegenerateDomainKey(ctx context.Context, in *pb.RegenerateDomainKeyRequest) (*pb.Domain, error) {
func (s *adminAPIService) RegenerateDomainKey(ctx context.Context, in *pb.RegenerateDomainKeyRequest) (*pb.Domain, error) {
return nil, nil
}

Expand All @@ -47,7 +47,7 @@ func CreateAdminAPIService(db *sql.DB) (pb.ApiServer, error) {
if err != nil {
return nil, err
}
api := adminApiService{
api := adminAPIService{
dm: dm,
}

Expand Down
17 changes: 8 additions & 9 deletions cmd/api/mail_api/mailer.go → cmd/api/mailapi/mailer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mail_api
package mailapi

import (
"context"
Expand All @@ -20,13 +20,13 @@ import (
"kannon.gyozatech.dev/internal/templates"
)

type mailApiService struct {
type mailAPIService struct {
domains domains.DomainManager
templates templates.Manager
sendingPoll pool.SendingPoolManager
}

func (s mailApiService) SendHTML(ctx context.Context, in *pb.SendHTMLRequest) (*pb.SendResponse, error) {
func (s mailAPIService) SendHTML(ctx context.Context, in *pb.SendHTMLRequest) (*pb.SendResponse, error) {
domain, ok := s.getCallDomainFromContext(ctx)
if !ok {
logrus.Errorf("invalid login\n")
Expand Down Expand Up @@ -59,7 +59,7 @@ func (s mailApiService) SendHTML(ctx context.Context, in *pb.SendHTMLRequest) (*
return &response, nil
}

func (s mailApiService) SendTemplate(ctx context.Context, in *pb.SendTemplateRequest) (*pb.SendResponse, error) {
func (s mailAPIService) SendTemplate(ctx context.Context, in *pb.SendTemplateRequest) (*pb.SendResponse, error) {
domain, ok := s.getCallDomainFromContext(ctx)
if !ok {
logrus.Errorf("invalid login\n")
Expand Down Expand Up @@ -92,11 +92,11 @@ func (s mailApiService) SendTemplate(ctx context.Context, in *pb.SendTemplateReq
return &response, nil
}

func (s mailApiService) Close() error {
func (s mailAPIService) Close() error {
return s.domains.Close()
}

func (s mailApiService) getCallDomainFromContext(ctx context.Context) (sqlc.Domain, bool) {
func (s mailAPIService) getCallDomainFromContext(ctx context.Context) (sqlc.Domain, bool) {
m, ok := metadata.FromIncomingContext(ctx)
if !ok {
logrus.Debugf("Cannot find metatada\n")
Expand Down Expand Up @@ -138,10 +138,9 @@ func (s mailApiService) getCallDomainFromContext(ctx context.Context) (sqlc.Doma
}

return domain, true

}

func NewMailApiService(dbi *sql.DB) (pb.MailerServer, error) {
func NewMailAPIService(dbi *sql.DB) (pb.MailerServer, error) {
domainsCli, err := domains.NewDomainManager(dbi)
if err != nil {
return nil, err
Expand All @@ -157,7 +156,7 @@ func NewMailApiService(dbi *sql.DB) (pb.MailerServer, error) {
return nil, err
}

return &mailApiService{
return &mailAPIService{
domains: domainsCli,
sendingPoll: sendingPoolCli,
templates: templates,
Expand Down
26 changes: 13 additions & 13 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,51 @@ import (
"sync"

"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"kannon.gyozatech.dev/cmd/api/admin_api"
"kannon.gyozatech.dev/cmd/api/mail_api"
"kannon.gyozatech.dev/cmd/api/adminapi"
"kannon.gyozatech.dev/cmd/api/mailapi"
"kannon.gyozatech.dev/generated/pb"
)

func main() {
log.SetFormatter(&log.JSONFormatter{})
runGrpcServer()
if err := runGrpcServer(); err != nil {
panic(err.Error())
}
}

func runGrpcServer() error {
godotenv.Load()
_ = godotenv.Load()

dbi, err := sql.Open("postgres", os.Getenv("DB_CONN"))
if err != nil {
panic(err)
}
defer dbi.Close()

adminApiService, err := admin_api.CreateAdminAPIService(dbi)
adminAPIService, err := adminapi.CreateAdminAPIService(dbi)
if err != nil {
logrus.Fatalf("Cannot create Admin API service: %v\n", err)
return err
return fmt.Errorf("cannot create Admin API service: %w", err)
}

mailApiService, err := mail_api.NewMailApiService(dbi)
mailAPIService, err := mailapi.NewMailAPIService(dbi)
if err != nil {
logrus.Fatalf("Cannot create Mailer API service: %v\n", err)
return fmt.Errorf("cannot create Mailer API service: %w", err)
}

wg := sync.WaitGroup{}
wg.Add(2)

go func() {
err := startApiServer(50051, adminApiService)
err := startAPIServer(50051, adminAPIService)
if err != nil {
panic("Cannot run api server")
}
}()

go func() {
err := startMailerServer(50052, mailApiService)
err := startMailerServer(50052, mailAPIService)
if err != nil {
panic("Cannot run mailer server")
}
Expand All @@ -63,7 +63,7 @@ func runGrpcServer() error {
return nil
}

func startApiServer(port uint16, srv pb.ApiServer) error {
func startAPIServer(port uint16, srv pb.ApiServer) error {
addr := fmt.Sprintf("0.0.0.0:%d", port)
lis, err := net.Listen("tcp", addr)
if err != nil {
Expand Down
10 changes: 7 additions & 3 deletions cmd/dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type appConfig struct {
}

func main() {
godotenv.Load()
_ = godotenv.Load()

var config appConfig
err := envconfig.Process("app", &config)
Expand Down Expand Up @@ -121,7 +121,9 @@ func handleErrors(mgr *jsm.Manager) {
} else {
logrus.Printf("[🛑 bump] %v %v - %v", errMsg.Email, errMsg.MessageId, errMsg.Msg)
}
msg.Ack()
if err := msg.Ack(); err != nil {
logrus.Errorf("Cannot hack msg to nats: %v\n", err)
}
}
}

Expand All @@ -142,6 +144,8 @@ func handleDelivereds(mgr *jsm.Manager) {
} else {
logrus.Printf("[🚀 delivered] %v %v", deliveredMsg.Email, deliveredMsg.MessageId)
}
msg.Ack()
if err := msg.Ack(); err != nil {
logrus.Errorf("Cannot hack msg to nats: %v\n", err)
}
}
}
12 changes: 6 additions & 6 deletions cmd/sender/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@ import (
"context"
"flag"

"github.com/golang/protobuf/proto"
"github.com/joho/godotenv"
"github.com/nats-io/jsm.go"
"github.com/nats-io/nats.go"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
"kannon.gyozatech.dev/generated/pb"
"kannon.gyozatech.dev/internal/smtp"
)

func main() {
godotenv.Load()
senderHost := flag.String("sender-host", "sender.kannon.io", "Sender hostname for SMTP presentation")
natsUrl := flag.String("nasts-url", "nats", "Nats url connection")
natsURL := flag.String("nasts-url", "nats", "Nats url connection")
maxSendingJobs := flag.Uint("max-sending-jobs", 100, "Max Parallel Job for sending")

flag.Parse()

nc, err := nats.Connect(*natsUrl, nats.UseOldRequestStyle())
nc, err := nats.Connect(*natsURL, nats.UseOldRequestStyle())
if err != nil {
logrus.Fatalf("Cannot connect to nats: %v\n", err)
}
Expand Down Expand Up @@ -55,7 +53,9 @@ func handleSend(sender smtp.Sender, con *jsm.Consumer, nc *nats.Conn, maxParalle
if err != nil {
logrus.Errorf("error in handling message: %v\n", err.Error())
}
msg.Ack()
if err := msg.Ack(); err != nil {
logrus.Errorf("cannot hack message: %v\n", err.Error())
}
<-ch
}()
}
Expand Down
4 changes: 2 additions & 2 deletions generated/sqlc/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestMain(m *testing.M) {

if err = pool.Retry(func() error {
var err error
db, err = initDb(resource.GetPort("5432/tcp"))
db, err = initDB(resource.GetPort("5432/tcp"))
if err != nil {
logrus.Warnf("connection error: %v", err)
return err
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestTemplates(t *testing.T) {
assert.Nil(t, err)
}

func initDb(dbPort string) (*sql.DB, error) {
func initDB(dbPort string) (*sql.DB, error) {
url := fmt.Sprintf("postgresql://test:test@localhost:%v/test", dbPort)
db, err := conn(url)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/dkim/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,5 @@ func decodeKey(dkimPrivateKey string) (*rsa.PrivateKey, error) {
if err != nil {
return nil, err
}
rsak, err := x509.ParsePKCS1PrivateKey(dkimPrivateKeyInBytes)
return rsak, nil
return x509.ParsePKCS1PrivateKey(dkimPrivateKeyInBytes)
}
26 changes: 24 additions & 2 deletions internal/mailbuilder/mailbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"bytes"
"context"
"database/sql"
"time"

"github.com/sirupsen/logrus"
"gopkg.in/mail.v2"
"kannon.gyozatech.dev/generated/pb"
"kannon.gyozatech.dev/generated/sqlc"
"kannon.gyozatech.dev/internal/dkim"
Expand Down Expand Up @@ -60,8 +63,8 @@ func (m *mailBuilder) PerpareForSend(email sqlc.SendingPoolEmail) (pb.EmailToSen

func prepareMessage(sender pool.Sender, subject string, to string, messageID string, html string, baseHeaders headers) ([]byte, error) {
emailMessageID := buildEmailMessageID(to, messageID)
headers := buildHeaders(subject, sender, to, messageID, emailMessageID, baseHeaders)
return renderMsg(html, sender.Email, to, headers)
h := buildHeaders(subject, sender, to, messageID, emailMessageID, baseHeaders)
return renderMsg(html, h)
}

func signMessage(domain string, dkimPrivateKey string, msg []byte) ([]byte, error) {
Expand All @@ -74,3 +77,22 @@ func signMessage(domain string, dkimPrivateKey string, msg []byte) ([]byte, erro

return dkim.SignMessage(signData, bytes.NewReader(msg))
}

// renderMsg render a MsgPayload to an SMTP message
func renderMsg(html string, headers headers) ([]byte, error) {
msg := mail.NewMessage()

for key, value := range headers {
msg.SetHeader(key, value)
}
msg.SetDateHeader("Date", time.Now())
msg.SetBody("text/html", html)

var buff bytes.Buffer
if _, err := msg.WriteTo(&buff); err != nil {
logrus.Warnf("🤢 Error writing message: %v\n", err)
return nil, err
}

return buff.Bytes(), nil
}
Loading

0 comments on commit c298278

Please sign in to comment.