refactor: Update Nats handler and Hasura repository initialization

This commit is contained in:
windyboy
2024-08-05 11:42:33 +08:00
parent 1a858cf19c
commit 3c37410737
3 changed files with 10 additions and 6 deletions
+3 -1
View File
@@ -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
}
+5 -3
View File
@@ -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
+2 -2
View File
@@ -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")},
)