Finish multistage-building
This commit is contained in:
commit
ad113f2339
@ -1,11 +1,52 @@
|
|||||||
# go generations
|
|
||||||
*.exe
|
|
||||||
|
|
||||||
# IDEA project files
|
# IDEA project files
|
||||||
/.idea
|
/.idea
|
||||||
|
/.run
|
||||||
# Visual Studio Code workspace files
|
# Visual Studio Code workspace files
|
||||||
/workstations.code-workspace
|
/.vscode
|
||||||
|
/golang-app.code-workspace
|
||||||
|
|
||||||
# vendoring
|
# git
|
||||||
/vendor/
|
/.git
|
||||||
/build/
|
/.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
|
||||||
|
|||||||
41
.gitignore
vendored
41
.gitignore
vendored
@ -1,14 +1,37 @@
|
|||||||
# go generations
|
|
||||||
*.exe
|
|
||||||
|
|
||||||
# IDEA project files
|
# IDEA project files
|
||||||
/.idea
|
/.idea
|
||||||
# Visual Studio Code workspace files
|
# Visual Studio Code workspace files
|
||||||
/workstations.code-workspace
|
/.vscode
|
||||||
|
/golang-app.code-workspace
|
||||||
|
|
||||||
# vendoring
|
# Mac OS X files
|
||||||
/vendor/
|
.DS_Store
|
||||||
/build/pkg/
|
|
||||||
|
|
||||||
.env
|
# Binaries for programs and plugins
|
||||||
.env.docker
|
*.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
|
||||||
|
|||||||
13
.run/build and run application with docker.run.xml
Normal file
13
.run/build and run application with docker.run.xml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="build and run application with docker" type="docker-deploy" factoryName="dockerfile" server-name="Docker">
|
||||||
|
<deployment type="dockerfile">
|
||||||
|
<settings>
|
||||||
|
<option name="imageTag" value="psssix/golang-app:latest" />
|
||||||
|
<option name="containerName" value="golang-app" />
|
||||||
|
<option name="commandLineOptions" value="--env-file .env.docker" />
|
||||||
|
<option name="sourceFilePath" value="Dockerfile" />
|
||||||
|
</settings>
|
||||||
|
</deployment>
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
22
.run/build and run application.run.xml
Normal file
22
.run/build and run application.run.xml
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="build and run application" type="GoApplicationRunConfiguration" factoryName="Go Application">
|
||||||
|
<module name="golang-app" />
|
||||||
|
<working_directory value="$PROJECT_DIR$" />
|
||||||
|
<EXTENSION ID="net.ashald.envfile">
|
||||||
|
<option name="IS_ENABLED" value="true" />
|
||||||
|
<option name="IS_SUBST" value="false" />
|
||||||
|
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
|
||||||
|
<option name="IS_IGNORE_MISSING_FILES" value="false" />
|
||||||
|
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
|
||||||
|
<ENTRIES>
|
||||||
|
<ENTRY IS_ENABLED="true" PARSER="runconfig" />
|
||||||
|
<ENTRY IS_ENABLED="true" PARSER="env" PATH=".env" />
|
||||||
|
</ENTRIES>
|
||||||
|
</EXTENSION>
|
||||||
|
<kind value="PACKAGE" />
|
||||||
|
<package value="golang-app" />
|
||||||
|
<directory value="$PROJECT_DIR$" />
|
||||||
|
<output_directory value="$PROJECT_DIR$/bin" />
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
11
.run/build and run as service.run.xml
Normal file
11
.run/build and run as service.run.xml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="build and run as service" type="docker-deploy" factoryName="docker-compose.yml" server-name="Docker">
|
||||||
|
<deployment type="docker-compose.yml">
|
||||||
|
<settings>
|
||||||
|
<option name="envFilePath" value="$PROJECT_DIR$/.env.docker" />
|
||||||
|
<option name="sourceFilePath" value="docker-compose.yml" />
|
||||||
|
</settings>
|
||||||
|
</deployment>
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
21
.run/debug application with docker.run.xml
Normal file
21
.run/debug application with docker.run.xml
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="debug application with docker" type="docker-deploy" factoryName="dockerfile" server-name="Docker">
|
||||||
|
<deployment type="dockerfile">
|
||||||
|
<settings>
|
||||||
|
<option name="imageTag" value="psssix/golang-app:debug" />
|
||||||
|
<option name="containerName" value="golang-app" />
|
||||||
|
<option name="portBindings">
|
||||||
|
<list>
|
||||||
|
<DockerPortBindingImpl>
|
||||||
|
<option name="containerPort" value="4040" />
|
||||||
|
<option name="hostPort" value="4040" />
|
||||||
|
</DockerPortBindingImpl>
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
<option name="commandLineOptions" value="--security-opt="apparmor=unconfined" --cap-add=SYS_PTRACE --env-file .env.docker" />
|
||||||
|
<option name="sourceFilePath" value="Debug.Dockerfile" />
|
||||||
|
</settings>
|
||||||
|
</deployment>
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
@ -1,6 +1,6 @@
|
|||||||
<component name="ProjectRunConfigurationManager">
|
<component name="ProjectRunConfigurationManager">
|
||||||
<configuration default="false" name="go build application" type="GoApplicationRunConfiguration" factoryName="Go Application">
|
<configuration default="false" name="debug application" type="GoApplicationRunConfiguration" factoryName="Go Application">
|
||||||
<module name="smart-bot" />
|
<module name="golang-app" />
|
||||||
<working_directory value="$PROJECT_DIR$" />
|
<working_directory value="$PROJECT_DIR$" />
|
||||||
<EXTENSION ID="net.ashald.envfile">
|
<EXTENSION ID="net.ashald.envfile">
|
||||||
<option name="IS_ENABLED" value="true" />
|
<option name="IS_ENABLED" value="true" />
|
||||||
@ -16,7 +16,6 @@
|
|||||||
<kind value="PACKAGE" />
|
<kind value="PACKAGE" />
|
||||||
<package value="golang-app" />
|
<package value="golang-app" />
|
||||||
<directory value="$PROJECT_DIR$" />
|
<directory value="$PROJECT_DIR$" />
|
||||||
<output_directory value="$PROJECT_DIR$/build" />
|
|
||||||
<method v="2" />
|
<method v="2" />
|
||||||
</configuration>
|
</configuration>
|
||||||
</component>
|
</component>
|
||||||
12
.run/debug as service.run.xml
Normal file
12
.run/debug as service.run.xml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="debug as service" type="docker-deploy" factoryName="docker-compose.yml" server-name="Docker">
|
||||||
|
<deployment type="docker-compose.yml">
|
||||||
|
<settings>
|
||||||
|
<option name="envFilePath" value="$PROJECT_DIR$/.env.docker" />
|
||||||
|
<option name="commandLineOptions" value="--build" />
|
||||||
|
<option name="sourceFilePath" value="docker-compose-debug.yml" />
|
||||||
|
</settings>
|
||||||
|
</deployment>
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
6
.run/debug remote docker container.run.xml
Normal file
6
.run/debug remote docker container.run.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="debug remote docker container" type="GoRemoteDebugConfigurationType" factoryName="Go Remote" port="4040">
|
||||||
|
<option name="disconnectOption" value="ASK" />
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
@ -1,6 +1,6 @@
|
|||||||
<component name="ProjectRunConfigurationManager">
|
<component name="ProjectRunConfigurationManager">
|
||||||
<configuration default="false" name="go test application" type="GoTestRunConfiguration" factoryName="Go Test">
|
<configuration default="false" name="test application" type="GoTestRunConfiguration" factoryName="Go Test">
|
||||||
<module name="smart-bot" />
|
<module name="golang-app" />
|
||||||
<working_directory value="$PROJECT_DIR$" />
|
<working_directory value="$PROJECT_DIR$" />
|
||||||
<EXTENSION ID="net.ashald.envfile">
|
<EXTENSION ID="net.ashald.envfile">
|
||||||
<option name="IS_ENABLED" value="true" />
|
<option name="IS_ENABLED" value="true" />
|
||||||
@ -13,11 +13,11 @@
|
|||||||
<ENTRY IS_ENABLED="true" PARSER="env" PATH=".env" />
|
<ENTRY IS_ENABLED="true" PARSER="env" PATH=".env" />
|
||||||
</ENTRIES>
|
</ENTRIES>
|
||||||
</EXTENSION>
|
</EXTENSION>
|
||||||
<framework value="gotest" />
|
|
||||||
<kind value="DIRECTORY" />
|
<kind value="DIRECTORY" />
|
||||||
<package value="smart-bot" />
|
<package value="golang-app" />
|
||||||
<directory value="$PROJECT_DIR$" />
|
<directory value="$PROJECT_DIR$" />
|
||||||
<filePath value="$PROJECT_DIR$" />
|
<filePath value="$PROJECT_DIR$" />
|
||||||
|
<framework value="gotest" />
|
||||||
<method v="2" />
|
<method v="2" />
|
||||||
</configuration>
|
</configuration>
|
||||||
</component>
|
</component>
|
||||||
53
Debug.Dockerfile
Normal file
53
Debug.Dockerfile
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
FROM golang:alpine as builder
|
||||||
|
|
||||||
|
LABEL org.opencontainers.image.authors="psssix <psssix@gmail.com>"
|
||||||
|
|
||||||
|
# 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"]
|
||||||
64
Dockerfile
64
Dockerfile
@ -1,49 +1,57 @@
|
|||||||
FROM golang:alpine
|
FROM golang:alpine as builder
|
||||||
|
|
||||||
MAINTAINER psssix <psssix@gmail.com>
|
LABEL org.opencontainers.image.authors="psssix <psssix@gmail.com>"
|
||||||
|
|
||||||
# Set necessary environmet variables needed for our image
|
# set build environmet variables needed for multistage building
|
||||||
ENV GO111MODULE=on \
|
ENV CGO_ENABLED=0 \
|
||||||
CGO_ENABLED=0 \
|
GOOS=linux
|
||||||
GOOS=linux \
|
|
||||||
GOARCH=amd64
|
|
||||||
|
|
||||||
# Use workdir
|
# source workdir
|
||||||
RUN mkdir /source
|
|
||||||
WORKDIR /source
|
WORKDIR /source
|
||||||
|
|
||||||
# Copy and download dependency using go mod
|
# Copy and download dependency using go mod
|
||||||
COPY go.mod .
|
# Vendoring
|
||||||
COPY go.sum .
|
COPY go.mod go.sum ./
|
||||||
|
RUN go mod download
|
||||||
|
|
||||||
# Copy the code into the container
|
# Copy the code into the container
|
||||||
COPY . .
|
COPY . .
|
||||||
# Vendoring
|
|
||||||
RUN go mod tidy && go mod vendor
|
|
||||||
|
|
||||||
# Build the application
|
# 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
|
FROM scratch
|
||||||
RUN mkdir /app
|
|
||||||
WORKDIR /app
|
# 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
|
# Copy binary from build to main folder
|
||||||
#
|
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
|
||||||
# some application assets
|
COPY --from=builder /source/bin/app app
|
||||||
# RUN cp /source/build/bot . \
|
|
||||||
# && cp -R /source/docs/dictionaries .
|
|
||||||
RUN cp /source/build/golang-app
|
|
||||||
|
|
||||||
# COPY ./database/data.json /dist/database/data.json
|
# Copy some application assets
|
||||||
|
#COPY --from=builder /source/deploy/docker/environment.sh ./deploy/docker/environment.sh
|
||||||
RUN rm -rf /source
|
#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
|
# 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
|
# Command to run when starting the container
|
||||||
#CMD ["/dist/golang-app"]
|
CMD ["/app"]
|
||||||
|
|
||||||
# if have not application daemon
|
# if have not application daemon
|
||||||
#CMD tail -f /dev/null
|
#CMD tail -f /dev/null
|
||||||
21
Makefile
Normal file
21
Makefile
Normal file
@ -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
|
||||||
105
README.md
105
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 docker container
|
||||||
Build container
|
Build container
|
||||||
```bash
|
``` shell
|
||||||
docker build . -t psssix/golang-app:latest
|
docker build . -t psssix/golang-app:latest
|
||||||
```
|
```
|
||||||
|
and container with debugger
|
||||||
|
``` shell
|
||||||
|
docker build . -f Debug.Dockerfile -t psssix/golang-app:debug
|
||||||
|
```
|
||||||
|
|
||||||
Push container
|
Push container
|
||||||
```bash
|
``` shell
|
||||||
docker push psssix/golang-app:latest
|
docker push psssix/golang-app:latest
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Run docker container
|
#### Run docker container
|
||||||
Run container on localhost docker
|
Run container on docker
|
||||||
```bash
|
``` shell
|
||||||
docker run -d --env-file .env.docker --name study-bot psssix/golang-app
|
docker run -d --name golang-app --env-file .env.docker psssix/golang-app:latest
|
||||||
```
|
```
|
||||||
Run container on overcast docker
|
and container with debugger
|
||||||
```bash
|
``` shell
|
||||||
docker pull psssix/golang-app
|
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
|
||||||
docker run -d --env-file .env --name golang-app psssix/golang-app:latest
|
```
|
||||||
```
|
|
||||||
|
#### 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".
|
||||||
@ -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{}
|
|
||||||
}
|
|
||||||
@ -1,4 +1,4 @@
|
|||||||
TOKEN=
|
TOKEN=
|
||||||
OWNER=
|
OWNER=
|
||||||
# some application assets
|
# some application assets
|
||||||
# DOCS_PATH=docs
|
# MIGRATION_PARAM=/migrations
|
||||||
52
deploy/docker/environment.sh
Normal file
52
deploy/docker/environment.sh
Normal file
@ -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
|
||||||
|
|
||||||
31
deploy/docker/logging.sh
Normal file
31
deploy/docker/logging.sh
Normal file
@ -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'
|
||||||
|
}
|
||||||
17
docker-compose-debug.yml
Normal file
17
docker-compose-debug.yml
Normal file
@ -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"
|
||||||
9
docker-compose.yml
Normal file
9
docker-compose.yml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
version: '3.7'
|
||||||
|
|
||||||
|
services:
|
||||||
|
golang-app:
|
||||||
|
container_name: golang-app
|
||||||
|
image: psssix/golang-app:latest
|
||||||
|
environment:
|
||||||
|
- TOKEN=${TOKEN}
|
||||||
|
- OWNER=${OWNER}
|
||||||
25
entrypoint.sh
Normal file
25
entrypoint.sh
Normal file
@ -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 "$@"
|
||||||
12
go.mod
12
go.mod
@ -1,8 +1,18 @@
|
|||||||
module golang-app
|
module golang-app
|
||||||
|
|
||||||
go 1.15
|
go 1.19
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/ilyakaznacheev/cleanenv v1.2.5
|
github.com/ilyakaznacheev/cleanenv v1.2.5
|
||||||
github.com/stretchr/testify v1.6.1
|
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
|
||||||
|
)
|
||||||
|
|||||||
55
go.sum
55
go.sum
@ -1,75 +1,20 @@
|
|||||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
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 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.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 h1:/SlcF9GaIvefWqFJzsccGG/NJdoaAwb7Mm7ImzhO3DM=
|
||||||
github.com/ilyakaznacheev/cleanenv v1.2.5/go.mod h1:/i3yhzwZ3s7hacNERGFwvlhwXMDcaqwIzmayEhbRplk=
|
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 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
||||||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
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/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 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
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 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
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 h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
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=
|
olympos.io/encoding/edn v0.0.0-20200308123125-93e3b8dd0e24 h1:sreVOrDp0/ezb0CHKVek/l7YwpxPJqv+jT3izfSphA4=
|
||||||
|
|||||||
8
main.go
8
main.go
@ -1,12 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"golang-app/pkg/config"
|
||||||
"log"
|
"log"
|
||||||
"golang-app/config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cfg := config.GetConfig()
|
cfg := config.GetConfig()
|
||||||
log.Printf("Telegram token is \"%s\", owner id is %d", cfg.API.Token, cfg.API.Owner)
|
log.Printf("Telegram token is %q", cfg.API.Token)
|
||||||
log.Println(cfg)
|
log.Printf("Owner id is %d", cfg.API.Owner)
|
||||||
|
|
||||||
|
log.Println("Config struct:", cfg)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,23 +8,26 @@ import (
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
API struct {
|
API struct {
|
||||||
Token string `env:"TOKEN" env-layout:"string" env-required:"true"`
|
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 {
|
Paths struct {
|
||||||
Docs string `env:"DOCS_PATH" env-layout:"string" env-default:"docs" env-upd:"true"`
|
Docs string `env:"DOCS_PATH" env-layout:"string" env-default:"docs" env-upd:"true"`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var cfg Config
|
var (
|
||||||
var once sync.Once
|
cfg Config //nolint:gochecknoglobals // singleton globals
|
||||||
|
once sync.Once //nolint:gochecknoglobals // singleton globals
|
||||||
|
)
|
||||||
|
|
||||||
// panic
|
// GetConfig can panic
|
||||||
func GetConfig() Config {
|
func GetConfig() Config {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
err := cleanenv.ReadEnv(&cfg)
|
err := cleanenv.ReadEnv(&cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
99
pkg/config/Config_test.go
Normal file
99
pkg/config/Config_test.go
Normal file
@ -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{}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user