yats.git

ref: 23690d53dd05b1a2051fcb8745a1c1a8d45165c1

server/grpc/metric-grpc-server.go


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
 * Yats - yats
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Paolo Lulli <kevwe.com>
 * @copyright Paolo Lulli 2024
 */

package grpc

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"log"
	"net"
	"yats-server/db"
	"yats-server/proto"
)

type server struct {
	proto.UnimplementedYatsGrpcServiceServer
}

func (s *server) CreateMetric(ctx context.Context, req *proto.CreateMetricRequest) (*proto.CreateMetricResponse, error) {
	metric := req.GetMetric()
	clientCN := metric.IdClient
	fmt.Printf("%s / %s / %s", clientCN, metric.Name, metric.Value)
	db.SaveMetric(clientCN, metric.Name, metric.Value)

	return &proto.CreateMetricResponse{Metric: metric}, nil
}

func (s *server) CreateEvent(ctx context.Context, req *proto.CreateEventRequest) (*proto.CreateEventResponse, error) {
	event := req.GetEvent()
	clientCN := event.IdClient
	fmt.Printf("%s / %s ", clientCN, event.Name)
	db.SaveEvent(clientCN, event.Name)
	return &proto.CreateEventResponse{Event: event}, nil
}

func RunYatsGrpcServer(address string) {
	lis, err := net.Listen("tcp", address)
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	proto.RegisterYatsGrpcServiceServer(s, &server{})
	log.Printf("Server is running on port %s\n", address)
	if err := s.Serve(lis); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}