Skip to content
This repository has been archived by the owner on Oct 3, 2019. It is now read-only.
/ go-grpcweb Public archive

grpc-web handler for Go

License

Notifications You must be signed in to change notification settings

erred/go-grpcweb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

depreciated

go-grpcweb

grpc-web handler for go

License GoDoc Build

About

Translates between grpc-web requests and grpc responses

Simply wrap your grpc server with this handler

Usage

Install

go get github.com/seankhliao/go-grpcweb

Use

import (
    "net/http"

    "google.golang.org/grpc"
    grpcweb "github.com/seankhliao/go-grpcweb"

    pb "your-proto-definition"
)

func main(){
    svr := grpc.NewServer()
    hw.RegisterGreeterServer(svr, &Server{})

    // wrap grpc handler in grpc-web handler
    handler := grpcweb.New(svr)
    http.ListenAndServe(":8080", handler)

    // OPTIONAL:
    // handle cors if necessary:
    //  Headers:
    //    Access-Control-Allow-Origin
    //    Access-Control-Allow-Headers
    //  Request:
    //    method: OPTIONS
    //    response: 200
    h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
      w.Header().Set("access-control-allow-origin", "*")
      w.Header().Set("Access-Control-Allow-Headers", "*")
      if r.Method == "OPTIONS" {
        w.WriteHeader(http.StatusOK)
        return
      }
      handler.ServeHTTP(w, r)
    })
    http.ListenAndServe(":8080", h)

}

Todo

  • Write tests
  • Improve error handling
  • investigate closing http2 streams
  • Write better docs (h2c)
  • Cleanup header parsing / constants

Links