-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
118 lines (104 loc) · 3.38 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"context"
"fmt"
"log"
"net"
"net/http"
"time"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/s1ntaxe770r/donut-rpc/db"
pb "github.com/s1ntaxe770r/donut-rpc/proto"
"github.com/s1ntaxe770r/donut-rpc/utils"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"google.golang.org/protobuf/types/known/emptypb"
"gorm.io/gorm"
)
const (
port = ":5050"
version = "v0.1"
)
var (
lg = utils.NewDonutLogger()
conn = db.Connect()
rpcMetrics = grpc_prometheus.NewServerMetrics()
reg = prometheus.NewRegistry()
GetDonutCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "get_donut_request_counter",
Help: "number of requests served by the get donut method",
}, []string{"getDonut"})
MakeDonutCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "make_donut_request_counter",
Help: "number of requests served by the make donut method",
}, []string{"MakeDonut"})
GetDonutsCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "get_donuts_request_counter",
Help: "number of requests served by the get donuts method",
}, []string{"getDonuts"})
)
func init() {
reg.MustRegister(rpcMetrics, GetDonutCounter, GetDonutsCounter, MakeDonutCounter)
}
type DonutServer struct {
pb.UnimplementedDonutShopServer
lg *log.Logger
db *gorm.DB
}
func (ds *DonutServer) GetDonut(ctx context.Context, in *pb.DonutRequest) (*pb.Donut, error) {
donut, err := db.GetDonut(ds.db, in)
GetDonutCounter.WithLabelValues(in.Name).Inc()
ds.lg.Println(in)
if err != nil {
return nil, err
}
defer ds.lg.Printf("%s|handle get donut for %v", time.Now().String(), in)
return donut, nil
}
func (ds *DonutServer) GetDonuts(ctx context.Context, in *emptypb.Empty) (*pb.Donuts, error) {
donuts, err := db.GetDonuts(ds.db)
GetDonutsCounter.WithLabelValues("GetDonuts").Inc()
if err != nil {
return nil, err
}
defer ds.lg.Printf("%s|handle get donuts", time.Now().String())
return donuts, nil
}
func (ds *DonutServer) MakeDonut(ctx context.Context, in *pb.Donut) (*pb.DonutRequest, error) {
_, err := db.MakeDonut(ds.db, in)
MakeDonutCounter.WithLabelValues(in.Name).Inc()
if err != nil {
return nil, err
}
defer ds.lg.Printf("%s|handle get make donut for %v", time.Now().String(), in)
return &pb.DonutRequest{Name: in.GetName()}, nil
}
func (ds *DonutServer) GetVersion(ctx context.Context, in *emptypb.Empty) (*pb.Version, error) {
return &pb.Version{Number: version}, nil
}
func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
lg.Panicf("could not open shop %s", err.Error())
}
server := grpc.NewServer(
grpc.StreamInterceptor(rpcMetrics.StreamServerInterceptor()),
grpc.UnaryInterceptor(rpcMetrics.UnaryServerInterceptor()),
)
httpServer := &http.Server{Handler: promhttp.HandlerFor(reg, promhttp.HandlerOpts{}), Addr: fmt.Sprintf("0.0.0.0:%d", 9092)}
reflection.Register(server)
pb.RegisterDonutShopServer(server, &DonutServer{lg: lg, db: conn})
rpcMetrics.InitializeMetrics(server)
lg.Printf("shop opened on %v", lis.Addr())
go func() {
if err := httpServer.ListenAndServe(); err != nil {
log.Fatal("Unable to start a http server.")
}
}()
err = server.Serve(lis)
if err != nil {
lg.Panicf("oops couldn't open up shop %s", err.Error())
}
}