aboutsummaryrefslogtreecommitdiffstats
path: root/telebot/telebot.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--telebot/telebot.go70
1 files changed, 18 insertions, 52 deletions
diff --git a/telebot/telebot.go b/telebot/telebot.go
index 04bbf1c..0676ce9 100644
--- a/telebot/telebot.go
+++ b/telebot/telebot.go
@@ -2,17 +2,16 @@ package main
import (
"context"
- "encoding/json"
"flag"
- "net/http"
+ "fmt"
+ "net"
"os"
- "os/signal"
- "time"
+ "strconv"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
- "github.com/gorilla/mux"
"github.com/rs/zerolog/log"
pb "github.com/terminaldweller/grpc/telebot/v1"
+ "google.golang.org/grpc"
)
var (
@@ -27,7 +26,8 @@ const (
SERVER_DEPLOYMENT_TYPE = "SERVER_DEPLOYMENT_TYPE"
)
-type NotificationService struct {
+type server struct {
+ pb.UnimplementedNotificationServiceServer
}
func getTGBot() *tgbotapi.BotAPI {
@@ -45,7 +45,7 @@ func sendMessage(bot *tgbotapi.BotAPI, msgText string, channelID int64) error {
return nil
}
-func (s *NotificationService) Notify(ctx context.Context, NotificationRequest *pb.NotificationRequest) (*pb.NotificationResponse, error) {
+func (s *server) Notify(ctx context.Context, NotificationRequest *pb.NotificationRequest) (*pb.NotificationResponse, error) {
var err error
tgbotapi := getTGBot()
if NotificationRequest.ChannelId == 0 {
@@ -59,57 +59,23 @@ func (s *NotificationService) Notify(ctx context.Context, NotificationRequest *p
return &pb.NotificationResponse{Error: "", IsOK: true}, 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)
+func startServer(port uint16) {
+ listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port))
+ if err != nil {
+ log.Fatal().Err(err)
}
- 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) {
+ var opts []grpc.ServerOption
-}
-
-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,
+ grpcServer := grpc.NewServer(opts...)
+ pb.RegisterNotificationServiceServer(grpcServer, &server{})
+ if err := grpcServer.Serve(listener); err != nil {
+ log.Fatal().Err(err)
}
- 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)
+ port, _ := strconv.Atoi(*flagPort)
+ startServer(uint16(port))
}