refactor: Update config.dev.toml and sub.go for Hasura integration

The code changes in config.dev.toml and sub.go update the configuration and message handling for integrating with Hasura. The config.dev.toml file now includes a new section for Hasura configuration, specifying the endpoint and secret. In sub.go, the UUID field is set to the UUID of the received message, ensuring proper tracking of parsed messages. This refactor enables seamless integration with Hasura and improves the functionality of the code.
This commit is contained in:
windyboy
2024-07-22 16:23:15 +08:00
parent 3115162454
commit 53f03bbc0a
9 changed files with 131 additions and 9 deletions
+6
View File
@@ -20,6 +20,7 @@ type Config struct {
Nats NatsConfig
Subscription SubscriptionConfig
Timeouts TimeoutsConfig
Hasura HasuraConfig
}
type NatsConfig struct {
@@ -50,6 +51,11 @@ type PatternConfig struct {
Expression *regexp.Regexp
}
type HasuraConfig struct {
Endpoint string
Secret string
}
// LoggerConfig represents the configuration for the logger.
type LoggerConfig struct {
ZapConfig zap.Config `json:"zapConfig"`
+7
View File
@@ -38,6 +38,10 @@ server_timeout = "30s"
reconnect_wait = "10s"
close_timeout = "10s"
ack_wait_timeout = "5s"
[hasura]
endpoint = "http://localhost:8080/v1/graphql"
secret = "aviation-test"
`
tmpFile, err := os.CreateTemp("", "config.*.toml")
Expect(err).NotTo(HaveOccurred())
@@ -68,6 +72,9 @@ ack_wait_timeout = "5s"
Expect(cfg.Nats.Client).To(Equal("test-client"))
Expect(cfg.Nats.URL).To(Equal("nats://localhost:4222"))
Expect(cfg.Subscription.Topic).To(Equal("example-topic"))
Expect(cfg.Subscription.QueueGroup).To(Equal("example-group"))
Expect(cfg.Hasura.Endpoint).To(Equal("http://localhost:8080/v1/graphql"))
Expect(cfg.Hasura.Secret).To(Equal("aviation-test"))
})
})
+2 -1
View File
@@ -86,7 +86,8 @@ NeedDispatch: false.
// ParsedMessage holds the parsed data from an aviation message
type ParsedMessage struct {
StartIndicator string `json:"startIndicator"` // 电报开始标识: The start of the message indicator (e.g., 'ZCZC').
// StartIndicator string `json:"startIndicator"` // 电报开始标识: The start of the message indicator (e.g., 'ZCZC').
Uuid string `json:"uuid"`
MessageID string `json:"messageId"` // 信息ID: The message ID (e.g., 'TMQ1324').
DateTime string `json:"dateTime"` // 日期时间: The date and time of the message (e.g., '150631').
PriorityIndicator string `json:"priorityIndicator"` // 优先级标识: The priority level of the message (e.g., 'FF').
+1
View File
@@ -67,6 +67,7 @@ func (n *NatsHandler) handleMessage(msg *message.Message) error {
payload := string(msg.Payload)
if parsed, err := parsers.Parse(payload); err != nil {
// log.Error("error parsing message", err, map[string]interface{}{"payload": payload})
parsed.Uuid = msg.UUID
fmt.Print("error parsing message", err)
return err
} else {
+17 -3
View File
@@ -149,19 +149,33 @@ func createBodyData(data map[string]string) (interface{}, error) {
}
}
// Parse parses the raw text message and returns a ParsedMessage.
// Parse parses the raw text message and returns a ParsedMessage.
func Parse(rawText string) (*domain.ParsedMessage, error) {
// Parse the header of the message
message, err := ParseHeader(rawText)
if err != nil {
return nil, err
}
// Initialize a new body parser
bodyParser := NewBodyParser()
// Parse the body and footer of the message
bodyData, err := bodyParser.Parse(message.BodyAndFooter)
if err != nil {
return nil, err
// Return the message with the parsed header and the error
// message.ParsedAt = time.Now()
return &message, err
}
// Set the parsed time to the current time
message.ParsedAt = time.Now()
// Assign the parsed body data to the message
message.BodyData = bodyData
// Return the fully parsed message
return &message, nil
}
@@ -188,7 +202,7 @@ func ParseHeader(fullMessage string) (domain.ParsedMessage, error) {
// fullMessage = strings.TrimSpace(fullMessage)
lines := strings.Split(fullMessage, "\n")
startIndicator, messageID, dateTime, err := parseStartIndicator(lines[0])
_, messageID, dateTime, err := parseStartIndicator(lines[0])
if err != nil {
return domain.ParsedMessage{}, err
}
@@ -202,7 +216,7 @@ func ParseHeader(fullMessage string) (domain.ParsedMessage, error) {
secondaryAddresses, originator, originatorDateTime, bodyAndFooter := parseRemainingLines(lines[2:])
return domain.ParsedMessage{
StartIndicator: startIndicator,
// StartIndicator: startIndicator,
MessageID: messageID,
DateTime: dateTime,
PriorityIndicator: priorityIndicator,
+2 -2
View File
@@ -54,7 +54,7 @@ ALTERNATE ROUTES ADVISED)
NNNN`
parsedHeader, err := ParseHeader(message)
Expect(err).ToNot(HaveOccurred())
Expect(parsedHeader.StartIndicator).To(Equal("ZCZC"))
// Expect(parsedHeader.StartIndicator).To(Equal("ZCZC"))
Expect(parsedHeader.MessageID).To(Equal("TAF6789"))
Expect(parsedHeader.DateTime).To(Equal("160530"))
Expect(parsedHeader.PriorityIndicator).To(Equal("QU"))
@@ -97,7 +97,7 @@ ALL DEPARTURES/ARRIVALS EXPECTED TO BE DELAYED)
NNNN`
parsedHeader, err := ParseHeader(message)
Expect(err).ToNot(HaveOccurred())
Expect(parsedHeader.StartIndicator).To(Equal("ZCZC"))
// Expect(parsedHeader.StartIndicator).To(Equal("ZCZC"))
Expect(parsedHeader.MessageID).To(Equal("NOTAM1122"))
Expect(parsedHeader.DateTime).To(Equal("171000"))
Expect(parsedHeader.PriorityIndicator).To(Equal("QU"))
+83
View File
@@ -0,0 +1,83 @@
package repository
import (
"context"
"fmt"
"os"
"caatsm/internal/domain"
"github.com/hasura/go-graphql-client"
"golang.org/x/oauth2"
)
type HasuraRepository struct {
hasuraClient *graphql.Client
}
// NewHasuraClient creates a new HasuraClient
func NewHasuraRepo(endpoint, secret string) *HasuraRepository {
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GRAPHQL_TOKEN")},
)
httpClient := oauth2.NewClient(context.Background(), src)
return &HasuraRepository{
hasuraClient: graphql.NewClient(endpoint, httpClient),
}
}
var mutation struct {
InsertParsedMessages struct {
Returning []domain.ParsedMessage `json:"returning"`
} `
graphql:"insert_parsed_messages(objects: {
startIndicator: $startIndicator,
messageId: $messageId,
dateTime: $dateTime,
priorityIndicator: $priorityIndicator,
primaryAddress: $primaryAddress,
secondaryAddresses: $secondaryAddresses,
originator: $originator,
originatorDateTime: $originatorDateTime,
category: $category,
bodyAndFooter: $bodyAndFooter,
bodyData: $bodyData,
receivedAt: $receivedAt,
parsedAt: $parsedAt,
dispatchedAt: $dispatchedAt,
needDispatch: $needDispatch
})"`
}
// InsertParsedMessage inserts a new ParsedMessage into the Hasura GraphQL API
func (hr *HasuraRepository) InsertParsedMessage(pm domain.ParsedMessage) error {
variables := map[string]interface{}{
// "startIndicator": graphql.String(pm.StartIndicator),
"messageId": graphql.String(pm.MessageID),
"dateTime": graphql.String(pm.DateTime),
"priorityIndicator": graphql.String(pm.PriorityIndicator),
"primaryAddress": graphql.String(pm.PrimaryAddress),
"secondaryAddresses": pm.SecondaryAddresses,
"originator": graphql.String(pm.Originator),
"originatorDateTime": graphql.String(pm.OriginatorDateTime),
"category": graphql.String(pm.Category),
"bodyAndFooter": graphql.String(pm.BodyAndFooter),
"bodyData": pm.BodyData,
"receivedAt": pm.ReceivedAt,
"parsedAt": pm.ParsedAt,
"dispatchedAt": pm.DispatchedAt,
"needDispatch": pm.NeedDispatch,
}
ctx := context.Background()
err := hr.hasuraClient.Mutate(ctx, &mutation, variables)
if err != nil {
return err
}
for _, returnedMessage := range mutation.InsertParsedMessages.Returning {
fmt.Printf("Inserted ParsedMessage: %+v\n", returnedMessage)
}
return nil
}