Skip to content

Commit d02afbd

Browse files
authored
Merge pull request #2 from isoppp/feature/refactor-initialization-and-config
refactor initialization and config
2 parents c762b6f + 94e9462 commit d02afbd

File tree

13 files changed

+322
-157
lines changed

13 files changed

+322
-157
lines changed

Makefile

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
export PORT := 8081
2-
export MIGRATION_PATH := db/migrations
3-
export DEV_DATABASE_PATH := postgresql://postgres:postgres@localhost:5433/sandbox?sslmode=disable
2+
export _MIGRATION_DIR := db/migrations
3+
export _MIGRATION_DB_PATH := postgresql://postgres:postgres@localhost:5433/sandbox?sslmode=disable
44

55
run:
66
go run ./cmd/api
77

88
dev:
9-
reflex -r '\.go$$' -s make run
9+
go run ./cmd/api -dev
10+
11+
devw:
12+
reflex -r '\.go$$' -s make dev
1013

1114
postgres:
1215
docker-compose up -d
@@ -15,21 +18,21 @@ sqlboiler:
1518
go generate
1619

1720
migrateup:
18-
migrate -path $(MIGRATION_PATH) -database $(DEV_DATABASE_PATH) -verbose up
21+
migrate -path $(_MIGRATION_DIR) -database $(_MIGRATION_DB_PATH) -verbose up
1922

2023
migrateup1:
21-
migrate -path $(MIGRATION_PATH) -database $(DEV_DATABASE_PATH) -verbose up 1
24+
migrate -path $(_MIGRATION_DIR) -database $(_MIGRATION_DB_PATH) -verbose up 1
2225

2326
migratedown:
24-
migrate -path $(MIGRATION_PATH) -database $(DEV_DATABASE_PATH) -verbose down
27+
migrate -path $(_MIGRATION_DIR) -database $(_MIGRATION_DB_PATH) -verbose down
2528

2629
migratedown1:
27-
migrate -path $(MIGRATION_PATH) -database $(DEV_DATABASE_PATH) -verbose down 1
30+
migrate -path $(_MIGRATION_DIR) -database $(_MIGRATION_DB_PATH) -verbose down 1
2831

2932
migrateredo:
3033
make migratedown1 && make migrateup1
3134

3235
test:
3336
go test -v -cover ./...
3437

35-
.PHONY: run dev postgres sqlboiler migratedown migratedown1 migrateup migrateup1 migrateredo test
38+
.PHONY: run dev devw postgres sqlboiler migratedown migratedown1 migrateup migrateup1 migrateredo test

cmd/api/main.go

+21-25
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,44 @@ import (
55
"fmt"
66
"log"
77
"sandbox-go-api-sqlboiler-rest-auth/internal/config"
8-
"sandbox-go-api-sqlboiler-rest-auth/internal/routes"
8+
"sandbox-go-api-sqlboiler-rest-auth/internal/logger"
9+
"sandbox-go-api-sqlboiler-rest-auth/internal/server"
910

1011
"github.com/volatiletech/sqlboiler/v4/boil"
1112

12-
"go.uber.org/zap/zapcore"
13-
14-
"go.uber.org/zap"
15-
1613
_ "github.com/lib/pq"
1714
)
1815

