From 18878de12c127c1ae7a7d1846af90598f572c0d6 Mon Sep 17 00:00:00 2001 From: Alphyron Date: Sat, 11 Apr 2020 23:27:24 +0200 Subject: [PATCH] :whale: Create Dockerfile and DockerCompose file and implement authentication with telegram api --- Dockerfile | 22 ++++++++++++++++++++++ config/telegram.go | 3 ++- docker-compose.yml | 28 ++++++++++++++++++++++++++++ go.mod | 2 ++ main.go | 17 +++++++++++++++-- 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ffd1030 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +# build stage +FROM golang:1.13-stretch AS build-env +COPY . /src +WORKDIR /src +ENV GO113MODULE=on +RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go test ./... -cover -coverprofile=c.out #gosetup +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o goapp + +# final stage +FROM alpine +LABEL maintainer="Alphyron " + +RUN apk update \ + && apk upgrade \ + && apk add --no-cache \ + ca-certificates \ + && update-ca-certificates 2>/dev/null || true + +WORKDIR /app +COPY --from=build-env /src . +RUN chmod +x ./goapp +CMD [ "./goapp"] \ No newline at end of file diff --git a/config/telegram.go b/config/telegram.go index adc96f8..91a8283 100644 --- a/config/telegram.go +++ b/config/telegram.go @@ -3,7 +3,8 @@ package config import "github.com/caarlos0/env" type TelegramConfig struct { - TelegramKey string `env:"TELEGRAM_KEY,required"` + TelegramKey string `env:"TELEGRAM_KEY,required"` + TelegramDebug bool `env:"TELEGRAM_DEBUG" envDefault:"false"` } func (tc *TelegramConfig) LoadConfig() error { diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e92abff --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,28 @@ +version: '3.7' + +services: + app: + image: group_helper:latest + build: + context: . + dockerfile: Dockerfile + restart: on-failure + environment: + - TELEGRAM_KEY=340059059:AAG8MRgRmdk5nI_hye5HkSwQyV9wqwoUSbA + - DB_DEBUG=true + - DB_TYPE=mysql + - DB_HOST=mysql + - DB_PORT=3306 + - DB_USER=go + - DB_PASS=go + - DB_DATABASE=go + + mysql: + image: mysql:8 + environment: + - MYSQL_RANDOM_ROOT_PASSWORD=true + - MYSQL_USER=go + - MYSQL_PASSWORD=go + - MYSQL_DATABASE=go + ports: + - 3306:3306 \ No newline at end of file diff --git a/go.mod b/go.mod index 8c6088a..eacd872 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,8 @@ go 1.13 require ( github.com/caarlos0/env v3.5.0+incompatible + github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible github.com/jinzhu/gorm v1.9.12 github.com/stretchr/testify v1.5.1 // indirect + github.com/technoweenie/multipartstreamer v1.0.1 // indirect ) diff --git a/main.go b/main.go index 2ef8d2b..8c39388 100644 --- a/main.go +++ b/main.go @@ -3,20 +3,33 @@ package main import ( "git.dragon-labs.de/alphyron/group_helper/config" "log" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" ) func main() { - err := config.DatabaseConfig{}.LoadConfig() + dbConfig := config.DatabaseConfig{} + err := dbConfig.LoadConfig() if err != nil { log.Println("Problem while loading database environment variables") log.Fatal(err) } - err = config.TelegramConfig{}.LoadConfig() + tgConfig := config.TelegramConfig{} + err = tgConfig.LoadConfig() if err != nil { log.Println("Problem while loading telegram environment variables") log.Fatal(err) } + + bot, err := tgbotapi.NewBotAPI(tgConfig.TelegramKey) + if err != nil { + log.Println("Problem while authenticate at telegram") + log.Fatal(err) + } + + bot.Debug = tgConfig.TelegramDebug + log.Printf("Authorized on account %s", bot.Self.UserName) }