diff options
author | terminaldweller <thabogre@gmail.com> | 2021-12-13 18:57:02 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2021-12-13 18:57:02 +0000 |
commit | a963b5272b30978927d5b3a6dc1a39abca38548d (patch) | |
tree | 16f44c11709c4e52be2a4b4623e3bb0956c0f6a8 /telebot/telebot.go | |
parent | restructuring WIP (diff) | |
download | hived-a963b5272b30978927d5b3a6dc1a39abca38548d.tar.gz hived-a963b5272b30978927d5b3a6dc1a39abca38548d.zip |
grpc for telebot. untested. [WIP]
Diffstat (limited to '')
-rw-r--r-- | telebot/telebot.go | 70 |
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)) } |