-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
63 lines (55 loc) · 1.5 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
package server
import (
"context"
"github.com/firekitz/fk-daemon-iam/config"
iampb "github.com/firekitz/fk-daemon-iam/internal/proto/iam"
log "github.com/firekitz/fk-lib-log-go"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"net"
"net/http"
)
// Run starts the example gRPC service.
// "network" and "address" are passed to net.Listen.
func Run(ctx context.Context, network, address string) error {
l, err := net.Listen(network, address)
if err != nil {
return err
}
defer func() {
if err := l.Close(); err != nil {
log.E("Failed to close %s %s: %v", network, address, err)
}
}()
s := config.Middleware()
iampb.RegisterIamServer(s, new(iamServer))
log.I("Starting listening at %s", address)
go func() {
defer s.GracefulStop()
<-ctx.Done()
}()
return s.Serve(l)
}
//RunInProcessGateway starts the invoke in process http gateway.
func RunInProcessGateway(ctx context.Context, addr string, opts ...runtime.ServeMuxOption) error {
mux := runtime.NewServeMux(opts...)
err := iampb.RegisterIamHandlerServer(ctx, mux, new(iamServer))
if err != nil {
log.E("Don't Handled HTTP: %v", err)
}
s := &http.Server{
Addr: addr,
Handler: mux,
}
go func() {
<-ctx.Done()
log.I("Shutting down the http gateway server")
if err := s.Shutdown(context.Background()); err != nil {
log.E("Failed to shutdown http gateway server: %v", err)
}
}()
if err := s.ListenAndServe(); err != http.ErrServerClosed {
log.E("Failed to listen and serve: %v", err)
return err
}
return nil
}