commit 304b9c73813edc5ce0c735787a2c424a53419cc0 Author: psssix Date: Sat Feb 20 00:39:46 2021 +0300 start commit, first version of golang application sample with configuration management and docker diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f93757f --- /dev/null +++ b/.dockerignore @@ -0,0 +1,11 @@ +# go generations +*.exe + +# IDEA project files +/.idea +# Visual Studio Code workspace files +/workstations.code-workspace + +# vendoring +/vendor/ +/build/ \ No newline at end of file diff --git a/.env.dist b/.env.dist new file mode 100644 index 0000000..30028a9 --- /dev/null +++ b/.env.dist @@ -0,0 +1,4 @@ +TOKEN= +OWNER= +# some application assets +# DOCS_PATH=docs \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0f36fad --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +# go generations +*.exe + +# IDEA project files +/.idea +# Visual Studio Code workspace files +/workstations.code-workspace + +# vendoring +/vendor/ +/build/pkg/ + +.env +.env.docker diff --git a/.run/go build smart-bot.run.xml b/.run/go build smart-bot.run.xml new file mode 100644 index 0000000..610c48d --- /dev/null +++ b/.run/go build smart-bot.run.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.run/go test smart-bot.run.xml b/.run/go test smart-bot.run.xml new file mode 100644 index 0000000..476221d --- /dev/null +++ b/.run/go test smart-bot.run.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0f64ee6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,49 @@ +FROM golang:alpine + +MAINTAINER psssix + +# Set necessary environmet variables needed for our image +ENV GO111MODULE=on \ + CGO_ENABLED=0 \ + GOOS=linux \ + GOARCH=amd64 + +# Use workdir +RUN mkdir /source +WORKDIR /source + +# Copy and download dependency using go mod +COPY go.mod . +COPY go.sum . +# 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 . + +# Move to /smart-bot directory as the place for resulting binary folder +RUN mkdir /app +WORKDIR /app + +# 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 + +# Export necessary port +# EXPOSE 3000 + +ENTRYPOINT ["/app/golang-app"] + +# Command to run when starting the container +#CMD ["/dist/golang-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 new file mode 100644 index 0000000..267fe46 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +#### Build docker container +Build container +```bash +docker build . -t psssix/golang-app:latest +``` +Push container +```bash +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 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 diff --git a/config/Config.go b/config/Config.go new file mode 100644 index 0000000..dffe9fa --- /dev/null +++ b/config/Config.go @@ -0,0 +1,30 @@ +package config + +import ( + "github.com/ilyakaznacheev/cleanenv" + "sync" +) + +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"` + } + Paths struct { + Docs string `env:"DOCS_PATH" env-layout:"string" env-default:"docs" env-upd:"true"` + } +} + +var cfg Config +var once sync.Once + +// panic +func GetConfig() Config { + once.Do(func() { + err := cleanenv.ReadEnv(&cfg) + if err != nil { + panic(err.Error()) + } + }) + return cfg +} diff --git a/config/Config_test.go b/config/Config_test.go new file mode 100644 index 0000000..ec6f8b9 --- /dev/null +++ b/config/Config_test.go @@ -0,0 +1,106 @@ +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/docs/.gitkeep b/docs/.gitkeep new file mode 100644 index 0000000..58fbb78 --- /dev/null +++ b/docs/.gitkeep @@ -0,0 +1 @@ +some application assets directory \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5ebd455 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module golang-app + +go 1.15 + +require ( + github.com/ilyakaznacheev/cleanenv v1.2.5 + github.com/stretchr/testify v1.6.1 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b7b58d7 --- /dev/null +++ b/go.sum @@ -0,0 +1,76 @@ +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= +olympos.io/encoding/edn v0.0.0-20200308123125-93e3b8dd0e24/go.mod h1:oVgVk4OWVDi43qWBEyGhXgYxt7+ED4iYNpTngSLX2Iw= diff --git a/main.go b/main.go new file mode 100644 index 0000000..332e39c --- /dev/null +++ b/main.go @@ -0,0 +1,12 @@ +package main + +import ( + "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) +}