Skip to content

Commit

Permalink
Merge pull request #8 from fond-of-vertigo/refactor/interfaces-linter
Browse files Browse the repository at this point in the history
Add golangci-lint
  • Loading branch information
Coffeeri authored Jun 7, 2023
2 parents 3c863e5 + 4bf45f2 commit ac0dd9e
Show file tree
Hide file tree
Showing 17 changed files with 515 additions and 526 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
name: Integration tests
on:
push:
branches:
- pull_request
- main
pull_request:
workflow_dispatch:

env:
NATS_SERVER_URL: "nats://127.0.0.1:4222"
jobs:
integration-tests:
runs-on: ubuntu-latest
steps:
- name: Setup GO
uses: actions/setup-go@v4
with:
go-version: "stable"
- name: Check out Go client code
uses: actions/checkout@v3
- name: Install NATS server
run: |
curl -sL https://github.com/nats-io/nats-server/releases/download/v2.9.15/nats-server-v2.9.15-linux-amd64.tar.gz | tar xzvf -
cd nats-server-v2.9.15-linux-amd64 && ./nats-server -p 4222 -js &
- name: Run unit and integration tests
name: Continuous Integration
on:
push:
branches:
- pull_request
- main
pull_request:
workflow_dispatch:

env:
NATS_SERVER_URL: "nats://127.0.0.1:4222"
jobs:
ci:
runs-on: ubuntu-latest
steps:
- name: Setup GO
uses: actions/setup-go@v4
with:
go-version: "stable"
- name: Check out Go client code
uses: actions/checkout@v3
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
skip-pkg-cache: true
- name: Install NATS server
run: |
curl -sL https://github.com/nats-io/nats-server/releases/download/v2.9.15/nats-server-v2.9.15-linux-amd64.tar.gz | tar xzvf -
cd nats-server-v2.9.15-linux-amd64 && ./nats-server -p 4222 -js &
- name: Run unit and integration tests
run: make test-all
51 changes: 51 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
run:
deadline: 5m

output:
sort-results: true

linters:
enable:
- asciicheck
- dogsled
- durationcheck
- errcheck
- exportloopref
- gci
- gofumpt
- goimports
- gosec
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- nilerr
- prealloc
- predeclared
- revive
- revive
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused

issues:
max-same-issues: 0
exclude-use-default: false
exclude:
- should have a package comment


linters-settings:
gofumpt:
extra-rules: true
simplify: true

gci:
sections:
- standard
- default
- prefix(github.com/fond-of-vertigo/vnats)
56 changes: 24 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ package main
import (
"encoding/json"
"github.com/fond-of-vertigo/vnats"
"github.com/fond-of-vertigo/logger"
"log"
"fmt"
"time"
)
Expand All @@ -34,29 +34,26 @@ type Product struct {
Price string
LastUpdated time.Time
}

var log = logger.New(logger.LvlDebug)

// Define NATS server/ cluster
var server = []string{"nats://ruser:T0pS3cr3t@localhost:4222"}

func main() {
// Establish connection to NATS server
conn, err := vnats.Connect(server, log)
conn, err := vnats.Connect(server, vnats.WithLogger(log.Printf))
if err != nil {
log.Errorf(err.Error())
log.Fatal(err.Error())
}
// Close NATS connection deferred
defer func(conn vnats.Connection) {
defer func(conn *vnats.Connection) {
if err := conn.Close(); err != nil {
log.Errorf("NATS connection could not be closed: %v", err)
log.Fatalf("NATS connection could not be closed: %v", err)
}
}(conn)

// Create publisher bound to stream `PRODUCTS`
pub, err := conn.NewPublisher(vnats.NewPublisherArgs{StreamName: "PRODUCTS"})
pub, err := conn.NewPublisher(vnats.PublisherArgs{StreamName: "PRODUCTS"})
if err != nil {
log.Errorf("Could not create publisher: %v", err)
log.Fatalf("Could not create publisher: %v", err)
}

p := Product{
Expand All @@ -74,12 +71,9 @@ func main() {
// Publish message to stream `PRODUCTS.PRICES` with a context bound, unique message ID
// msgID is used for deduplication
msgID := fmt.Sprintf("%s-%s", p.Name, p.LastUpdated)
if err := pub.Publish(&vnats.OutMsg{
Subject: "PRODUCTS.PRICES",
MsgID: msgID,
Data: productToBytes,
}); err != nil {
log.Errorf("Could not publish %v: %v", p, err)
msg := vnats.NewMsg("PRODUCTS.PRICES", msgID, productToBytes)
if err := pub.Publish(msg); err != nil {
log.Fatalf("Could not publish %v: %v", p, err)
}
}
```
Expand All @@ -103,7 +97,7 @@ package main
import (
"encoding/json"
"github.com/fond-of-vertigo/vnats"
"github.com/fond-of-vertigo/logger"
"log"
"time"
"os"
"os/signal"
Expand All @@ -115,51 +109,49 @@ type Product struct {
LastUpdated time.Time
}

var log = logger.New(logger.LvlDebug)

// Define NATS server/ cluster
var server = []string{"nats://ruser:T0pS3cr3t@localhost:4222"}

func main() {
// Establish connection to NATS server
conn, err := vnats.Connect(server, log)
conn, err := vnats.Connect(server, vnats.WithLogger(log.Printf))
if err != nil {
log.Errorf(err.Error())
log.Fatal(err.Error())
}

// Unsubscribe to all open subscriptions and close NATS connection deferred
defer func(conn vnats.Connection) {
defer func(conn *vnats.Connection) {
if err := conn.Close(); err != nil {
log.Errorf("NATS connection could not be closed: %v", err)
log.Fatalf("NATS connection could not be closed: %v", err)
}
}(conn)

// Create Pull-Subscriber bound to consumer `EXAMPLE_CONSUMER`
// and the subject `PRODUCTS.PRICES`
sub, err := conn.NewSubscriber(vnats.NewSubscriberArgs{
sub, err := conn.NewSubscriber(vnats.SubscriberArgs{
ConsumerName: "EXAMPLE_CONSUMER",
Subject: "PRODUCTS.PRICES",
})
if err != nil {
log.Errorf("Could not create subscriber: %v", err)
log.Fatalf("Could not create subscriber: %v", err)
}

// Subscribe and specify messageHandler
if err := sub.Subscribe(msgHandler); err != nil {
log.Errorf(err.Error())
// Start subscribing with specify messageHandler
if err := sub.Start(msgHandler); err != nil {
log.Fatalf(err.Error())
}

// Wait for stop signal (e.g. ctrl-C)
waitForStopSignal()
}

// MsgHandler returns the data in a slice of bytes inside the InMsg struct.
func msgHandler(msg vnats.InMsg) error {
// msgHandler returns the data in a slice of bytes inside the Msg struct.
func msgHandler(msg vnats.Msg) error {
var p Product
if err := json.Unmarshal(msg.Data(), &p); err != nil {
if err := json.Unmarshal(msg.Data, &p); err != nil {
return err
}
log.Debugf("Received product: %v", p)
log.Printf("Received product: %v", p)
return nil
}

Expand Down
Loading

0 comments on commit ac0dd9e

Please sign in to comment.