-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathmain.go
69 lines (58 loc) · 1.65 KB
/
main.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
package main
import (
"errors"
"net/http"
"github.com/go-chi/chi"
"github.com/rakyll/statik/fs"
"github.com/sirupsen/logrus"
pb "github.com/utrack/clay/doc/example/pb"
"github.com/utrack/clay/v2/log"
"github.com/utrack/clay/v2/transport"
"github.com/utrack/clay/v2/transport/middlewares/mwgrpc"
"github.com/utrack/clay/v2/transport/server"
"golang.org/x/net/context"
// We're using statik-compiled files of Swagger UI
// for the sake of example.
_ "github.com/utrack/clay/doc/example/static/statik"
)
// SumImpl is an implementation of SummatorService.
type SumImpl struct{}
// Sum implements SummatorServer.Sum.
func (s *SumImpl) Sum(ctx context.Context, r *pb.SumRequest) (*pb.SumResponse, error) {
if r.GetA() == 0 {
return nil, errors.New("a is zero")
}
if r.GetB().B == 65536 {
panic(errors.New("we've got a problem"))
}
sum := r.GetA() + r.GetB().B
return &pb.SumResponse{
Sum: sum,
}, nil
}
// GetDescription is a simple alias to the ServiceDesc constructor.
// It makes it possible to register the service implementation @ the server.
func (s *SumImpl) GetDescription() transport.ServiceDesc {
return pb.NewSummatorServiceDesc(s)
}
func main() {
// Wire up our bundled Swagger UI
staticFS, err := fs.New()
if err != nil {
logrus.Fatal(err)
}
hmux := chi.NewRouter()
hmux.Mount("/", http.FileServer(staticFS))
impl := &SumImpl{}
srv := server.NewServer(
12345,
// Pass our mux with Swagger UI
server.WithHTTPMux(hmux),
// Recover from both HTTP and gRPC panics and use our own middleware
server.WithGRPCUnaryMiddlewares(mwgrpc.UnaryPanicHandler(log.Default)),
)
err = srv.Run(impl)
if err != nil {
logrus.Fatal(err)
}
}