From 99a93f106797a084754ab5b93f44a7f27eb7d180 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Fri, 12 Nov 2021 12:01:15 +0330 Subject: restructured the repo-WIP --- hived/go.mod | 2 +- telebot/go.mod | 2 +- telebot/main.go | 92 ----------------------------------------------------- telebot/telebot | Bin 7115842 -> 0 bytes telebot/telebot.go | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 94 deletions(-) delete mode 100644 telebot/main.go delete mode 100755 telebot/telebot create mode 100644 telebot/telebot.go diff --git a/hived/go.mod b/hived/go.mod index 9df31fa..e3d4d45 100644 --- a/hived/go.mod +++ b/hived/go.mod @@ -1,4 +1,4 @@ -module github.com/terminaldweller/hived +module hived go 1.17 diff --git a/telebot/go.mod b/telebot/go.mod index 71766e5..de7cf3c 100644 --- a/telebot/go.mod +++ b/telebot/go.mod @@ -1,6 +1,6 @@ module telebot -go 1.15 +go 1.17 require ( github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible diff --git a/telebot/main.go b/telebot/main.go deleted file mode 100644 index f6a3bf7..0000000 --- a/telebot/main.go +++ /dev/null @@ -1,92 +0,0 @@ -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 deleted file mode 100755 index 21b84aa..0000000 Binary files a/telebot/telebot and /dev/null differ diff --git a/telebot/telebot.go b/telebot/telebot.go new file mode 100644 index 0000000..f6a3bf7 --- /dev/null +++ b/telebot/telebot.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) +} -- cgit v1.2.3