1916
func main() {
2017
// config
21-
cfg, err := config.NewConfig()
18+
cfg := config.NewConfig()
19+
20+
// db
21+
db, err := sql.Open("postgres", cfg.GetDataSourceName())
2222
if err != nil {
23-
log.Fatalln(err)
24-
return
23+
log.Fatalln("cannot open the database", cfg.GetDataSourceName())
2524
}
26-
27-
// logger
28-
zapConfig := zap.NewDevelopmentConfig()
29-
encConfig := zap.NewDevelopmentEncoderConfig()
30-
encConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
31-
zapConfig.EncoderConfig = encConfig
32-
logger, _ := zapConfig.Build()
3325
defer func() {
34-
_ = logger.Sync()
26+
_ = db.Close()
3527
}()
3628

37-
// db
38-
connStr := "host=localhost port=5433 dbname=sandbox user=postgres password=postgres sslmode=disable"
39-
db, err := sql.Open("postgres", connStr)
29+
// enable debug mode
30+
boil.DebugMode = cfg.IsDev
31+
32+
// logger
33+
zl, err := logger.NewLogger(cfg)
4034
if err != nil {
41-
log.Fatalln("cannot open the database")
35+
log.Fatalln("cannot create logger")
4236
}
4337
defer func() {
44-
_ = db.Close()
38+
_ = zl.Sync()
4539
}()
4640

47-
// sqlboiler debug mode
48-
boil.DebugMode = true
41+
// router
42+
e := server.NewServer(cfg, db, zl)
4943

50-
e := routes.NewRouter(db, logger)
51-
e.Logger.Fatal(e.Start(fmt.Sprintf(":%s", cfg.Port)))
44+
err = e.Start(fmt.Sprintf(":%s", cfg.Port))
45+
if err != nil {
46+
zl.Sugar().Fatal(err)
47+
}
5248
}

go.mod

+9-10
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@ module sandbox-go-api-sqlboiler-rest-auth
33
go 1.17
44

55
require (
6-
github.com/caarlos0/env/v6 v6.7.1
76
github.com/friendsofgo/errors v0.9.2
87
github.com/google/uuid v1.3.0
98
github.com/gorilla/securecookie v1.1.1
109
github.com/kat-co/vala v0.0.0-20170210184112-42e1d8b61f12
1110
github.com/labstack/echo/v4 v4.5.0
1211
github.com/lib/pq v1.10.3
13-
github.com/spf13/viper v1.8.1
12+
github.com/spf13/viper v1.9.0
1413
github.com/volatiletech/randomize v0.0.1
1514
github.com/volatiletech/sqlboiler/v4 v4.6.0
1615
github.com/volatiletech/strmangle v0.0.1
1716
go.uber.org/zap v1.19.1
1817
)
1918

2019
require (
21-
github.com/fsnotify/fsnotify v1.4.9 // indirect
20+
github.com/fsnotify/fsnotify v1.5.1 // indirect
2221
github.com/gofrs/uuid v3.2.0+incompatible // indirect
2322
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
2423
github.com/hashicorp/hcl v1.0.0 // indirect
@@ -27,12 +26,12 @@ require (
2726
github.com/magiconair/properties v1.8.5 // indirect
2827
github.com/mattn/go-colorable v0.1.8 // indirect
2928
github.com/mattn/go-isatty v0.0.12 // indirect
30-
github.com/mitchellh/mapstructure v1.4.1 // indirect
29+
github.com/mitchellh/mapstructure v1.4.2 // indirect
3130
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
32-
github.com/pelletier/go-toml v1.9.3 // indirect
31+
github.com/pelletier/go-toml v1.9.4 // indirect
3332
github.com/pkg/errors v0.9.1 // indirect
3433
github.com/spf13/afero v1.6.0 // indirect
35-
github.com/spf13/cast v1.3.1 // indirect
34+
github.com/spf13/cast v1.4.1 // indirect
3635
github.com/spf13/jwalterweatherman v1.1.0 // indirect
3736
github.com/spf13/pflag v1.0.5 // indirect
3837
github.com/subosito/gotenv v1.2.0 // indirect
@@ -41,13 +40,13 @@ require (
4140
github.com/volatiletech/inflect v0.0.1 // indirect
4241
go.uber.org/atomic v1.7.0 // indirect
4342
go.uber.org/multierr v1.6.0 // indirect
44-
golang.org/x/crypto v0.0.0-20210506145944-38f3c27a63bf // indirect
45-
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
46-
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
43+
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
44+
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420 // indirect
45+
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
4746
golang.org/x/text v0.3.6 // indirect
4847
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
4948
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
5049
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
51-
gopkg.in/ini.v1 v1.62.0 // indirect
50+
gopkg.in/ini.v1 v1.63.2 // indirect
5251
gopkg.in/yaml.v2 v2.4.0 // indirect
5352
)

0 commit comments

Comments
 (0)