Skip to content

Commit f6bb76d

Browse files
committed
feat: major refactor to follow good standards
1 parent ab395ef commit f6bb76d

19 files changed

Lines changed: 619 additions & 579 deletions

File tree

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 & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
module github.com/felipefrizzo/brazilian-zipcode-api
22

3-
go 1.17
3+
go 1.24
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.122.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.8.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
19+
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
20+
github.com/sagikazarmark/locafero v0.7.0 // indirect
21+
github.com/sourcegraph/conc v0.3.0 // indirect
22+
github.com/spf13/afero v1.12.0 // indirect
23+
github.com/spf13/cast v1.7.1 // indirect
24+
github.com/spf13/pflag v1.0.6 // indirect
25+
github.com/subosito/gotenv v1.6.0 // indirect
26+
go.uber.org/atomic v1.9.0 // indirect
27+
go.uber.org/multierr v1.9.0 // indirect
28+
golang.org/x/sys v0.29.0 // indirect
2829
golang.org/x/text v0.21.0 // indirect
2930
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
30-
gopkg.in/yaml.v3 v3.0.0 // indirect
31+
gopkg.in/yaml.v3 v3.0.1 // indirect
3132
)

0 commit comments

Comments
 (0)