Skip to content

Commit 1f2d93d

Browse files
authored
feat: add DAL for MySQL (#62)
* feat: add DAL for MySQL * Update CI&CD.yml * Update CI&CD.yml * Update CI&CD.yml * Update CI&CD.yml
1 parent b72db49 commit 1f2d93d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1255
-12
lines changed

.circleci/config.yml

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ setup_env: &setup_env
77
command: |
88
env | sort > /tmp/env.old
99
10+
export DOCKERIZE_VER=0.12.0
1011
export HADOLINT_VER=1.18.0
1112
export SHELLCHECK_VER=0.7.1
1213
export GOLANGCI_LINT_VER=1.31.0
@@ -32,6 +33,13 @@ jobs:
3233
environment:
3334
GOFLAGS: "-mod=readonly"
3435
EXAMPLE_APIKEY_ADMIN: "admin"
36+
EXAMPLE_MYSQL_ADDR_HOST: "localhost"
37+
EXAMPLE_MYSQL_AUTH_LOGIN: "root"
38+
EXAMPLE_MYSQL_AUTH_PASS: ""
39+
NARADA4D_TEST_MYSQL: "goose-mysql://[email protected]"
40+
- image: "mysql:5.6"
41+
environment:
42+
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
3543
steps:
3644
- checkout
3745
- *setup_env
@@ -46,6 +54,8 @@ jobs:
4654
GO111MODULE: "on"
4755
command: |
4856
cd /tmp # Protect go.mod for modifications by `go get`.
57+
dockerize --version | tee /dev/stderr | grep -wq v$DOCKERIZE_VER ||
58+
curl -sSfL https://github.com/powerman/dockerize/releases/download/v${DOCKERIZE_VER}/dockerize-$(uname)-x86_64 | install /dev/stdin $(go env GOPATH)/bin/dockerize
4959
hadolint --version | tee /dev/stderr | grep -wq v$HADOLINT_VER ||
5060
curl -sSfL https://github.com/hadolint/hadolint/releases/download/v${HADOLINT_VER}/hadolint-$(uname)-x86_64 | install /dev/stdin $(go env GOPATH)/bin/hadolint
5161
shellcheck --version | tee /dev/stderr | grep -wq $SHELLCHECK_VER ||

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
# a*b?c/d[0-9]e[^a-z\]]f\[g - pattern
55
*
66
!bin
7+
!internal/migrations/mysql/*.sql

.github/workflows/CI&CD.yml

+14
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,26 @@ jobs:
1616
test:
1717
runs-on: ubuntu-latest
1818
timeout-minutes: 30
19+
services:
20+
mysql:
21+
image: 'mysql:5.6'
22+
env:
23+
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
24+
ports:
25+
- 3306:3306
1926
env:
27+
DOCKERIZE_VER: '0.12.0'
2028
HADOLINT_VER: '1.18.0'
2129
SHELLCHECK_VER: '0.7.1'
2230
GOLANGCI_LINT_VER: '1.31.0'
2331
GOTESTSUM_VER: '0.5.3'
2432
GOSWAGGER_VER: '0.25.0'
2533
GOVERALLS_VER: '0.0.7'
2634
EXAMPLE_APIKEY_ADMIN: 'admin'
35+
EXAMPLE_MYSQL_ADDR_HOST: 'localhost'
36+
EXAMPLE_MYSQL_AUTH_LOGIN: 'root'
37+
EXAMPLE_MYSQL_AUTH_PASS: ''
38+
NARADA4D_TEST_MYSQL: 'goose-mysql://[email protected]'
2739
steps:
2840
- uses: actions/setup-go@v2
2941
with:
@@ -52,6 +64,8 @@ jobs:
5264
GO111MODULE: 'on'
5365
run: |
5466
cd /tmp # Protect go.mod for modifications by `go get`.
67+
dockerize --version | tee /dev/stderr | grep -wq v$DOCKERIZE_VER ||
68+
curl -sSfL https://github.com/powerman/dockerize/releases/download/v${DOCKERIZE_VER}/dockerize-$(uname)-x86_64 | install /dev/stdin $(go env GOPATH)/bin/dockerize
5569
hadolint --version | tee /dev/stderr | grep -wq v$HADOLINT_VER ||
5670
curl -sSfL https://github.com/hadolint/hadolint/releases/download/v${HADOLINT_VER}/hadolint-$(uname)-x86_64 | install /dev/stdin $(go env GOPATH)/bin/hadolint
5771
shellcheck --version | tee /dev/stderr | grep -wq $SHELLCHECK_VER ||

.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ run:
4141
# on Windows.
4242
skip-files:
4343
- "\\.[\\w-]+\\.go$"
44+
- "/migrations/(.*/)?[0-9]{5}_"
4445

