forked from improbable-eng/grpc-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exampleserver.go
110 lines (92 loc) · 2.81 KB
/
exampleserver.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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/metadata"
"strings"
library "github.com/improbable-eng/grpc-web/example/go/_proto/examplecom/library"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"golang.org/x/net/context"
)
var (
enableTls = flag.Bool("enable_tls", false, "Use TLS - required for HTTP2.")
tlsCertFilePath = flag.String("tls_cert_file", "../../misc/localhost.crt", "Path to the CRT/PEM file.")
tlsKeyFilePath = flag.String("tls_key_file", "../../misc/localhost.key", "Path to the private key file.")
)
func main() {
flag.Parse()
port := 9090
if *enableTls {
port = 9091
}
grpcServer := grpc.NewServer()
library.RegisterBookServiceServer(grpcServer, &bookService{})
grpclog.SetLogger(log.New(os.Stdout, "exampleserver: ", log.LstdFlags))
wrappedServer := grpcweb.WrapServer(grpcServer)
handler := func(resp http.ResponseWriter, req *http.Request) {
wrappedServer.ServeHTTP(resp, req)
}
httpServer := http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: http.HandlerFunc(handler),
}
grpclog.Printf("Starting server. http port: %d, with TLS: %v", port, *enableTls)
if *enableTls {
if err := httpServer.ListenAndServeTLS(*tlsCertFilePath, *tlsKeyFilePath); err != nil {
grpclog.Fatalf("failed starting http2 server: %v", err)
}
} else {
if err := httpServer.ListenAndServe(); err != nil {
grpclog.Fatalf("failed starting http server: %v", err)
}
}
}
type bookService struct{}
var books = []*library.Book{
{
Isbn: 60929871,
Title: "Brave New World",
Author: "Aldous Huxley",
},
{
Isbn: 140009728,
Title: "Nineteen Eighty-Four",
Author: "George Orwell",
},
{
Isbn: 9780140301694,
Title: "Alice's Adventures in Wonderland",
Author: "Lewis Carroll",
},
{
Isbn: 140008381,
Title: "Animal Farm",
Author: "George Orwell",
},
}
func (s *bookService) GetBook(ctx context.Context, bookQuery *library.GetBookRequest) (*library.Book, error) {
grpc.SendHeader(ctx, metadata.Pairs("Pre-Response-Metadata", "Is-sent-as-headers-unary"))
grpc.SetTrailer(ctx, metadata.Pairs("Post-Response-Metadata", "Is-sent-as-trailers-unary"))
for _, book := range books {
if book.Isbn == bookQuery.Isbn {
return book, nil
}
}
return nil, grpc.Errorf(codes.NotFound, "Book could not be found")
}
func (s *bookService) QueryBooks(bookQuery *library.QueryBooksRequest, stream library.BookService_QueryBooksServer) error {
stream.SendHeader(metadata.Pairs("Pre-Response-Metadata", "Is-sent-as-headers-stream"))
for _, book := range books {
if strings.HasPrefix(book.Author, bookQuery.AuthorPrefix) {
stream.Send(book)
}
}
stream.SetTrailer(metadata.Pairs("Post-Response-Metadata", "Is-sent-as-trailers-stream"))
return nil
}