From 0fad033ff38707e07011b7008d506cf4fcdaa7ec Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Fri, 12 Nov 2021 11:57:17 +0330 Subject: restructured the repo-WIP --- telebot/Dockerfile | 18 +++++++++ telebot/docker-entrypoint.sh | 8 ++++ telebot/go.mod | 10 +++++ telebot/go.sum | 35 ++++++++++++++++ telebot/main.go | 92 +++++++++++++++++++++++++++++++++++++++++++ telebot/telebot | Bin 0 -> 7115842 bytes 6 files changed, 163 insertions(+) create mode 100644 telebot/Dockerfile create mode 100755 telebot/docker-entrypoint.sh create mode 100644 telebot/go.mod create mode 100644 telebot/go.sum create mode 100644 telebot/main.go create mode 100755 telebot/telebot (limited to 'telebot') diff --git a/telebot/Dockerfile b/telebot/Dockerfile new file mode 100644 index 0000000..0a9e3e2 --- /dev/null +++ b/telebot/Dockerfile @@ -0,0 +1,18 @@ +FROM alpine:3.13 as builder +RUN apk update && apk upgrade +RUN apk add go git +COPY go.* /hived/ +RUN cd /hived && go mod download +COPY *.go /hived/ +RUN cd /hived && go build + +FROM node:lts-alpine3.13 AS certbuilder +RUN apk add openssl +WORKDIR /certs +RUN openssl req -nodes -new -x509 -subj="/C=US/ST=Denial/L=springfield/O=Dis/CN=localhost" -keyout server.key -out server.cert + +FROM gcr.io/distroless/static-debian10 +COPY --from=certbuilder /cert/ /cert/ +COPY --from=builder /hived/hived /hived/ +COPY ./docker-entrypoint.sh /hived/ +ENTRYPOINT ["/hived/docker-entrypoint.sh"] diff --git a/telebot/docker-entrypoint.sh b/telebot/docker-entrypoint.sh new file mode 100755 index 0000000..932c9c9 --- /dev/null +++ b/telebot/docker-entrypoint.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env sh +set -ex + +export $(cat /run/secrets/tg_bot_token) +export $(cat /run/secrets/ch_api_key) +export $(cat /run/secrets/ch_api_secret) + +"/hived/hived" "$@" diff --git a/telebot/go.mod b/telebot/go.mod new file mode 100644 index 0000000..71766e5 --- /dev/null +++ b/telebot/go.mod @@ -0,0 +1,10 @@ +module telebot + +go 1.15 + +require ( + github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible + github.com/gorilla/mux v1.8.0 + github.com/rs/zerolog v1.26.0 + github.com/technoweenie/multipartstreamer v1.0.1 // indirect +) diff --git a/telebot/go.sum b/telebot/go.sum new file mode 100644 index 0000000..ca57a1e --- /dev/null +++ b/telebot/go.sum @@ -0,0 +1,35 @@ +github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +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/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.26.0 h1:ORM4ibhEZeTeQlCojCK2kPz1ogAY4bGs4tD+SaAdGaE= +github.com/rs/zerolog v1.26.0/go.mod h1:yBiM87lvSqX8h0Ww4sdzNSkVYZ8dL2xjZJG1lAuGZEo= +github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM= +github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog= +github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/telebot/main.go b/telebot/main.go new file mode 100644 index 0000000..f6a3bf7 --- /dev/null +++ b/telebot/main.go @@ -0,0 +1,92 @@ +package main + +import ( + "context" + "encoding/json" + "flag" + "net/http" + "os" + "os/signal" + "time" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "github.com/gorilla/mux" + "github.com/rs/zerolog/log" +) + +var flagPort = flag.String("port", "8000", "determined the port the sercice runs on") + +// FIXME-the client should provide the channel ID +var botChannelID = flag.Int64("botchannelid", 146328407, "determines the channel id the telgram bot should send messages to") + +const TELEGRAM_BOT_TOKEN_ENV_VAR = "TELEGRAM_BOT_TOKEN" + +func getTGBot() *tgbotapi.BotAPI { + token := os.Getenv(TELEGRAM_BOT_TOKEN_ENV_VAR) + bot, err := tgbotapi.NewBotAPI(token[1 : len(token)-1]) + if err != nil { + log.Error().Err(err) + } + return bot +} + +func sendMessage(bot *tgbotapi.BotAPI, msgText string) error { + msg := tgbotapi.NewMessage(*botChannelID, msgText) + bot.Send(msg) + return nil +} + +func healthHandler(w http.ResponseWriter, r *http.Request) { + var telebotError string + IsTelebotOk := true + + w.Header().Add("Content-Type", "application/json") + if r.Method != "GET" { + http.Error(w, "Method is not supported.", http.StatusNotFound) + } + + w.WriteHeader(http.StatusOK) + + json.NewEncoder(w).Encode(struct { + IsHivedOk bool `json:"isTelebotOK"` + TelebotError string `json:"telebotError"` + }{IsHivedOk: IsTelebotOk, TelebotError: telebotError}) +} + +func msgHandler(w http.ResponseWriter, r *http.Request) { + +} + +func startServer(gracefulWait time.Duration) { + r := mux.NewRouter() + srv := &http.Server{ + Addr: "0.0.0.0:" + *flagPort, + WriteTimeout: time.Second * 15, + ReadTimeout: time.Second * 15, + Handler: r, + } + r.HandleFunc("/health", healthHandler) + r.HandleFunc("/msg", msgHandler) + + go func() { + if err := srv.ListenAndServe(); err != nil { + log.Fatal().Err(err) + } + }() + + c := make(chan os.Signal, 1) + + signal.Notify(c, os.Interrupt) + <-c + ctx, cancel := context.WithTimeout(context.Background(), gracefulWait) + defer cancel() + srv.Shutdown(ctx) + log.Info().Msg("gracefully shut down the server") +} + +func main() { + var gracefulWait time.Duration + flag.DurationVar(&gracefulWait, "gracefulwait", time.Second*15, "the duration to wait during the graceful shutdown") + flag.Parse() + startServer(gracefulWait) +} diff --git a/telebot/telebot b/telebot/telebot new file mode 100755 index 0000000..21b84aa Binary files /dev/null and b/telebot/telebot differ -- cgit v1.2.3