4546
# by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules":
4647
# If invoked with -mod=readonly, the go command is disallowed from the implicit

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,6 @@ $ ./bin/address-book serve
271271
## TODO
272272

273273
- [ ] Update JSON Schema support cheatsheet to latest go-swagger version.
274-
- [ ] Replace trivial in-memory DAL with more complete one based on
275-
Postgresql with metrics and migrations support.
274+
- [ ] Add alternative DAL implementation for Postgresql.
276275
- [ ] Add cookie-based auth with CSRF middleware.
277276
- [ ] Add an example of adapter for external service in `svc/something`.

cmd/address-book/init_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import (
1010
"github.com/powerman/go-service-example/api/openapi/model"
1111
"github.com/powerman/go-service-example/internal/app"
1212
"github.com/powerman/go-service-example/internal/config"
13+
dal "github.com/powerman/go-service-example/internal/dal/mysql"
1314
"github.com/powerman/go-service-example/internal/srv/openapi"
1415
"github.com/powerman/go-service-example/pkg/def"
1516
)
1617

1718
func TestMain(m *testing.M) {
1819
def.Init()
1920
initMetrics(reg, "test")
21+
dal.InitMetrics(reg, "test")
2022
app.InitMetrics(reg)
2123
openapi.InitMetrics(reg, "test")
2224
cfg = config.MustGetServeTest()
@@ -32,3 +34,7 @@ var (
3234
apiKeyUser = oapiclient.APIKeyAuth("API-Key", "header", "user")
3335
apiContact1 = &model.Contact{ID: 1, Name: swag.String("A")}
3436
)
37+
38+
type tLogger check.C
39+
40+
func (t tLogger) Print(args ...interface{}) { t.Log(args...) }

cmd/address-book/service.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"github.com/powerman/go-service-example/api/openapi/restapi"
88
"github.com/powerman/go-service-example/internal/app"
99
"github.com/powerman/go-service-example/internal/config"
10-
dal "github.com/powerman/go-service-example/internal/dal/memory"
10+
dal "github.com/powerman/go-service-example/internal/dal/mysql"
11+
migrations_mysql "github.com/powerman/go-service-example/internal/migrations/mysql"
1112
"github.com/powerman/go-service-example/internal/srv/openapi"
13+
"github.com/powerman/go-service-example/pkg/cobrax"
1214
"github.com/powerman/go-service-example/pkg/concurrent"
1315
"github.com/powerman/go-service-example/pkg/def"
1416
"github.com/powerman/go-service-example/pkg/serve"
@@ -29,14 +31,19 @@ type service struct {
2931
srv *restapi.Server
3032
}
3133

32-
func initService(_, serveCmd *cobra.Command) error {
34+
func initService(cmd, serveCmd *cobra.Command) error {
3335
namespace := regexp.MustCompile(`[^a-zA-Z0-9]+`).ReplaceAllString(def.ProgName, "_")
3436
initMetrics(reg, namespace)
37+
dal.InitMetrics(reg, namespace)
3538
app.InitMetrics(reg)
3639
openapi.InitMetrics(reg, namespace)
3740

41+
gooseMySQLCmd := cobrax.NewGooseMySQLCmd(migrations_mysql.Goose(), config.GetGooseMySQL)
42+
cmd.AddCommand(gooseMySQLCmd)
43+
3844
return config.Init(config.FlagSets{
39-
Serve: serveCmd.Flags(),
45+
Serve: serveCmd.Flags(),
46+
GooseMySQL: gooseMySQLCmd.Flags(),
4047
})
4148
}
4249

@@ -78,7 +85,7 @@ func (s *service) runServe(ctxStartup, ctxShutdown Ctx, shutdown func()) (err er
7885
}
7986

8087
func (s *service) connectRepo(ctx Ctx) (interface{}, error) {
81-
return dal.New(ctx)
88+
return dal.New(ctx, s.cfg.MySQLGooseDir, s.cfg.MySQL)
8289
}
8390

8491
func (s *service) serveMetrics(ctx Ctx) error {

cmd/address-book/service_integration_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ import (
1414
"github.com/powerman/go-service-example/internal/srv/openapi"
1515
"github.com/powerman/go-service-example/pkg/def"
1616
"github.com/powerman/go-service-example/pkg/netx"
17+
"github.com/powerman/mysqlx"
1718
)
1819

1920
func TestSmoke(tt *testing.T) {
2021
t := check.T(tt)
2122

23+
tempDBCfg, cleanup, err := mysqlx.EnsureTempDB(tLogger(*t), "", cfg.MySQL)
24+
cfg.MySQL = tempDBCfg
25+
t.Must(t.Nil(err))
26+
defer cleanup()
27+
2228
s := &service{cfg: cfg}
2329

2430
ctxStartup, cancel := context.WithTimeout(ctx, def.TestTimeout)
@@ -29,6 +35,9 @@ func TestSmoke(tt *testing.T) {
2935
defer func() {
3036
shutdown()
3137
t.Nil(<-errc, "RunServe")
38+
if s.repo != nil {
39+
s.repo.Close()
40+
}
3241
}()
3342
t.Must(t.Nil(netx.WaitTCPPort(ctxStartup, cfg.Addr), "connect to service"))
3443

docker-compose.yml

+17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
version: "3.8"
22

3+
volumes:
4+
mysql:
5+
36
services:
47

8+
mysql:
9+
image: "mysql:5.6"
10+
container_name: example_mysql
11+
restart: always
12+
ports:
13+
- "${example_mysql_addr_port:-0}:3306"
14+
volumes:
15+
- "mysql:/var/lib/mysql"
16+
environment:
17+
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
18+
519
address-book:
620
build:
721
context: .
@@ -14,3 +28,6 @@ services:
1428
- "${EXAMPLE_METRICS_ADDR_PORT:-0}:9000"
1529
environment:
1630
EXAMPLE_APIKEY_ADMIN: "${EXAMPLE_APIKEY_ADMIN:?}"
31+
EXAMPLE_MYSQL_ADDR_HOST: "mysql"
32+
EXAMPLE_MYSQL_AUTH_LOGIN: "root"
33+
EXAMPLE_MYSQL_AUTH_PASS: ""

env.sh.dist

+7
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55

66
# - Set all _PORT vars to port numbers not used by your system.
77

8+
export example_mysql_addr_port="3306"
9+
810
export EXAMPLE_APIKEY_ADMIN="admin"
911
export EXAMPLE_ADDR_HOST="localhost"
1012
export EXAMPLE_ADDR_PORT="8000"
1113
export EXAMPLE_METRICS_ADDR_PORT="9000"
14+
export EXAMPLE_MYSQL_ADDR_HOST="127.0.0.1"
15+
export EXAMPLE_MYSQL_ADDR_PORT="${example_mysql_addr_port}"
16+
export EXAMPLE_MYSQL_AUTH_LOGIN="root"
17+
export EXAMPLE_MYSQL_AUTH_PASS=""
1218
export GO_TEST_TIME_FACTOR="1.0" # Increase if tests fail because of slow CPU.
19+
export NARADA4D_TEST_MYSQL="goose-mysql://[email protected]:${example_mysql_addr_port}"
1320

1421
# DO NOT MODIFY BELOW THIS LINE!
1522
env1="$(sed -e '/^$/d' -e '/^#/d' -e 's/=.*//' env.sh.dist)"

go.mod

+6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ require (
1111
github.com/go-openapi/strfmt v0.19.5
1212
github.com/go-openapi/swag v0.19.9
1313
github.com/go-openapi/validate v0.19.11
14+
github.com/go-sql-driver/mysql v1.5.0
1415
github.com/golang/mock v1.4.4
1516
github.com/jessevdk/go-flags v1.4.0
17+
github.com/jmoiron/sqlx v1.2.0
1618
github.com/powerman/appcfg v0.5.0
1719
github.com/powerman/check v1.2.1
1820
github.com/powerman/getenv v0.1.0
21+
github.com/powerman/goose/v2 v2.7.0
1922
github.com/powerman/must v0.1.0
23+
github.com/powerman/mysqlx v0.3.3
24+
github.com/powerman/narada4d v1.7.0
25+
github.com/powerman/sqlxx v0.2.0
2026
github.com/powerman/structlog v0.7.1
2127
github.com/prometheus/client_golang v1.7.1
2228
github.com/rs/cors v1.7.0

0 commit comments

Comments
 (0)