udemy-go-microservices/logger-service/cmd/api/rpc.go
2024-09-06 21:03:23 +02:00

38 lines
662 B
Go

package main
import (
"context"
"fmt"
"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")
now := time.Now()
payload.Data += fmt.Sprintf(" received date %d", now.UnixMicro())
_, 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
}