🐳 Create Dockerfile and DockerCompose file and implement authentication with telegram api

This commit is contained in:
Alphyron 2020-04-11 23:27:24 +02:00
parent 6001cdba88
commit 18878de12c
5 changed files with 69 additions and 3 deletions

22
Dockerfile Normal file
View File

@ -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 <admin@dragon-labs.de>"
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"]

View File

@ -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 {

28
docker-compose.yml Normal file
View File

@ -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

2
go.mod
View File

@ -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
)

17
main.go
View File

@ -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)
}