Skip to content

Commit 6cda547

Browse files
chore: fix linting
1 parent 401eaf5 commit 6cda547

32 files changed

+270
-300
lines changed

.golangci.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,11 @@ issues:
4141
- EXC0014
4242
- EXC0015
4343
run:
44-
go: "1.14"
44+
go: "1.18"
45+
linters-settings:
46+
tagliatelle:
47+
case:
48+
use-field-name: true
49+
rules:
50+
json: snake
51+
yaml: camel

cmd/server/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
httptransport "github.com/speakeasy-api/speakeasy-example-rest-service-go/internal/transport/http"
1414
"github.com/speakeasy-api/speakeasy-example-rest-service-go/internal/users"
1515
"github.com/speakeasy-api/speakeasy-example-rest-service-go/internal/users/store"
16-
1716
"go.uber.org/zap"
1817
)
1918

config/config-docker.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
http:
22
port: "8080"
33
psql:
4-
username: guest
5-
password: guest
6-
host: postgres
7-
port: 5432
8-
database: speakeasy
4+
dsn: "postgresql://guest:guest@postgres:5432/speakeasy?sslmode=disable"

config/config.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
http:
22
port: "8080"
33
psql:
4-
username: guest
5-
password: guest
6-
host: localhost
7-
port: 5432
8-
database: speakeasy
4+
dsn: "postgresql://guest:guest@localhost:5432/speakeasy?sslmode=disable"

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.18
44

