udemy-go-microservices/logger-service/cmd/api/rpc.go

38 lines
662 B
Go
Raw Normal View History

2024-09-06 18:27:53 +00:00
package main
import (
"context"
2024-09-06 19:00:08 +00:00
"fmt"
2024-09-06 18:27:53 +00:00
"log"
"logger/data"
"time"
)
type RPCServer struct{}
type RPCPayload struct {
Name string
Data string
}
func (r *RPCServer) LogInfo(payload RPCPayload, resp *string) error {
collection := client.Database("logs").Collection("logs")
2024-09-06 19:00:08 +00:00
now := time.Now()
payload.Data += fmt.Sprintf(" received date %d", now.UnixMicro())
2024-09-06 18:27:53 +00:00
_, err := collection.InsertOne(context.TODO(), data.LogEntry{
Name: payload.Name,
Data: payload.Data,
CreatedAt: time.Now(),
})
if err != nil {
log.Println("error writing to mongo", err)
return err
}
*resp = "Processed payload via RPC:" + payload.Name
return nil
}