Skip to content

Commit 220bacd

Browse files
authored
feat: major refactor to follow good standards (#7)
* feat: major refactor to follow good standards * fix: upgrade dependencies versions * fix: remove fmt
1 parent ab395ef commit 220bacd

20 files changed

Lines changed: 626 additions & 595 deletions

File tree

.github/workflows/go.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ name: Go
22

33
on:
44
push:
5-
branches: [ "master" ]
5+
branches: ["master"]
66
pull_request:
7-
branches: [ "master" ]
7+
branches: ["master"]
88

99
jobs:
1010
build:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v3
14-
- name: Set up Go
15-
uses: actions/setup-go@v4
16-
with:
17-
go-version: '1.20'
18-
- name: Build
19-
run: go build -v ./...
20-
- name: Test
21-
run: go test -v ./...
13+
- uses: actions/checkout@v4
14+
- name: Set up Go
15+
uses: actions/setup-go@v5
16+
with:
17+
go-version: "1.24"
18+
- name: Build
19+
run: go build -v ./...
20+
- name: Test
21+
run: go test -v ./...

cmd/main.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log/slog"
7+
"net/http"
8+
"os"
9+
"os/signal"
10+
"sync"
11+
"syscall"
12+
"time"
13+
14+
"github.com/felipefrizzo/brazilian-zipcode-api/internal/config"
15+
"github.com/felipefrizzo/brazilian-zipcode-api/internal/server"
16+
)
17+
18+
func main() {
19+
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
20+
logger.Info("starting server")
21+
22+
cfg, err := config.New()
23+
if err != nil {
24+
logger.Error("failed to create config", slog.String("error", err.Error()))
25+
os.Exit(1)
26+
}
27+
28+
srvr, err := server.New(logger, cfg)
29+
if err != nil {
30+
logger.Error("failed to create server", slog.String("error", err.Error()))
31+
os.Exit(1)
32+
}
33+
34+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
35+
defer stop()
36+
37+
var wg sync.WaitGroup
38+
wg.Add(1)
39+
40+
go func() {
41+
defer wg.Done()
42+
logger.Info(fmt.Sprintf("server is running on port %s", cfg.ServerPort))
43+
if err := srvr.Server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
44+
logger.Error("server error", slog.String("error", err.Error()))
45+
}
46+
}()
47+
48+
<-ctx.Done()
49+
50+
logger.Info("shutting down server")
51+
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
52+
defer cancel()
53+
54+
if err := srvr.Server.Shutdown(shutdownCtx); err != nil {
55+
logger.Error("server shutdown error", slog.String("error", err.Error()))
56+
}
57+
58+
wg.Wait()
59+
}

cmd/server.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

go.mod

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
module github.com/felipefrizzo/brazilian-zipcode-api
22

3-
go 1.17
3+
go 1.24.2
44

55
require (
6-
github.com/caarlos0/env/v6 v6.8.0
7-
github.com/gorilla/handlers v1.5.1
8-
github.com/gorilla/mux v1.8.0
9-
github.com/stretchr/testify v1.6.1
10-
go.mongodb.org/mongo-driver v1.8.1
6+
github.com/julienschmidt/httprouter v1.3.0
7+
github.com/mitchellh/mapstructure v1.5.0
8+
github.com/redis/go-redis/v9 v9.8.0
9+
github.com/richardwilkes/toolbox v1.123.1
10+
github.com/spf13/viper v1.20.1
1111
)
1212

1313
require (
14-
github.com/davecgh/go-spew v1.1.1 // indirect
15-
github.com/felixge/httpsnoop v1.0.1 // indirect
16-
github.com/go-stack/stack v1.8.0 // indirect
17-
github.com/golang/snappy v0.0.1 // indirect
18-
github.com/klauspost/compress v1.13.6 // indirect
14+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
15+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
16+
github.com/fsnotify/fsnotify v1.9.0 // indirect
17+
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
1918
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
20-
github.com/pkg/errors v0.9.1 // indirect
21-
github.com/pmezard/go-difflib v1.0.0 // indirect
22-
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
23-
github.com/xdg-go/scram v1.0.2 // indirect
24-
github.com/xdg-go/stringprep v1.0.2 // indirect
25-
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
26-
golang.org/x/crypto v0.31.0 // indirect
27-
golang.org/x/sync v0.10.0 // indirect
28-
golang.org/x/text v0.21.0 // indirect
19+
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
20+
github.com/sagikazarmark/locafero v0.9.0 // indirect
21+
github.com/sourcegraph/conc v0.3.0 // indirect
22+
github.com/spf13/afero v1.14.0 // indirect
23+
github.com/spf13/cast v1.8.0 // indirect
24+
github.com/spf13/pflag v1.0.6 // indirect
25+
github.com/subosito/gotenv v1.6.0 // indirect
26+
go.uber.org/multierr v1.11.0 // indirect
27+
golang.org/x/sys v0.32.0 // indirect
28+
golang.org/x/text v0.24.0 // indirect
2929
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
30-
gopkg.in/yaml.v3 v3.0.0 // indirect
30+
gopkg.in/yaml.v3 v3.0.1 // indirect
3131
)

0 commit comments

Comments
 (0)