Files
zeekay c01e5afcae refactor: own the module path (github.com/hanzoai/grpc-web)
Per the standing fork rule, a hanzoai fork declares its OWN module path and
is required directly -- never resolved via a `replace` against the upstream
path. Keeping the upstream path meant every consumer's import line carried
the upstream brand.

  module github.com/traefik/grpc-web -> module github.com/hanzoai/grpc-web

All occurrences were import paths (package self-imports plus the test
servers and the react example server). go/grpcweb -- the package Hanzo
Ingress links -- is green.
2026-07-25 12:27:26 -07:00

111 lines
2.8 KiB
Go

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/hanzoai/grpc-web/client/grpc-web-react-example/go/_proto/examplecom/library"
"github.com/hanzoai/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
}