-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (56 loc) · 1.89 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
package main
import (
"context"
"fmt"
"log"
"net/http"
interface_pingpong_server "pbkit/pingpong-server/pbkit/interface-pingpong-server"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
httpPort = "8080"
)
type PingPongServer struct {
interface_pingpong_server.UnimplementedPingPongServiceServer
}
type ThrowServiceServer struct {
interface_pingpong_server.UnimplementedThrowServiceServer
}
func (s *PingPongServer) PingPong(ctx context.Context, in *interface_pingpong_server.Ping) (*interface_pingpong_server.Pong, error) {
log.Printf("Received: %s", in.GetHello())
return &interface_pingpong_server.Pong{
World: "Pong",
}, nil
}
func (s *ThrowServiceServer) Throw(ctx context.Context, in *interface_pingpong_server.ThrowRequest) (*interface_pingpong_server.ThrowResponse, error) {
log.Printf("Received: %s", in.GetCode().Enum())
return nil, status.Errorf(codes.Code(in.GetCode().Number()), "%s", in.GetCode().Enum())
}
func main() {
grpcServer := grpc.NewServer()
interface_pingpong_server.RegisterPingPongServiceServer(grpcServer, &PingPongServer{})
interface_pingpong_server.RegisterThrowServiceServer(grpcServer, &ThrowServiceServer{})
wrappedServer := grpcweb.WrapServer(grpcServer,
grpcweb.WithOriginFunc(func(origin string) bool {
return true
}),
)
handler := func(resp http.ResponseWriter, req *http.Request) {
if wrappedServer.IsGrpcWebRequest(req) || wrappedServer.IsAcceptableGrpcCorsRequest(req) {
wrappedServer.ServeHTTP(resp, req)
return
}
http.DefaultServeMux.ServeHTTP(resp, req)
}
httpServer := http.Server{
Addr: fmt.Sprintf(":%v", httpPort),
Handler: http.HandlerFunc(handler),
}
log.Printf("gRPC web server listening at %v", httpPort)
if err := httpServer.ListenAndServe(); err != nil {
log.Fatalf("failed to start http server at %v", httpPort)
}
}