-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
65 lines (53 loc) · 1.53 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
package http
import (
"context"
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
"github.com/xfrr/dyschat/proto/rooms/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/encoding/protojson"
)
type Server struct {
name string
httpAddr string
grpcAddr string
log *zerolog.Logger
}
func NewServer(httpAddr, grpcAddr string, log *zerolog.Logger) Server {
return Server{name: "rooms-http-server", httpAddr: httpAddr, grpcAddr: grpcAddr, log: log}
}
func (s *Server) Serve(ctx context.Context) error {
// TODO: Implements TLS
mux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
UseProtoNames: true,
EmitUnpopulated: true,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: true,
},
}))
opts := []grpc.DialOption{
grpc.WithBlock(),
grpc.WithTransportCredentials(
insecure.NewCredentials(),
),
}
// Register Prometheus metrics handler.
mux.HandlePath("GET", "/metrics", s.promHandler)
err := rooms.RegisterRoomsServiceHandlerFromEndpoint(ctx, mux, s.grpcAddr, opts)
if err != nil {
return err
}
s.log.Info().
Str("addr", s.httpAddr).
Msg("starting http server")
return http.ListenAndServe(s.httpAddr, mux)
}
func (s *Server) promHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {
h := promhttp.Handler()
h.ServeHTTP(w, r)
}