diff --git a/.dockerignore b/.dockerignore
index f93757f..5c18963 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -1,11 +1,52 @@
-# go generations
-*.exe
-
# IDEA project files
/.idea
+/.run
# Visual Studio Code workspace files
-/workstations.code-workspace
+/.vscode
+/golang-app.code-workspace
-# vendoring
-/vendor/
-/build/
\ No newline at end of file
+# git
+/.git
+/.gitignore
+
+# docker
+/Dockerfile
+/docker-compose.yml
+/docker-compose-services.yml
+
+# utilities
+/.gitlab-ci.yml
+/.golangci.yaml
+
+# Mac OS X files
+.DS_Store
+
+# 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
+/data
+/docs
+
+# Dependency directories (remove the comment below to include it)
+/node_modules
+/vendor
+
+# Environment
+/.env
+/.env.docker
+/config.yaml
diff --git a/.gitignore b/.gitignore
index 0f36fad..7e11089 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,14 +1,37 @@
-# go generations
-*.exe
-
# IDEA project files
/.idea
# Visual Studio Code workspace files
-/workstations.code-workspace
+/.vscode
+/golang-app.code-workspace
-# vendoring
-/vendor/
-/build/pkg/
+# Mac OS X files
+.DS_Store
-.env
-.env.docker
+# 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
+/data
+
+# Dependency directories (remove the comment below to include it)
+/node_modules
+/vendor
+
+# Environment
+/.env
+/.env.docker
+/config.yaml
diff --git a/.run/build and run application with docker.run.xml b/.run/build and run application with docker.run.xml
new file mode 100644
index 0000000..1e5f40f
--- /dev/null
+++ b/.run/build and run application with docker.run.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/build and run application.run.xml b/.run/build and run application.run.xml
new file mode 100644
index 0000000..28203e6
--- /dev/null
+++ b/.run/build and run application.run.xml
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/build and run as service.run.xml b/.run/build and run as service.run.xml
new file mode 100644
index 0000000..64d6024
--- /dev/null
+++ b/.run/build and run as service.run.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/debug application with docker.run.xml b/.run/debug application with docker.run.xml
new file mode 100644
index 0000000..35524e5
--- /dev/null
+++ b/.run/debug application with docker.run.xml
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/go build smart-bot.run.xml b/.run/debug application.run.xml
similarity index 77%
rename from .run/go build smart-bot.run.xml
rename to .run/debug application.run.xml
index 610c48d..d2c2bd9 100644
--- a/.run/go build smart-bot.run.xml
+++ b/.run/debug application.run.xml
@@ -1,6 +1,6 @@
-
-
+
+
@@ -16,7 +16,6 @@
-
\ No newline at end of file
diff --git a/.run/debug as service.run.xml b/.run/debug as service.run.xml
new file mode 100644
index 0000000..1735dd9
--- /dev/null
+++ b/.run/debug as service.run.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/debug remote docker container.run.xml b/.run/debug remote docker container.run.xml
new file mode 100644
index 0000000..5a471f7
--- /dev/null
+++ b/.run/debug remote docker container.run.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.run/go test smart-bot.run.xml b/.run/test application.run.xml
similarity index 81%
rename from .run/go test smart-bot.run.xml
rename to .run/test application.run.xml
index 476221d..5736e74 100644
--- a/.run/go test smart-bot.run.xml
+++ b/.run/test application.run.xml
@@ -1,6 +1,6 @@
-
-
+
+
@@ -13,11 +13,11 @@
-
-
+
+
\ No newline at end of file
diff --git a/Debug.Dockerfile b/Debug.Dockerfile
new file mode 100644
index 0000000..52556c4
--- /dev/null
+++ b/Debug.Dockerfile
@@ -0,0 +1,53 @@
+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 bin/app .
+
+FROM alpine:latest
+
+# Add entrypoint dependency
+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 /
+
+# Copy binary from build to main folder
+COPY --from=builder /go/bin/dlv .
+COPY --from=builder /source/bin/app app
+
+# Copy some application assets
+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
+
+# Export necessary port
+EXPOSE 4040
+
+# entry and start entrypoint with some actions and checks before CMD
+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 0f64ee6..999b112 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,49 +1,57 @@
-FROM golang:alpine
+FROM golang:alpine as builder
-MAINTAINER psssix
+LABEL org.opencontainers.image.authors="psssix "
-# 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
# Build the application
-RUN go build -o build/golang-app -mod=vendor .
+RUN go build -ldflags "-s -w" -o bin/app .
-# Move to /smart-bot directory as the place for resulting binary folder
-RUN mkdir /app
-WORKDIR /app
+FROM scratch
+
+# or alpine, if you need install utilities
+#
+#FROM alpine:latest
+#
+## Add entrypoint dependency
+#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 /
# 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 --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
+COPY --from=builder /source/bin/app app
-# COPY ./database/data.json /dist/database/data.json
-
-RUN rm -rf /source
+# Copy some application assets
+#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
# Export necessary port
-# EXPOSE 3000
+#EXPOSE 3000
-ENTRYPOINT ["/app/golang-app"]
+# entry and start entrypoint with some actions and checks before CMD
+#ENTRYPOINT ["bash", "/entrypoint.sh" ]
# 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
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..18131b1
--- /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 "golang-app" $(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 bin/app .
+
+run:
+ make build-backoffice-gateway
+ ./bin/app
diff --git a/README.md b/README.md
index 267fe46..3af345f 100644
--- a/README.md
+++ b/README.md
@@ -1,20 +1,105 @@
+# 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 configs/.env.dist .env
+```
+
+#### Build application
+``` shell
+go mod tidy
+go build -ldflags "-s -w" -o bin/app .
+```
+
+#### Run application
+``` shell
+ env $(cat .env|grep -v -e #|xargs) ./bin/app
+```
+
+#### Test application
+``` shell
+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 configs/.env.dist .env.docker
+```
+
#### Build docker container
Build container
-```bash
+``` shell
docker build . -t psssix/golang-app:latest
```
+and container with debugger
+``` 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 localhost docker
-```bash
- docker run -d --env-file .env.docker --name study-bot psssix/golang-app
+Run container on docker
+``` shell
+docker run -d --name golang-app --env-file .env.docker psssix/golang-app:latest
```
-Run container on overcast docker
-```bash
-docker pull psssix/golang-app
-docker run -d --env-file .env --name golang-app psssix/golang-app:latest
-```
\ No newline at end of file
+and container with debugger
+``` 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
+```
+
+#### 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".
+
+
+
+### Build, run and debug application with docker-compose
+
+#### Create environment file for docker
+Create and fill environment file from template `.env.dist`
+``` shell
+cp configs/.env.dist .env.docker
+```
+
+#### Build docker container
+Build container
+``` shell
+docker build . -t psssix/golang-app:latest
+```
+and container with debugger
+``` shell
+docker build . -f Debug.Dockerfile -t psssix/golang-app:debug
+```
+
+#### Run docker container
+Run container on docker
+``` shell
+docker up -d --env-file .env.docker
+```
+and container with debugger
+``` shell
+docker up -d --env-file .env.docker -f docker-compose-debug.yml
+```
+
+#### 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
diff --git a/config/Config_test.go b/config/Config_test.go
deleted file mode 100644
index ec6f8b9..0000000
--- a/config/Config_test.go
+++ /dev/null
@@ -1,106 +0,0 @@
-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`
-
- initTelegramBotEnvironment(telegramToken, telegramOwner, pathDictionaries)
- defer cleanTelegramBotEnvironment()
-
- var config Config
- assert.NotPanics(t, func() { config = GetConfig() })
-
- assert.NotEmpty(t, config)
- assert.IsType(t, Config{}, 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()
-
- config1 := GetConfig()
- config2 := GetConfig()
-
- assert.NotEmpty(t, config1)
- assert.NotEmpty(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()
-
- config1 := GetConfig()
-
- assert.Equal(t, telegramToken, config1.API.Token)
- assert.Equal(t, telegramOwner, config1.API.Owner)
- assert.Equal(t, pathDictionaries, config1.Paths.Docs)
-
- config1.API.Token = ":: some another telegram token ::"
- config1.API.Owner = 1
- config1.Paths.Docs = ":: some else path ::"
- config2 := GetConfig()
-
- assert.Equal(t, telegramToken, config2.API.Token)
- assert.Equal(t, telegramOwner, config2.API.Owner)
- assert.Equal(t, pathDictionaries, config2.Paths.Docs)
-}
-
-func TestGetConfigWithOnlyRequiredEnvironment(t *testing.T) {
- telegramToken := ":: telegram telegram token ::"
- telegramOwner := 1234567890
-
- _ = os.Setenv("TOKEN", telegramToken)
- _ = os.Setenv("OWNER", strconv.Itoa(telegramOwner))
- defer cleanTelegramBotEnvironment()
-
- config := GetConfig()
-
- assert.NotEmpty(t, config)
- assert.IsType(t, Config{}, config)
- assert.Equal(t, telegramToken, config.API.Token)
- assert.Equal(t, telegramOwner, config.API.Owner)
- assert.Equal(t, `docs`, config.Paths.Docs)
-}
-
-func TestGetConfigWithEmptyEnvironment(t *testing.T) {
- cleanTelegramBotEnvironment()
-
- 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 cleanTelegramBotEnvironment() {
- _ = os.Unsetenv("TOKEN")
- _ = os.Unsetenv("OWNER")
- _ = os.Unsetenv("DOCS_PATH")
- resetConfig()
-}
-
-func resetConfig() {
- cfg = Config{}
- once = sync.Once{}
-}
\ No newline at end of file
diff --git a/.env.dist b/configs/.env.dist
similarity index 57%
rename from .env.dist
rename to configs/.env.dist
index 30028a9..95848bd 100644
--- a/.env.dist
+++ b/configs/.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/deploy/docker/environment.sh b/deploy/docker/environment.sh
new file mode 100644
index 0000000..518f241
--- /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_PARAM' ''
+# # 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/docker-compose-debug.yml b/docker-compose-debug.yml
new file mode 100644
index 0000000..faa50f1
--- /dev/null
+++ b/docker-compose-debug.yml
@@ -0,0 +1,17 @@
+version: '3.7'
+
+services:
+ golang-app-debug:
+ container_name: golang-app
+ build:
+ context: .
+ dockerfile: Debug.Dockerfile
+ security_opt:
+ - apparmor=unconfined
+ cap_add:
+ - SYS_PTRACE
+ environment:
+ - TOKEN=${TOKEN}
+ - OWNER=${OWNER}
+ ports:
+ - "4040:4040"
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..c515aac
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,9 @@
+version: '3.7'
+
+services:
+ golang-app:
+ container_name: golang-app
+ image: psssix/golang-app:latest
+ environment:
+ - TOKEN=${TOKEN}
+ - OWNER=${OWNER}
\ No newline at end of file
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 "$@"
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
+)
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..277dd5c 100644
--- a/main.go
+++ b/main.go
@@ -1,12 +1,14 @@
package main
import (
+ "golang-app/pkg/config"
"log"
- "golang-app/config"
)
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)
}
diff --git a/config/Config.go b/pkg/config/Config.go
similarity index 63%
rename from config/Config.go
rename to pkg/config/Config.go
index dffe9fa..0ec4bc4 100644
--- a/config/Config.go
+++ b/pkg/config/Config.go
@@ -8,23 +8,26 @@ 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"`
}
}
-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)
if err != nil {
- panic(err.Error())
+ panic(err)
}
})
+
return cfg
}
diff --git a/pkg/config/Config_test.go b/pkg/config/Config_test.go
new file mode 100644
index 0000000..ca73b53
--- /dev/null
+++ b/pkg/config/Config_test.go
@@ -0,0 +1,99 @@
+package config
+
+import (
+ "github.com/stretchr/testify/assert"
+ "strconv"
+ "sync"
+ "testing"
+)
+
+const (
+ telegramToken = ":: telegram telegram token ::" //nolint:gosec // it is not real credentials
+ telegramOwner = 1234567890
+ pathDictionaries = `docs/dictionaries`
+)
+
+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.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) { //nolint:paralleltest // because this test mock globals
+ initTelegramBotEnvironment(t, telegramToken, telegramOwner, pathDictionaries)
+ defer resetConfig(t)
+
+ config1 := GetConfig()
+ config2 := GetConfig()
+
+ assert.NotNil(t, config1)
+ assert.NotNil(t, config2)
+ assert.Equal(t, config1, config2)
+ assert.NotSame(t, config1, config2)
+}
+
+func TestGetConfigAndSingletonIsImmutable(t *testing.T) { //nolint:paralleltest // because this test mock globals
+ initTelegramBotEnvironment(t, telegramToken, telegramOwner, pathDictionaries)
+ defer resetConfig(t)
+
+ config1 := GetConfig()
+
+ assert.Equal(t, telegramToken, config1.API.Token)
+ assert.Equal(t, telegramOwner, config1.API.Owner)
+ assert.Equal(t, pathDictionaries, config1.Paths.Docs)
+
+ config1.API.Token = ":: some another telegram token ::"
+ config1.API.Owner = 1
+ config1.Paths.Docs = ":: some else path ::"
+ config2 := GetConfig()
+
+ assert.Equal(t, telegramToken, config2.API.Token)
+ assert.Equal(t, telegramOwner, config2.API.Owner)
+ assert.Equal(t, pathDictionaries, config2.Paths.Docs)
+}
+
+func TestGetConfigWithOnlyRequiredEnvironment(t *testing.T) { //nolint:paralleltest // because this test mock globals
+ t.Setenv("TOKEN", telegramToken)
+ t.Setenv("OWNER", strconv.Itoa(telegramOwner))
+
+ resetConfig(t)
+ defer resetConfig(t)
+
+ config := GetConfig()
+
+ assert.NotEmpty(t, config)
+ assert.IsType(t, Config{}, config)
+ assert.Equal(t, telegramToken, config.API.Token)
+ assert.Equal(t, telegramOwner, config.API.Owner)
+ assert.Equal(t, `docs`, config.Paths.Docs)
+}
+
+func TestGetConfigWithEmptyEnvironment(t *testing.T) { //nolint:paralleltest // because this test mock globals
+ resetConfig(t)
+ defer resetConfig(t)
+
+ assert.Panics(t, func() { GetConfig() })
+}
+
+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 resetConfig(t *testing.T) {
+ t.Helper()
+
+ cfg = Config{}
+ once = sync.Once{}
+}