From 3c37410737da351685c98dc37ff61fa0cedf584c Mon Sep 17 00:00:00 2001 From: windyboy Date: Mon, 5 Aug 2024 11:42:33 +0800 Subject: [PATCH] refactor: Update Nats handler and Hasura repository initialization --- cmd/main/main.go | 4 +++- internal/handlers/message_handler.go | 8 +++++--- internal/repository/hasura.go | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cmd/main/main.go b/cmd/main/main.go index 6b83aff..8fde8aa 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -38,12 +38,14 @@ func setupApp() *cli.App { Flags: []cli.Flag{ &cli.StringFlag{ Name: "nats", + Aliases: []string{"n"}, Usage: "Nats server address", Value: "nats://localhost:4222", EnvVars: []string{"NATS_SERVER"}, }, &cli.StringFlag{ Name: "topic", + Aliases: []string{"t"}, Usage: "Nats topic to listen to", Value: "Telegram.Serial", EnvVars: []string{"NATS_SUBJECT"}, @@ -81,7 +83,7 @@ func executeListen(c *cli.Context) error { fmt.Println("Loaded configuration successfully") log := utils.GetLogger() log.Info("Starting nats subscriber") - handler := handlers.NewNatsHandler(cfg) + handler := handlers.New(cfg) nats.Subscribe(cfg, &handlers.PlainTextMarshaler{}, handler) return nil } diff --git a/internal/handlers/message_handler.go b/internal/handlers/message_handler.go index f9c421f..01a9bc3 100644 --- a/internal/handlers/message_handler.go +++ b/internal/handlers/message_handler.go @@ -21,8 +21,11 @@ type MessageHandler struct { hasuraRepo *repository.HasuraRepository } -func NewNatsHandler(config *config.Config) *MessageHandler { - return &MessageHandler{config: config, hasuraRepo: repository.NewHasuraRepo(config.Hasura.Endpoint, config.Hasura.Secret)} +func New(config *config.Config) *MessageHandler { + return &MessageHandler{ + config: config, + hasuraRepo: repository.New(config.Hasura.Endpoint, config.Hasura.Secret), + } } func (n *MessageHandler) HandleMessage(msg *message.Message) error { @@ -39,7 +42,6 @@ func (n *MessageHandler) HandleMessage(msg *message.Message) error { log.Infof("not parsed: [%s] : {%s} \n", msg.UUID, msg.Payload) } else { log.Infof("parsed [%s]: %v\n", msg.UUID, parsed) - } n.SaveMessage(parsed, msg.UUID) return nil diff --git a/internal/repository/hasura.go b/internal/repository/hasura.go index 9518c0d..819fc24 100644 --- a/internal/repository/hasura.go +++ b/internal/repository/hasura.go @@ -17,8 +17,8 @@ type HasuraRepository struct { client graphql.Client } -// NewHasuraRepo creates a new HasuraRepository -func NewHasuraRepo(endpoint, secret string) *HasuraRepository { +// New creates a new HasuraRepository +func New(endpoint, secret string) *HasuraRepository { src := oauth2.StaticTokenSource( &oauth2.Token{AccessToken: os.Getenv("GRAPHQL_TOKEN")}, )