Skip to content

Commit

Permalink
Merge pull request #6 from hiennguyen9874/develop
Browse files Browse the repository at this point in the history
versioned-migrations
  • Loading branch information
hiennguyen9874 committed Apr 30, 2023
2 parents cf1a6fc + b0a15d1 commit c259ecf
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .env.dev
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
COMPOSE_ENV_FILE=.env.dev

SERVER_APP_VERSION=v1.0.0
SERVER_APP_VERSION=20230430.135148.dev
SERVER_PORT=5000
SERVER_MODE=Development
SERVER_PROCESS_TIMEOUT=60
Expand Down
2 changes: 1 addition & 1 deletion .env.prod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
COMPOSE_ENV_FILE=.env.prod

SERVER_APP_VERSION=v1.0.0
SERVER_APP_VERSION=20230430.135148.dev
SERVER_PORT=5000
SERVER_MODE=Production
SERVER_PROCESS_TIMEOUT=60
Expand Down
13 changes: 11 additions & 2 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ FROM golang:1.20-alpine

WORKDIR /app

RUN go install github.com/cosmtrek/air@v1.42.0
RUN go install github.com/roerohan/wait-for-it@v0.2.13
RUN apk add --update \
curl \
&& rm -rf /var/cache/apk/*

RUN curl -sSf https://atlasgo.sh | sh

RUN go install github.com/cosmtrek/air@v1.42.0 && \
go install github.com/roerohan/wait-for-it@v0.2.13

COPY go.mod go.sum ./
RUN go mod download

COPY ./prestart.dev.sh .
RUN chmod +X ./prestart.dev.sh

CMD ["air", "-c", ".air.toml", "serve"]
19 changes: 17 additions & 2 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
FROM golang:1.20-alpine as builder
FROM golang:1.20-alpine as base

RUN apk add --update \
curl \
&& rm -rf /var/cache/apk/*

RUN curl -sSf https://atlasgo.sh | sh

FROM base as builder

ENV GO111MODULE=on CGO_ENABLED=0

Expand All @@ -13,12 +21,19 @@ COPY ./ .

RUN go build -ldflags "-s -w" -o /app/go-boilerplate

FROM golang:1.20-alpine as runtime
FROM base as runtime

WORKDIR /app

COPY ./prestart.prod.sh .
RUN chmod +X ./prestart.prod.sh

# Binary file
COPY --from=builder /app/go-boilerplate /app/go-boilerplate
COPY --from=builder /go/bin/wait-for-it /go/wait-for-it

# Migrations folder
COPY --from=builder /app/ent/migrate/migrations /app/ent/migrate/migrations

RUN mkdir config
COPY ./config/config.default.yml /app/config/
53 changes: 0 additions & 53 deletions cmd/migrate.go

This file was deleted.

14 changes: 0 additions & 14 deletions cmd/serve.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"context"

"github.com/hiennguyen9874/go-boilerplate-v2/config"
"github.com/hiennguyen9874/go-boilerplate-v2/internal/distributor"
"github.com/hiennguyen9874/go-boilerplate-v2/internal/server"
Expand All @@ -18,8 +16,6 @@ var serveCmd = &cobra.Command{
Short: "start http server with configured api",
Long: `Starts a http server and serves the configured api`,
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()

cfg := config.GetCfg()

appLogger := logger.NewApiLogger(cfg)
Expand All @@ -33,16 +29,6 @@ var serveCmd = &cobra.Command{
appLogger.Infof("Postgres connected")
}

if cfg.Server.MigrateOnStart {
err = Migrate(ctx, psqlClient)

if err != nil {
appLogger.Info("Can not migrate data", err)
} else {
appLogger.Info("Data migrated")
}
}

redisClient := redis.NewRedis(cfg)

taskRedisClient := distributor.NewRedisClient(cfg)
Expand Down
1 change: 0 additions & 1 deletion config/config.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ server:
ProcessTimeout: 60
ReadTimeout: 5
WriteTimeout: 5
MigrateOnStart: true

jwt:
SecretKey: 9056b94a8611363c2c43aa3760d65a48c834744b877b5041fa99a919d5aaf635
Expand Down
1 change: 0 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ type ServerConfig struct {
ProcessTimeout int
ReadTimeout int
WriteTimeout int
MigrateOnStart bool
}

type Logger struct {
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
build:
context: .
dockerfile: Dockerfile.dev
command: sh -c "wait-for-it -w db:5432 -w redis:6379 -- go run main.go migrate && go run main.go initdata && air -c .air.toml serve"
command: sh -c "wait-for-it -w db:5432 -w redis:6379 -- ./prestart.dev.sh && air -c .air.toml serve"
ports:
- ${SERVER_PORT}:${SERVER_PORT}
volumes:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
build:
context: .
dockerfile: Dockerfile.prod
command: sh -c "/go/wait-for-it -w db:5432 -w redis:6379 -- /app/go-boilerplate migrate && /app/go-boilerplate initdata && /app/go-boilerplate serve"
command: sh -c "/go/wait-for-it -w db:5432 -w redis:6379 -- ./prestart.prod.sh && /app/go-boilerplate serve"
ports:
- ${SERVER_PORT}:${SERVER_PORT}
depends_on:
Expand Down
6 changes: 6 additions & 0 deletions ent/migrate/migrations/20230430054333_initial.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Create "users" table
CREATE TABLE "users" ("id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "create_time" timestamptz NOT NULL, "update_time" timestamptz NOT NULL, "name" character varying NOT NULL, "email" character varying NOT NULL, "password" character varying NOT NULL, "is_active" boolean NOT NULL DEFAULT true, "is_super_user" boolean NOT NULL DEFAULT false, "verified" boolean NOT NULL DEFAULT false, "verification_code" character varying NULL, "password_reset_token" character varying NULL, "password_reset_at" timestamptz NULL, PRIMARY KEY ("id"));
-- Create index "users_email_key" to table: "users"
CREATE UNIQUE INDEX "users_email_key" ON "users" ("email");
-- Create "items" table
CREATE TABLE "items" ("id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "create_time" timestamptz NOT NULL, "update_time" timestamptz NOT NULL, "title" character varying NOT NULL, "description" character varying NOT NULL, "owner_id" bigint NOT NULL, PRIMARY KEY ("id"), CONSTRAINT "items_users_items" FOREIGN KEY ("owner_id") REFERENCES "users" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION);
2 changes: 2 additions & 0 deletions ent/migrate/migrations/atlas.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
h1:/02JRic5LvWG/9kKSNcp20nE65z4iRo4kFlvOTtuspM=
20230430054333_initial.sql h1:MKWnGLnMG7y0hmpVX+8k/SgSHPX0h592ATjXHHfzd+Y=
31 changes: 19 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@ require (
github.com/swaggo/files/v2 v2.0.0
github.com/swaggo/swag v1.8.11
go.uber.org/zap v1.24.0
golang.org/x/crypto v0.7.0
golang.org/x/crypto v0.8.0
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
)

require (
ariga.io/atlas v0.10.0 // indirect
ariga.io/atlas v0.10.1 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
github.com/PuerkitoBio/goquery v1.8.1 // indirect
github.com/agext/levenshtein v1.2.1 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/ajg/form v1.5.1 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/benbjohnson/clock v1.3.3 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand All @@ -49,25 +50,30 @@ require (
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
github.com/hashicorp/hcl/v2 v2.16.2 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jaytaylor/html2text v0.0.0-20211105163654-bc68cce691ba // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/leodido/go-urn v1.2.2 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/onsi/gomega v1.23.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand All @@ -76,15 +82,16 @@ require (
github.com/subosito/gotenv v1.4.2 // indirect
github.com/vanng822/css v1.0.1 // indirect
github.com/vanng822/go-premailer v1.20.1 // indirect
github.com/zclconf/go-cty v1.8.0 // indirect
github.com/zclconf/go-cty v1.13.1 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
go.uber.org/goleak v1.2.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.7.0 // indirect
golang.org/x/tools v0.8.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
Loading

0 comments on commit c259ecf

Please sign in to comment.