55
require (
66
github.com/AlekSi/pointer v1.2.0
7+
github.com/cenkalti/backoff/v4 v4.1.2
78
github.com/golang-migrate/migrate/v4 v4.15.1
89
github.com/golang/mock v1.6.0
910
github.com/gorilla/mux v1.8.0
@@ -24,7 +25,6 @@ require (
2425
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
2526
github.com/Microsoft/go-winio v0.5.2 // indirect
2627
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
27-
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
2828
github.com/containerd/continuity v0.2.2 // indirect
2929
github.com/davecgh/go-spew v1.1.1 // indirect
3030
github.com/docker/cli v20.10.14+incompatible // indirect

internal/config/config.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ package config
22

33
import "github.com/speakeasy-api/speakeasy-example-rest-service-go/internal/core/config"
44

5-
// Config represents the configuration of our application
5+
// Config represents the configuration of our application.
66
type Config struct {
77
config.AppConfig `yaml:",inline"`
88
}
99

10-
// Load loads the configuration from the config/config.yaml file
10+
// Load loads the configuration from the config/config.yaml file.
1111
func Load() (*Config, error) {
1212
cfg := &Config{}
1313

14-
err := config.Load(cfg)
15-
if err != nil {
14+
if err := config.Load(cfg); err != nil {
1615
return nil, err
1716
}
1817

internal/core/app/app.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,59 @@ package app
22

33
import (
44
"context"
5+
"log"
56
"os"
67
"os/signal"
78
"sync"
89
"syscall"
910

1011
"github.com/speakeasy-api/speakeasy-example-rest-service-go/internal/core/logging"
1112
"github.com/speakeasy-api/speakeasy-example-rest-service-go/internal/core/tracing"
12-
1313
"go.uber.org/zap"
1414
)
1515

16+
// Listener represents a type that can listen for incoming connections.
1617
type Listener interface {
1718
Listen(context.Context) error
1819
}
1920

21+
// OnShutdownFunc is a function that is called when the app is shutdown.
2022
type OnShutdownFunc func()
2123

24+
// App represents the application run by this service.
2225
type App struct {
2326
Name string
2427
shutdownFuncs []OnShutdownFunc
2528
}
2629

30+
// OnStart is a function that is called when the app is started.
2731
type OnStart func(context.Context, *App) ([]Listener, error)
2832

33+
// Start starts the application.
2934
func Start(onStart OnStart) {
3035
ctx := context.Background()
3136

3237
a := &App{
3338
Name: "test-app", // TODO determine how to configure this
3439
}
3540
a.OnShutdown(func() {
36-
logging.Sync(ctx)
41+
if err := logging.Sync(ctx); err != nil {
42+
log.Printf("failed to sync logging: %v", err)
43+
}
3744
})
3845

3946
logging.From(ctx).Info("app starting...")
4047

41-
tracing.EnableTracing(ctx, a.Name, a)
48+
if err := tracing.EnableTracing(ctx, a.Name, a); err != nil {
49+
logging.From(ctx).Fatal("failed to enable tracing", zap.Error(err))
50+
}
4251

4352
listeners, err := onStart(ctx, a)
4453
if err != nil {
4554
logging.From(ctx).Fatal("failed to start app", zap.Error(err))
4655
}
4756

48-
c := make(chan os.Signal)
57+
c := make(chan os.Signal, 1)
4958
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
5059
go func() {
5160
<-c
@@ -73,6 +82,7 @@ func Start(onStart OnStart) {
7382
shutdown(ctx, a)
7483
}
7584

85+
// OnShutdown registers a function that is called when the app is shutdown.
7686
func (a *App) OnShutdown(onShutdown func()) {
7787
a.shutdownFuncs = append([]OnShutdownFunc{onShutdown}, a.shutdownFuncs...)
7888
}

internal/core/config/config.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@ import (
66
"github.com/speakeasy-api/speakeasy-example-rest-service-go/internal/core/drivers/psql"
77
"github.com/speakeasy-api/speakeasy-example-rest-service-go/internal/core/errors"
88
"github.com/speakeasy-api/speakeasy-example-rest-service-go/internal/core/listeners/http"
9-
109
"gopkg.in/yaml.v2"
1110
)
1211

1312
const (
14-
ErrRead = errors.Error("failed to read file")
13+
// ErrRead is returned when we cannot read the config file.
14+
ErrRead = errors.Error("failed to read file")
15+
// ErrUnmarshal is returned when we cannot unmarshal the config file.
1516
ErrUnmarshal = errors.Error("failed to unmarshal file")
1617
)
1718

19+
// AppConfig represents the configuration of our application.
1820
type AppConfig struct {
1921
HTTP http.Config `yaml:"http"`
2022
PSQL psql.Config `yaml:"psql"`
2123
}
2224

25+
// Load loads the configuration from a yaml file on disk.
2326
func Load(cfg interface{}) error {
2427
data, err := ioutil.ReadFile("config/config.yaml") // TODO support different environments
2528
if err != nil {

internal/core/drivers/psql/migrations.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@ package psql
33
import (
44
"context"
55

6-
"github.com/speakeasy-api/speakeasy-example-rest-service-go/internal/core/errors"
7-
"github.com/speakeasy-api/speakeasy-example-rest-service-go/internal/core/logging"
8-
96
"github.com/golang-migrate/migrate/v4"
107
"github.com/golang-migrate/migrate/v4/database/postgres"
11-
12-
// import file driver for migrate
13-
_ "github.com/golang-migrate/migrate/v4/source/file"
8+
_ "github.com/golang-migrate/migrate/v4/source/file" // import file driver for migrate
9+
"github.com/speakeasy-api/speakeasy-example-rest-service-go/internal/core/errors"
10+
"github.com/speakeasy-api/speakeasy-example-rest-service-go/internal/core/logging"
1411
)
1512

1613
const (
17-
ErrDriverInit = errors.Error("failed to initialize postgres driver")
14+
// ErrDriverInit is returned when we cannot initialize the driver.
15+
ErrDriverInit = errors.Error("failed to initialize postgres driver")
16+
// ErrMigrateInit is returned when we cannot initialize the migrate driver.
1817
ErrMigrateInit = errors.Error("failed to initialize migration driver")
19-
ErrMigration = errors.Error("failed to migrate database")
18+
// ErrMigration is returned when we cannot run a migration.
19+
ErrMigration = errors.Error("failed to migrate database")
2020
)
2121

22+
// MigratePostgres migrates the database to the latest version.
2223
func (d *Driver) MigratePostgres(ctx context.Context, migrationsPath string) error {
2324
driver, err := postgres.WithInstance(d.db.DB, &postgres.Config{})
2425
if err != nil {
@@ -43,6 +44,7 @@ func (d *Driver) MigratePostgres(ctx context.Context, migrationsPath string) err
4344
return nil
4445
}
4546

47+
// RevertMigrations reverts the database to the previous version.
4648
func (d *Driver) RevertMigrations(ctx context.Context, migrationsPath string) error {
4749
driver, err := postgres.WithInstance(d.db.DB, &postgres.Config{})
4850
if err != nil {

internal/core/drivers/psql/psql.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,40 @@ package psql
22

33
import (
44
"context"
5-
"fmt"
6-
7-
"github.com/speakeasy-api/speakeasy-example-rest-service-go/internal/core/errors"
85

96
"github.com/jmoiron/sqlx"
10-
_ "github.com/lib/pq"
7+
_ "github.com/lib/pq" // imports the postgres driver
8+
"github.com/speakeasy-api/speakeasy-example-rest-service-go/internal/core/errors"
119
)
1210

1311
const (
12+
// ErrConnect is returned when we cannot connect to the database.
1413
ErrConnect = errors.Error("failed to connect to postgres db")
15-
ErrClose = errors.Error("failed to close postgres db connection")
14+
// ErrClose is returned when we cannot close the database.
15+
ErrClose = errors.Error("failed to close postgres db connection")
1616
)
1717

18+
// Config represents the configuration for our postgres database.
1819
type Config struct {
19-
Username string `yaml:"username"` // TODO get these from secrets in the future
20-
Password string `yaml:"password"` // TODO get these from secrets in the future
21-
Host string `yaml:"host"`
22-
Port string `yaml:"port"`
23-
Database string `yaml:"database"`
20+
DSN string `yaml:"dsn"` // TODO get this from secrets in the future
2421
}
2522

23+
// Driver provides an implementation for connecting to a postgres database.
2624
type Driver struct {
2725
cfg Config
2826
db *sqlx.DB
2927
}
3028

29+
// New instantiates a instance of the Driver.
3130
func New(cfg Config) *Driver {
3231
return &Driver{
3332
cfg: cfg,
3433
}
3534
}
3635

36+
// Connect connects to the database.
3737
func (d *Driver) Connect(ctx context.Context) error {
38-
db, err := sqlx.Connect("postgres", fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=disable", d.cfg.Username, d.cfg.Password, d.cfg.Host, d.cfg.Port, d.cfg.Database))
38+
db, err := sqlx.Connect("postgres", d.cfg.DSN)
3939
if err != nil {
4040
return ErrConnect.Wrap(err)
4141
}
@@ -45,6 +45,7 @@ func (d *Driver) Connect(ctx context.Context) error {
4545
return nil
4646
}
4747

48+
// Close closes the database connection.
4849
func (d *Driver) Close(ctx context.Context) error {
4950
if err := d.db.Close(); err != nil {
5051
return ErrClose.Wrap(err)
@@ -53,6 +54,7 @@ func (d *Driver) Close(ctx context.Context) error {
5354
return nil
5455
}
5556

57+
// GetDB returns the underlying database connection.
5658
func (d *Driver) GetDB() *sqlx.DB {
5759
return d.db
5860
}

0 commit comments

Comments
 (0)