From b9006e4a1bf1bf4a9f13f2099424a53f8826b3b9 Mon Sep 17 00:00:00 2001 From: psssix Date: Mon, 18 Jul 2022 10:32:25 +0300 Subject: [PATCH 01/22] feat: multistage build Dockerfile, not tested --- .dockerignore | 7 +++++-- .gitignore | 4 ++-- Dockerfile | 53 +++++++++++++++++++++++---------------------------- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/.dockerignore b/.dockerignore index f93757f..7645523 100644 --- a/.dockerignore +++ b/.dockerignore @@ -6,6 +6,9 @@ # Visual Studio Code workspace files /workstations.code-workspace -# vendoring +# directories /vendor/ -/build/ \ No newline at end of file +/build/ + +go.mod +go.sum \ No newline at end of file diff --git a/.gitignore b/.gitignore index 0f36fad..557f4c0 100644 --- a/.gitignore +++ b/.gitignore @@ -6,9 +6,9 @@ # Visual Studio Code workspace files /workstations.code-workspace -# vendoring +# directories /vendor/ -/build/pkg/ +/build/ .env .env.docker diff --git a/Dockerfile b/Dockerfile index 0f64ee6..65ad9aa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,49 +1,44 @@ -FROM golang:alpine +LABEL org.opencontainers.image.authors="psssix " -MAINTAINER psssix +FROM golang:alpine as builder -# Set necessary environmet variables needed for our image -ENV GO111MODULE=on \ - CGO_ENABLED=0 \ - GOOS=linux \ - GOARCH=amd64 +# set build environmet variables needed for multistage building +ENV CGO_ENABLED=0 \ + GOOS=linux -# Use workdir -RUN mkdir /source +# source workdir WORKDIR /source # Copy and download dependency using go mod -COPY go.mod . -COPY go.sum . +# Vendoring +COPY go.mod go.sum ./ +RUN go mod download + # Copy the code into the container COPY . . -# Vendoring -RUN go mod tidy && go mod vendor +# Some different way for vendoring +#RUN go mod tidy && go mod vendor # Build the application -RUN go build -o build/golang-app -mod=vendor . +RUN go build -o build/app . -# Move to /smart-bot directory as the place for resulting binary folder -RUN mkdir /app -WORKDIR /app +FROM scratch + +# Move to /app directory as the place for resulting binary folder +WORKDIR / # Copy binary from build to main folder -# -# some application assets -# RUN cp /source/build/bot . \ -# && cp -R /source/docs/dictionaries . -RUN cp /source/build/golang-app - -# COPY ./database/data.json /dist/database/data.json - -RUN rm -rf /source +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +COPY --from=builder build/app app +# Copy some application assets +#COPY ./database/data.json /dist/database/data.json # Export necessary port -# EXPOSE 3000 +#EXPOSE 3000 -ENTRYPOINT ["/app/golang-app"] +ENTRYPOINT ["/app"] # Command to run when starting the container -#CMD ["/dist/golang-app"] +#CMD ["/app"] # if have not application daemon #CMD tail -f /dev/null \ No newline at end of file From 3cea5519880cbcff93385e73671ea3d919599445 Mon Sep 17 00:00:00 2001 From: psssix Date: Wed, 20 Jul 2022 01:05:08 +0300 Subject: [PATCH 02/22] feat: debug multistage build --- .dockerignore | 5 +---- Dockerfile | 8 ++++---- config/Config.go | 4 ++-- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/.dockerignore b/.dockerignore index 7645523..6f1caf6 100644 --- a/.dockerignore +++ b/.dockerignore @@ -8,7 +8,4 @@ # directories /vendor/ -/build/ - -go.mod -go.sum \ No newline at end of file +/build/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 65ad9aa..3970996 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ -LABEL org.opencontainers.image.authors="psssix " - FROM golang:alpine as builder +LABEL org.opencontainers.image.authors="psssix " + # set build environmet variables needed for multistage building ENV CGO_ENABLED=0 \ GOOS=linux @@ -29,10 +29,10 @@ WORKDIR / # Copy binary from build to main folder COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ -COPY --from=builder build/app app +COPY --from=builder /source/build/app app # Copy some application assets -#COPY ./database/data.json /dist/database/data.json +#COPY ./database/data.json /database/data.json # Export necessary port #EXPOSE 3000 diff --git a/config/Config.go b/config/Config.go index dffe9fa..827d757 100644 --- a/config/Config.go +++ b/config/Config.go @@ -8,7 +8,7 @@ import ( type Config struct { API struct { Token string `env:"TOKEN" env-layout:"string" env-required:"true"` - Owner int `env:"OWNER" env-layout:"int" env-required:"true"` + Owner int `env:"OWNER" env-layout:"int" env-required:"true"` } Paths struct { Docs string `env:"DOCS_PATH" env-layout:"string" env-default:"docs" env-upd:"true"` @@ -23,7 +23,7 @@ func GetConfig() Config { once.Do(func() { err := cleanenv.ReadEnv(&cfg) if err != nil { - panic(err.Error()) + panic(err) } }) return cfg From 53e186f8967a961e2f832e1163428c90fb0153f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=24i=D1=85?= Date: Wed, 3 Aug 2022 22:28:22 +0300 Subject: [PATCH 03/22] feat: extend ignores --- .dockerignore | 39 ++++++++++++++++++++++++++++++++------- .gitignore | 22 ++++++++++++++++------ 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/.dockerignore b/.dockerignore index 6f1caf6..1e21915 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,11 +1,36 @@ -# go generations -*.exe - # IDEA project files /.idea +/.run # Visual Studio Code workspace files -/workstations.code-workspace +/.vscode +/golang-app.code-workspace -# directories -/vendor/ -/build/ \ No newline at end of file +# git +/.git +/.gitignore + +# docker +/Dockerfile +/docker-compose.yml +/docker-compose-services.yml + +# tools +/.gitlab-ci.yml +/.golangci.yaml + +# OS +.DS_Store + +# artefacts +*.exe +/bin +/build +/data +/docs +/node_modules +/vendor + +# environment +/.env +/.env.docker +/config.yaml \ No newline at end of file diff --git a/.gitignore b/.gitignore index 557f4c0..5e5e389 100644 --- a/.gitignore +++ b/.gitignore @@ -4,11 +4,21 @@ # IDEA project files /.idea # Visual Studio Code workspace files -/workstations.code-workspace +/.vscode +/golang-app.code-workspace -# directories -/vendor/ -/build/ +# OS +.DS_Store -.env -.env.docker +# artefacts +*.exe +/bin +/build +/data +/node_modules +/vendor + +# environment +/.envenv +/.env.docker +/config.yaml From d767706dec589bf1315df9f17f1c27c3b3c3eac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=24i=D1=85?= Date: Wed, 3 Aug 2022 22:29:34 +0300 Subject: [PATCH 04/22] feat: extend ignores --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5e5e389..5fcfef4 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,6 @@ /vendor # environment -/.envenv +/.env /.env.docker /config.yaml From 4c73ed54c5873f04b42b3f7cfb4739b99d90ed97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=24i=D1=85?= Date: Wed, 3 Aug 2022 22:59:35 +0300 Subject: [PATCH 05/22] feat: entrypoint --- deploy/docker/environment.sh | 52 ++++++++++++++++++++++++++++++++++++ deploy/docker/logging.sh | 31 +++++++++++++++++++++ entrypoint.sh | 25 +++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 deploy/docker/environment.sh create mode 100644 deploy/docker/logging.sh create mode 100644 entrypoint.sh diff --git a/deploy/docker/environment.sh b/deploy/docker/environment.sh new file mode 100644 index 0000000..3fd7008 --- /dev/null +++ b/deploy/docker/environment.sh @@ -0,0 +1,52 @@ +source deploy/docker/logging.sh + +# usage: loadVariable VAR [DEFAULT] +# ie: file_env 'DB_PASSWORD' 'example' +# (will allow for "$DB_PASSWORD_FILE" to fill in the value of +# "$DB_PASSWORD" from a file, especially for Docker's secrets feature) +loadVariable() { + local variable="$1" + local variableFile="${variable}_FILE" + local default="${2:-}" + if [ "${!variable:-}" ] && [ "${!variableFile:-}" ]; then + logError "Both $variable and $variableFile are set (but are exclusive)" + fi + local value="$default" + if [ "${!variable:-}" ]; then + value="${!variable}" + elif [ "${!variableFile:-}" ]; then + value="$(<"${!variableFile}")" + fi + export "$variable"="$value" + unset "$variableFile" +} + +# loads various settings +setupEnvironment() { + loadVariable 'TIMEZONE' 'Etc/GMT' + +# loadVariable 'MIGRATION_PATH' '' +# # initialize values that might be stored in a file +# loadVariable 'MYSQL_HOST' +# loadVariable 'MYSQL_DATABASE' 'defaultDatabase' +# loadVariable 'MYSQL_USER' 'defaultUser' +# loadVariable 'MYSQL_PASSWORD' + + # initialize values that might be stored in a file + loadVariable 'TOKEN' + loadVariable 'OWNER' 'default owner' +} + +# verify required environment. +verifyEnvironment() { + if [ -z "$TOKEN" ]; then + logError $'Token is not completely filled\n\tYou need to specify $TOKEN' + fi +# if [ -z "$MYSQL_HOST" -o -z "$MYSQL_PASSWORD" ]; then +# logError $'MYSQL databases credentials is not completely filled\n\tYou need to specify $MYSQL_HOST, $MYSQL_PASSWORD' +# fi +} + +setupEnvironment +verifyEnvironment + diff --git a/deploy/docker/logging.sh b/deploy/docker/logging.sh new file mode 100644 index 0000000..a1a9d4f --- /dev/null +++ b/deploy/docker/logging.sh @@ -0,0 +1,31 @@ +# logging functions +_log() { + local type="$1" + shift + printf '%s [%s] [entrypoint]: %s\n' "$(date -R)" "$type" "$*" +} + +logNotice() { + _log NOTICE "$@" +} +logWarning() { + _log WARNING "$@" >&2 +} +logError() { + _log ERROR "$@" >&2 + exit 1 +} + +startWaiting() { + local type="NOTICE" + printf '%s [%s] [entrypoint]: %s' "$(date -R)" "$type" "$*" +} + +waiting() { + sleep 1 + printf '.' +} + +finishWaiting() { + printf '\n' +} diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..6b78bd5 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +source deploy/docker/environment.sh +source deploy/docker/logging.sh + +IS_STARTED='/tmp/is-started' +if [ ! -e $IS_STARTED ]; then + logNotice 'Entrypoint script for golang-app started' + +# logNotice 'Waiting to start MYSQL database...' +# while ! mysql --protocol=TCP --host="$MYSQL_HOST" --user="$MYSQL_USER" --password="$MYSQL_PASSWORD" --execute="show databases;" --silent 1>/dev/null; do +# sleep 1 +# done +# logNotice 'Connected' + +# if [ "$MIGRATION_PARAM" ]; then +# logNotice 'Apply golang-app database migrations from ' $MIGRATION_PARAM +# ./migrate -p $MIGRATION_PARAM up +# fi + + touch $IS_STARTED + logNotice 'Project golang-app initialization complete. Ready for start up' +fi + +exec "$@" From 5023d75fcd924c1e9961fa23e00f9af4efff99e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=24i=D1=85?= Date: Wed, 3 Aug 2022 23:17:49 +0300 Subject: [PATCH 06/22] feat: Docker file --- .dockerignore | 2 +- .env.dist | 2 +- Dockerfile | 24 ++++++++++++++++++------ README.md | 9 ++------- deploy/docker/environment.sh | 2 +- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/.dockerignore b/.dockerignore index 1e21915..fc23a21 100644 --- a/.dockerignore +++ b/.dockerignore @@ -14,7 +14,7 @@ /docker-compose.yml /docker-compose-services.yml -# tools +# utilities /.gitlab-ci.yml /.golangci.yaml diff --git a/.env.dist b/.env.dist index 30028a9..95848bd 100644 --- a/.env.dist +++ b/.env.dist @@ -1,4 +1,4 @@ TOKEN= OWNER= # some application assets -# DOCS_PATH=docs \ No newline at end of file +# MIGRATION_PARAM=/migrations \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 3970996..3d0c9a9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,29 +16,41 @@ RUN go mod download # Copy the code into the container COPY . . -# Some different way for vendoring -#RUN go mod tidy && go mod vendor # Build the application -RUN go build -o build/app . +RUN go build -ldflags "-s -w" -o /build/app . FROM scratch +# or alpine, if you need install utilities +# +#FROM alpine:latest +# +## Add entrypoint dependency +#RUN apk add --no-cache bash mysql-client mariadb-connector-c + # Move to /app directory as the place for resulting binary folder WORKDIR / # Copy binary from build to main folder COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ -COPY --from=builder /source/build/app app +COPY --from=builder /build/app app # Copy some application assets +#COPY --from=builder /source/docker/environment.sh ./docker/environment.sh +#COPY --from=builder /source/docker/logging.sh ./docker/logging.sh +#COPY --from=builder /source/entrypoint.sh . +#COPY --from=builder /source/migrations ./migrations #COPY ./database/data.json /database/data.json + # Export necessary port #EXPOSE 3000 -ENTRYPOINT ["/app"] +# entry and start entrypoint with some actions and checks before CMD +#ENTRYPOINT ["bash", "/entrypoint.sh" ] # Command to run when starting the container -#CMD ["/app"] +CMD ["/app"] + # if have not application daemon #CMD tail -f /dev/null \ No newline at end of file diff --git a/README.md b/README.md index 267fe46..d4cac33 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,7 @@ docker push psssix/golang-app:latest ``` #### Run docker container -Run container on localhost docker +Run container on docker ```bash - docker run -d --env-file .env.docker --name study-bot psssix/golang-app -``` -Run container on overcast docker -```bash -docker pull psssix/golang-app -docker run -d --env-file .env --name golang-app psssix/golang-app:latest +docker run -d --env-file .env.docker --name golang-app psssix/golang-app:latest ``` \ No newline at end of file diff --git a/deploy/docker/environment.sh b/deploy/docker/environment.sh index 3fd7008..518f241 100644 --- a/deploy/docker/environment.sh +++ b/deploy/docker/environment.sh @@ -25,7 +25,7 @@ loadVariable() { setupEnvironment() { loadVariable 'TIMEZONE' 'Etc/GMT' -# loadVariable 'MIGRATION_PATH' '' +# loadVariable 'MIGRATION_PARAM' '' # # initialize values that might be stored in a file # loadVariable 'MYSQL_HOST' # loadVariable 'MYSQL_DATABASE' 'defaultDatabase' From cda5d0cff24f5b81cbbcc1ab32f58ddada2738ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=24i=D1=85?= Date: Wed, 3 Aug 2022 23:37:26 +0300 Subject: [PATCH 07/22] feat: docker debug Dockerfile --- Debug.Dockerfile | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 4 ++-- 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 Debug.Dockerfile diff --git a/Debug.Dockerfile b/Debug.Dockerfile new file mode 100644 index 0000000..5986f9d --- /dev/null +++ b/Debug.Dockerfile @@ -0,0 +1,52 @@ +FROM golang:alpine as builder + +LABEL org.opencontainers.image.authors="psssix " + +# Build Delve +RUN go install github.com/go-delve/delve/cmd/dlv@latest + +# set build environmet variables needed for multistage building +ENV CGO_ENABLED=0 \ + GOOS=linux + +# source workdir +WORKDIR /source + +# Copy and download dependency using go mod +# Vendoring +COPY go.mod go.sum ./ +RUN go mod download + +# Copy the code into the container +COPY . . + +# Build the application +RUN go build -gcflags="all=-N -l" -o /build/app . + +FROM alpine:latest + +# Add entrypoint dependency +#RUN apk add --no-cache bash mysql-client mariadb-connector-c + +# Move to /backoffice_gateway directory as the place for resulting binary folder +WORKDIR / + +# Copy binary from build to main folder +COPY --from=builder /go/bin/dlv . +COPY --from=builder /build/app app + +# Copy some application assets +COPY --from=builder /source/deploy/docker/environment.sh ./docker/environment.sh +COPY --from=builder /source/deploy/docker/logging.sh ./docker/logging.sh +COPY --from=builder /source/entrypoint.sh . +#COPY --from=builder /source/migrations ./migrations +#COPY ./database/data.json /database/data.json + +# Export necessary port +EXPOSE 4040 + +# entry and start php-fpm server +ENTRYPOINT ["bash", "/entrypoint.sh" ] + +# Command to run when starting the container +CMD ["/dlv", "--listen=:4040", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "/app"] \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 3d0c9a9..36a3988 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,8 +37,8 @@ COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ COPY --from=builder /build/app app # Copy some application assets -#COPY --from=builder /source/docker/environment.sh ./docker/environment.sh -#COPY --from=builder /source/docker/logging.sh ./docker/logging.sh +#COPY --from=builder /source/deploy/docker/environment.sh ./deploy/docker/environment.sh +#COPY --from=builder /source/deploy/docker/logging.sh ./deploy/docker/logging.sh #COPY --from=builder /source/entrypoint.sh . #COPY --from=builder /source/migrations ./migrations #COPY ./database/data.json /database/data.json From d87aed14f49cfd85f7b9c20ad6b099ea237f255f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=24i=D1=85?= Date: Sat, 6 Aug 2022 23:40:55 +0300 Subject: [PATCH 08/22] feat refactor config for default application --- config/Config.go | 9 +++-- config/Config_test.go | 77 ++++++++++++++++++++----------------------- 2 files changed, 41 insertions(+), 45 deletions(-) diff --git a/config/Config.go b/config/Config.go index 827d757..0ec4bc4 100644 --- a/config/Config.go +++ b/config/Config.go @@ -15,10 +15,12 @@ type Config struct { } } -var cfg Config -var once sync.Once +var ( + cfg Config //nolint:gochecknoglobals // singleton globals + once sync.Once //nolint:gochecknoglobals // singleton globals +) -// panic +// GetConfig can panic func GetConfig() Config { once.Do(func() { err := cleanenv.ReadEnv(&cfg) @@ -26,5 +28,6 @@ func GetConfig() Config { panic(err) } }) + return cfg } diff --git a/config/Config_test.go b/config/Config_test.go index ec6f8b9..ca73b53 100644 --- a/config/Config_test.go +++ b/config/Config_test.go @@ -2,50 +2,47 @@ package config import ( "github.com/stretchr/testify/assert" - "os" "strconv" "sync" "testing" ) -func TestGetConfig(t *testing.T) { - telegramToken := ":: telegram telegram token ::" - telegramOwner := 1234567890 - pathDictionaries := `docs/dictionaries` +const ( + telegramToken = ":: telegram telegram token ::" //nolint:gosec // it is not real credentials + telegramOwner = 1234567890 + pathDictionaries = `docs/dictionaries` +) - initTelegramBotEnvironment(telegramToken, telegramOwner, pathDictionaries) - defer cleanTelegramBotEnvironment() +func TestGetConfig(t *testing.T) { //nolint:paralleltest // because this test mock globals + initTelegramBotEnvironment(t, telegramToken, telegramOwner, pathDictionaries) + defer resetConfig(t) var config Config + assert.NotPanics(t, func() { config = GetConfig() }) - assert.NotEmpty(t, config) - assert.IsType(t, Config{}, config) + assert.NotNil(t, config) assert.Equal(t, telegramToken, config.API.Token) assert.Equal(t, telegramOwner, config.API.Owner) assert.Equal(t, pathDictionaries, config.Paths.Docs) } -func TestGetConfigTwice(t *testing.T) { - initTelegramBotEnvironment(":: telegram telegram token ::", 1234567890, `docs/dictionaries`) - defer cleanTelegramBotEnvironment() +func TestGetConfigTwice(t *testing.T) { //nolint:paralleltest // because this test mock globals + initTelegramBotEnvironment(t, telegramToken, telegramOwner, pathDictionaries) + defer resetConfig(t) config1 := GetConfig() config2 := GetConfig() - assert.NotEmpty(t, config1) - assert.NotEmpty(t, config2) + assert.NotNil(t, config1) + assert.NotNil(t, config2) assert.Equal(t, config1, config2) assert.NotSame(t, config1, config2) } -func TestGetConfigAndSingletonIsImmutable(t *testing.T) { - telegramToken := ":: telegram telegram token ::" - telegramOwner := 1234567890 - pathDictionaries := `docs/dictionaries` - - initTelegramBotEnvironment(telegramToken, telegramOwner, pathDictionaries) - defer cleanTelegramBotEnvironment() +func TestGetConfigAndSingletonIsImmutable(t *testing.T) { //nolint:paralleltest // because this test mock globals + initTelegramBotEnvironment(t, telegramToken, telegramOwner, pathDictionaries) + defer resetConfig(t) config1 := GetConfig() @@ -63,13 +60,12 @@ func TestGetConfigAndSingletonIsImmutable(t *testing.T) { assert.Equal(t, pathDictionaries, config2.Paths.Docs) } -func TestGetConfigWithOnlyRequiredEnvironment(t *testing.T) { - telegramToken := ":: telegram telegram token ::" - telegramOwner := 1234567890 +func TestGetConfigWithOnlyRequiredEnvironment(t *testing.T) { //nolint:paralleltest // because this test mock globals + t.Setenv("TOKEN", telegramToken) + t.Setenv("OWNER", strconv.Itoa(telegramOwner)) - _ = os.Setenv("TOKEN", telegramToken) - _ = os.Setenv("OWNER", strconv.Itoa(telegramOwner)) - defer cleanTelegramBotEnvironment() + resetConfig(t) + defer resetConfig(t) config := GetConfig() @@ -80,27 +76,24 @@ func TestGetConfigWithOnlyRequiredEnvironment(t *testing.T) { assert.Equal(t, `docs`, config.Paths.Docs) } -func TestGetConfigWithEmptyEnvironment(t *testing.T) { - cleanTelegramBotEnvironment() +func TestGetConfigWithEmptyEnvironment(t *testing.T) { //nolint:paralleltest // because this test mock globals + resetConfig(t) + defer resetConfig(t) assert.Panics(t, func() { GetConfig() }) } -func initTelegramBotEnvironment(telegramToken string, telegramOwner int, pathDictionaries string) { - resetConfig() - _ = os.Setenv("TOKEN", telegramToken) - _ = os.Setenv("OWNER", strconv.Itoa(telegramOwner)) - _ = os.Setenv("DOCS_PATH", pathDictionaries) +func initTelegramBotEnvironment(t *testing.T, telegramToken string, telegramOwner int, pathDictionaries string) { + t.Helper() + t.Setenv("TOKEN", telegramToken) + t.Setenv("OWNER", strconv.Itoa(telegramOwner)) + t.Setenv("DOCS_PATH", pathDictionaries) + resetConfig(t) } -func cleanTelegramBotEnvironment() { - _ = os.Unsetenv("TOKEN") - _ = os.Unsetenv("OWNER") - _ = os.Unsetenv("DOCS_PATH") - resetConfig() -} +func resetConfig(t *testing.T) { + t.Helper() -func resetConfig() { cfg = Config{} once = sync.Once{} -} \ No newline at end of file +} From 5f88b64cbe8fac5913a5a3833a7a6dc2e0c2cc35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=24i=D1=85?= Date: Sun, 7 Aug 2022 00:20:37 +0300 Subject: [PATCH 09/22] feat: play application on docker under debug --- .run/remote docker debug.run.xml | 6 ++++ Debug.Dockerfile | 11 ++++--- Dockerfile | 3 +- README.md | 11 ++++++- go.sum | 55 -------------------------------- main.go | 8 +++-- 6 files changed, 29 insertions(+), 65 deletions(-) create mode 100644 .run/remote docker debug.run.xml diff --git a/.run/remote docker debug.run.xml b/.run/remote docker debug.run.xml new file mode 100644 index 0000000..209d21a --- /dev/null +++ b/.run/remote docker debug.run.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/Debug.Dockerfile b/Debug.Dockerfile index 5986f9d..d4d7be5 100644 --- a/Debug.Dockerfile +++ b/Debug.Dockerfile @@ -26,9 +26,10 @@ RUN go build -gcflags="all=-N -l" -o /build/app . FROM alpine:latest # Add entrypoint dependency -#RUN apk add --no-cache bash mysql-client mariadb-connector-c +RUN apk add --no-cache bash +#RUN apk add --no-cache mysql-client mariadb-connector-c -# Move to /backoffice_gateway directory as the place for resulting binary folder +# Move to /app directory as the place for resulting binary folder WORKDIR / # Copy binary from build to main folder @@ -36,8 +37,8 @@ COPY --from=builder /go/bin/dlv . COPY --from=builder /build/app app # Copy some application assets -COPY --from=builder /source/deploy/docker/environment.sh ./docker/environment.sh -COPY --from=builder /source/deploy/docker/logging.sh ./docker/logging.sh +COPY --from=builder /source/deploy/docker/environment.sh ./deploy/docker/environment.sh +COPY --from=builder /source/deploy/docker/logging.sh ./deploy/docker/logging.sh COPY --from=builder /source/entrypoint.sh . #COPY --from=builder /source/migrations ./migrations #COPY ./database/data.json /database/data.json @@ -45,7 +46,7 @@ COPY --from=builder /source/entrypoint.sh . # Export necessary port EXPOSE 4040 -# entry and start php-fpm server +# entry and start entrypoint with some actions and checks before CMD ENTRYPOINT ["bash", "/entrypoint.sh" ] # Command to run when starting the container diff --git a/Dockerfile b/Dockerfile index 36a3988..1857eb7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,7 +27,8 @@ FROM scratch #FROM alpine:latest # ## Add entrypoint dependency -#RUN apk add --no-cache bash mysql-client mariadb-connector-c +#RUN apk add --no-cache bash +#RUN apk add --no-cache mysql-client mariadb-connector-c # Move to /app directory as the place for resulting binary folder WORKDIR / diff --git a/README.md b/README.md index d4cac33..3edf47b 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,11 @@ Build container ```bash docker build . -t psssix/golang-app:latest ``` +and container with debugger +```bash +docker build . -f Debug.Dockerfile -t psssix/golang-app:debug +``` + Push container ```bash docker push psssix/golang-app:latest @@ -11,5 +16,9 @@ docker push psssix/golang-app:latest #### Run docker container Run container on docker ```bash -docker run -d --env-file .env.docker --name golang-app psssix/golang-app:latest +docker run -d --name golang-app --env-file .env.docker psssix/golang-app:latest +``` +and container with debugger +```bash +docker run -d --name golang-app --security-opt="apparmor=unconfined" --cap-add=SYS_PTRACE -p 4040:4040 --env-file .env.docker psssix/golang-app:debug ``` \ No newline at end of file diff --git a/go.sum b/go.sum index b7b58d7..534f915 100644 --- a/go.sum +++ b/go.sum @@ -1,75 +1,20 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598 h1:j2XRGH5Y5uWtBYXGwmrjKeM/kfu/jh7ZcnrGvyN5Ttk= -github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598/go.mod h1:sduMkaHcXDIWurl/Bd/z0rNEUHw5tr6LUA9IO8E9o0o= -github.com/cdipaolo/sentiment v0.0.0-20200617002423-c697f64e7f10 h1:6dGQY3apkf7lG3a1UFhS6grlo009buPFVy79RvNVUF4= -github.com/cdipaolo/sentiment v0.0.0-20200617002423-c697f64e7f10/go.mod h1:JWoVf4GJxCxM3iCiZSVoXNMV+JFG49L+ou70KK3HTvQ= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= -github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= -github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14= -github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= -github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= -github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= -github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= -github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= -github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= -github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= -github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY= -github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= -github.com/go-telegram-bot-api/telegram-bot-api v1.0.0 h1:HXVtsZ+yINQeyyhPFAUU4yKmeN+iFhJ87jXZOC016gs= -github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU= -github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM= -github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= -github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= -github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/hlts2/gohot v0.0.0-20180508065504-3f530881705a h1:6pvgqGIP2wn9fkQn/WcdISxajzxU1GDt8Qfktc4mARM= -github.com/hlts2/gohot v0.0.0-20180508065504-3f530881705a/go.mod h1:c3g+4iw3eH6M6TJ54z/qNYLBEW0oj5Rzqt7JYFRSJmA= -github.com/ikawaha/kagome v1.11.2 h1:eCWpLqv5Euqa5JcwkaobUSy6uGM8rwwMw5Su3eRepBI= -github.com/ikawaha/kagome v1.11.2/go.mod h1:lHwhkGuuWqKWTxeQMppD0EmQAfKbc39QKx9qoWqgo+A= github.com/ilyakaznacheev/cleanenv v1.2.5 h1:/SlcF9GaIvefWqFJzsccGG/NJdoaAwb7Mm7ImzhO3DM= github.com/ilyakaznacheev/cleanenv v1.2.5/go.mod h1:/i3yhzwZ3s7hacNERGFwvlhwXMDcaqwIzmayEhbRplk= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= -github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= -github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= -github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= -github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= -github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= -github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= -github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM= -github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog= -github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= -github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= -github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= -github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= olympos.io/encoding/edn v0.0.0-20200308123125-93e3b8dd0e24 h1:sreVOrDp0/ezb0CHKVek/l7YwpxPJqv+jT3izfSphA4= diff --git a/main.go b/main.go index 332e39c..1d634a9 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,14 @@ package main import ( - "log" "golang-app/config" + "log" ) func main() { cfg := config.GetConfig() - log.Printf("Telegram token is \"%s\", owner id is %d", cfg.API.Token, cfg.API.Owner) - log.Println(cfg) + log.Printf("Telegram token is %q", cfg.API.Token) + log.Printf("Owner id is %d", cfg.API.Owner) + + log.Println("Config struct:", cfg) } From 7b75541300c03b04c7973be740ee88fa4db16c19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=24i=D1=85?= Date: Sun, 7 Aug 2022 00:47:11 +0300 Subject: [PATCH 10/22] feat: describe debuging with docker --- ... => debug remote docker container.run.xml} | 2 +- README.md | 31 +++++++++++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) rename .run/{remote docker debug.run.xml => debug remote docker container.run.xml} (51%) diff --git a/.run/remote docker debug.run.xml b/.run/debug remote docker container.run.xml similarity index 51% rename from .run/remote docker debug.run.xml rename to .run/debug remote docker container.run.xml index 209d21a..5a471f7 100644 --- a/.run/remote docker debug.run.xml +++ b/.run/debug remote docker container.run.xml @@ -1,5 +1,5 @@ - + diff --git a/README.md b/README.md index 3edf47b..b8fffb0 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,43 @@ +# Standard Go Project Template +This is a template for Go application projects based on [Standard Go Project Layout](https://github.com/golang-standards/project-layout). + +### Build, run and debug application locally + +### Build, run and debug application with docker + +#### Create environment file for docker +Create and fill environment file from template `.env.dist` +``` shell +cp .env.dist .env.docker +``` + #### Build docker container Build container -```bash +``` shell docker build . -t psssix/golang-app:latest ``` and container with debugger -```bash +``` shell docker build . -f Debug.Dockerfile -t psssix/golang-app:debug ``` Push container -```bash +``` shell docker push psssix/golang-app:latest ``` #### Run docker container Run container on docker -```bash +``` shell docker run -d --name golang-app --env-file .env.docker psssix/golang-app:latest ``` and container with debugger -```bash +``` shell docker run -d --name golang-app --security-opt="apparmor=unconfined" --cap-add=SYS_PTRACE -p 4040:4040 --env-file .env.docker psssix/golang-app:debug -``` \ No newline at end of file +``` + +#### Debug docker container +1. Create and fill `.env.docker` file by template `.env.dist`. +2. Build docker container with debugger. +3. Run container on docker with debugger. +4. Play debug configuration names as "debug remote docker container". \ No newline at end of file From 2eaad07c50e74e857c031bfe4498af9ee2ac80cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=24i=D1=85?= Date: Sun, 7 Aug 2022 00:56:50 +0300 Subject: [PATCH 11/22] feat: use Standard Go Project Layout as project structure template --- .dockerignore | 25 +++++++++++++++++++++---- .gitignore | 26 ++++++++++++++++++++------ main.go | 2 +- {config => pkg/config}/Config.go | 0 {config => pkg/config}/Config_test.go | 0 5 files changed, 42 insertions(+), 11 deletions(-) rename {config => pkg/config}/Config.go (100%) rename {config => pkg/config}/Config_test.go (100%) diff --git a/.dockerignore b/.dockerignore index fc23a21..d90bc82 100644 --- a/.dockerignore +++ b/.dockerignore @@ -18,19 +18,36 @@ /.gitlab-ci.yml /.golangci.yaml -# OS +# Mac OS X files .DS_Store -# artefacts +# Binaries for programs and plugins *.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 +.glide/ + +# Artefacts /bin /build /data /docs + +# Dependency directories (remove the comment below to include it) /node_modules /vendor -# environment +# Environment /.env /.env.docker -/config.yaml \ No newline at end of file +/config.yaml diff --git a/.gitignore b/.gitignore index 5fcfef4..f3ded8d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,24 +1,38 @@ -# go generations -*.exe - # IDEA project files /.idea # Visual Studio Code workspace files /.vscode /golang-app.code-workspace -# OS +# Mac OS X files .DS_Store -# artefacts +# Binaries for programs and plugins *.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 +.glide/ + +# Artefacts /bin /build /data + +# Dependency directories (remove the comment below to include it) /node_modules /vendor -# environment +# Environment /.env /.env.docker /config.yaml diff --git a/main.go b/main.go index 1d634a9..277dd5c 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,7 @@ package main import ( - "golang-app/config" + "golang-app/pkg/config" "log" ) diff --git a/config/Config.go b/pkg/config/Config.go similarity index 100% rename from config/Config.go rename to pkg/config/Config.go diff --git a/config/Config_test.go b/pkg/config/Config_test.go similarity index 100% rename from config/Config_test.go rename to pkg/config/Config_test.go From 06100b4209a3a558fb199849a7c1d48494751b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=24i=D1=85?= Date: Sun, 7 Aug 2022 00:59:03 +0300 Subject: [PATCH 12/22] feat: use Standard Go Project Layout as project structure template --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index b8fffb0..deae4de 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,12 @@ This is a template for Go application projects based on [Standard Go Project Lay ### Build, run and debug application locally +#### Create environment file +Create and fill environment file from template `.env.dist` +``` shell +cp .env.dist .env +``` + ### Build, run and debug application with docker #### Create environment file for docker From d2b4bdf3244ffd233078d587972a1e070862f6ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=24i=D1=85?= Date: Sun, 7 Aug 2022 23:05:39 +0300 Subject: [PATCH 13/22] feat: build, run and test application locally --- Makefile | 21 +++++++++++++++++++++ README.md | 18 +++++++++++++++++- go.mod | 12 +++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..17d544b --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +SRC := $(shell find . -name "*.go" | grep -v -e .pb.go -e .pb.micro.go) + +.DEFAULT_GOAL := help + +ifneq (,$(wildcard ./.env)) + include .env + export +endif + +fmt: ## Format and fix import order + goimports -w -local "e46" $(SRC) + +help: ## Display this help screen + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' + +build: + go build -ldflags "-s -w" -o build/app . + +run: + make build-backoffice-gateway + ./build/app diff --git a/README.md b/README.md index deae4de..5840047 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Standard Go Project Template This is a template for Go application projects based on [Standard Go Project Layout](https://github.com/golang-standards/project-layout). -### Build, run and debug application locally +### Build, run and test application locally #### Create environment file Create and fill environment file from template `.env.dist` @@ -9,6 +9,22 @@ Create and fill environment file from template `.env.dist` cp .env.dist .env ``` +#### Build application +``` shell +go mod tidy +go build -ldflags "-s -w" -o build/app . +``` + +#### Run application +``` shell +env $(cat .env|xargs) ./build/app +``` + +#### Test application +``` shell +go test ./... +``` + ### Build, run and debug application with docker #### Create environment file for docker diff --git a/go.mod b/go.mod index 5ebd455..a60288b 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,18 @@ module golang-app -go 1.15 +go 1.19 require ( github.com/ilyakaznacheev/cleanenv v1.2.5 github.com/stretchr/testify v1.6.1 ) + +require ( + github.com/BurntSushi/toml v0.3.1 // indirect + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/joho/godotenv v1.3.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v2 v2.2.2 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect + olympos.io/encoding/edn v0.0.0-20200308123125-93e3b8dd0e24 // indirect +) From 8c6ded67946a31a92c9e03855206e6292ed8807d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=24i=D1=85?= Date: Sun, 7 Aug 2022 23:21:08 +0300 Subject: [PATCH 14/22] refactor: use bin instead of build directory --- .dockerignore | 1 - .gitignore | 1 - Debug.Dockerfile | 4 ++-- Dockerfile | 4 ++-- Makefile | 2 +- 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/.dockerignore b/.dockerignore index d90bc82..5c18963 100644 --- a/.dockerignore +++ b/.dockerignore @@ -39,7 +39,6 @@ # Artefacts /bin -/build /data /docs diff --git a/.gitignore b/.gitignore index f3ded8d..7e11089 100644 --- a/.gitignore +++ b/.gitignore @@ -25,7 +25,6 @@ # Artefacts /bin -/build /data # Dependency directories (remove the comment below to include it) diff --git a/Debug.Dockerfile b/Debug.Dockerfile index d4d7be5..52556c4 100644 --- a/Debug.Dockerfile +++ b/Debug.Dockerfile @@ -21,7 +21,7 @@ RUN go mod download COPY . . # Build the application -RUN go build -gcflags="all=-N -l" -o /build/app . +RUN go build -gcflags="all=-N -l" -o bin/app . FROM alpine:latest @@ -34,7 +34,7 @@ WORKDIR / # Copy binary from build to main folder COPY --from=builder /go/bin/dlv . -COPY --from=builder /build/app app +COPY --from=builder /source/bin/app app # Copy some application assets COPY --from=builder /source/deploy/docker/environment.sh ./deploy/docker/environment.sh diff --git a/Dockerfile b/Dockerfile index 1857eb7..2281b0e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,7 @@ RUN go mod download COPY . . # Build the application -RUN go build -ldflags "-s -w" -o /build/app . +RUN go build -ldflags "-s -w" -o /bin/app . FROM scratch @@ -35,7 +35,7 @@ WORKDIR / # Copy binary from build to main folder COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ -COPY --from=builder /build/app app +COPY --from=builder /bin/app app # Copy some application assets #COPY --from=builder /source/deploy/docker/environment.sh ./deploy/docker/environment.sh diff --git a/Makefile b/Makefile index 17d544b..19c1fd9 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ help: ## Display this help screen @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' build: - go build -ldflags "-s -w" -o build/app . + go build -ldflags "-s -w" -o bin/app . run: make build-backoffice-gateway From ff29c49867585720e18ff3e6b10b25ca3d36b3a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=24i=D1=85?= Date: Sun, 7 Aug 2022 23:25:20 +0300 Subject: [PATCH 15/22] refactor: use bin instead of build directory --- .run/go build smart-bot.run.xml | 2 +- ...go test smart-bot.run.xml => go test application.run.xml} | 5 ++--- Dockerfile | 4 ++-- Makefile | 2 +- README.md | 4 ++-- 5 files changed, 8 insertions(+), 9 deletions(-) rename .run/{go test smart-bot.run.xml => go test application.run.xml} (93%) diff --git a/.run/go build smart-bot.run.xml b/.run/go build smart-bot.run.xml index 610c48d..a327a6b 100644 --- a/.run/go build smart-bot.run.xml +++ b/.run/go build smart-bot.run.xml @@ -16,7 +16,7 @@ - + \ No newline at end of file diff --git a/.run/go test smart-bot.run.xml b/.run/go test application.run.xml similarity index 93% rename from .run/go test smart-bot.run.xml rename to .run/go test application.run.xml index 476221d..e33d0c8 100644 --- a/.run/go test smart-bot.run.xml +++ b/.run/go test application.run.xml @@ -1,6 +1,5 @@ - - - + + \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 2281b0e..999b112 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,7 @@ RUN go mod download COPY . . # Build the application -RUN go build -ldflags "-s -w" -o /bin/app . +RUN go build -ldflags "-s -w" -o bin/app . FROM scratch @@ -35,7 +35,7 @@ WORKDIR / # Copy binary from build to main folder COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ -COPY --from=builder /bin/app app +COPY --from=builder /source/bin/app app # Copy some application assets #COPY --from=builder /source/deploy/docker/environment.sh ./deploy/docker/environment.sh diff --git a/Makefile b/Makefile index 19c1fd9..9d6d715 100644 --- a/Makefile +++ b/Makefile @@ -18,4 +18,4 @@ build: run: make build-backoffice-gateway - ./build/app + ./bin/app diff --git a/README.md b/README.md index 5840047..28dda5f 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,12 @@ cp .env.dist .env #### Build application ``` shell go mod tidy -go build -ldflags "-s -w" -o build/app . +go build -ldflags "-s -w" -o bin/app . ``` #### Run application ``` shell -env $(cat .env|xargs) ./build/app +env $(cat .env|xargs) ./bin/app ``` #### Test application From 0e447986968c2e459e1385a76b2d248b7ad890ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=24i=D1=85?= Date: Sun, 7 Aug 2022 23:32:24 +0300 Subject: [PATCH 16/22] fix: corrected run command --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 28dda5f..b4d25d5 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ go build -ldflags "-s -w" -o bin/app . #### Run application ``` shell -env $(cat .env|xargs) ./bin/app + env $(cat .env|grep -v -e #|xargs) ./bin/app ``` #### Test application From 2463ed4508f4fdbe50cfef5056ab341c7aa83653 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=24i=D1=85?= Date: Sun, 7 Aug 2022 23:37:41 +0300 Subject: [PATCH 17/22] feat: use configs folder for config templates --- README.md | 8 ++++++-- .env.dist => configs/.env.dist | 0 2 files changed, 6 insertions(+), 2 deletions(-) rename .env.dist => configs/.env.dist (100%) diff --git a/README.md b/README.md index b4d25d5..db9ca95 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,14 @@ # Standard Go Project Template This is a template for Go application projects based on [Standard Go Project Layout](https://github.com/golang-standards/project-layout). + + ### Build, run and test application locally #### Create environment file Create and fill environment file from template `.env.dist` ``` shell -cp .env.dist .env +cp configs/.env.dist .env ``` #### Build application @@ -25,12 +27,14 @@ go build -ldflags "-s -w" -o bin/app . go test ./... ``` + + ### Build, run and debug application with docker #### Create environment file for docker Create and fill environment file from template `.env.dist` ``` shell -cp .env.dist .env.docker +cp configs/.env.dist .env.docker ``` #### Build docker container diff --git a/.env.dist b/configs/.env.dist similarity index 100% rename from .env.dist rename to configs/.env.dist From 689ec8f8f4072f9ea619730b77aad2bf428f89de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=24i=D1=85?= Date: Mon, 8 Aug 2022 00:09:43 +0300 Subject: [PATCH 18/22] feat: add JB run configuration --- ...uild and run application in docker.run.xml | 13 ++++++++++++ .run/debug application.run.xml | 21 +++++++++++++++++++ ...l => go build and run application.run.xml} | 4 ++-- .run/go test application.run.xml | 1 + 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 .run/build and run application in docker.run.xml create mode 100644 .run/debug application.run.xml rename .run/{go build smart-bot.run.xml => go build and run application.run.xml} (82%) diff --git a/.run/build and run application in docker.run.xml b/.run/build and run application in docker.run.xml new file mode 100644 index 0000000..6b7fb15 --- /dev/null +++ b/.run/build and run application in docker.run.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.run/debug application.run.xml b/.run/debug application.run.xml new file mode 100644 index 0000000..d2c2bd9 --- /dev/null +++ b/.run/debug application.run.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.run/go build smart-bot.run.xml b/.run/go build and run application.run.xml similarity index 82% rename from .run/go build smart-bot.run.xml rename to .run/go build and run application.run.xml index a327a6b..279c97a 100644 --- a/.run/go build smart-bot.run.xml +++ b/.run/go build and run application.run.xml @@ -1,6 +1,6 @@ - - + +