Giter Club home page Giter Club logo

grpc-web-hacker-news's People

Contributors

dependabot[bot] avatar easycz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grpc-web-hacker-news's Issues

Using Gin framework instead of Chi

I have an existing react web-app, with https://github.com/gin-gonic/gin as the backend. Now I'd like to use your great package, to migrate to grpc.

To begin with, I change the existing sample project to use gin, by the following changes:

func main() {
	grpcServer := grpc.NewServer()
	hackernewsService := hackernews.NewHackerNewsService(nil)
	hackernews_pb.RegisterHackerNewsServiceServer(grpcServer, hackernewsService)

	wrappedGrpc := grpcweb.WrapServer(grpcServer)

+	r := gin.Default()
+	r.Use(LogRequest())
+	r.Use(middleware.NewGrpcWebMiddleware(wrappedGrpc).Handler2())
+	r.Use(cors.New(cors.Config{
+		AllowOrigins:     []string{"http://localhost:3000"},
+		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
+		AllowHeaders:     []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token", "X-GRPC-WEB"},
+		ExposeHeaders:    []string{"Link"},
+		AllowCredentials: true,
+		MaxAge:           time.Duration(300) * time.Second,
+	}))
+	r.GET("/article-proxy", proxy.Article)

+	err := r.Run(":8900")
+	if err != nil {
+		grpclog.Fatalf("failed starting http2 server: %v", err)
+	}
-	router := chi.NewRouter()
-	router.Use(
-		chiMiddleware.Logger,
-		chiMiddleware.Recoverer,
-		middleware.NewGrpcWebMiddleware(wrappedGrpc).Handler, // Must come before general CORS handling
-		cors.New(cors.Options{
-			AllowedOrigins:   []string{"*"},
-			AllowedMethods:   []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
-			AllowedHeaders:   []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
-			ExposedHeaders:   []string{"Link"},
-			AllowCredentials: true,
-			MaxAge:           300, // Maximum value not ignored by any of major browsers
-		}).Handler,
-	)

-	router.Get("/article-proxy", proxy.Article)

-	if err := http.ListenAndServe(":8900", router); err != nil {
-		grpclog.Fatalf("failed starting http2 server: %v", err)
-	}
}

and adding Handler2 method to server/middleware/grpcWeb.go

func (m *GrpcWebMiddleware) Handler2() gin.HandlerFunc {

	return func(c *gin.Context) {
		if m.IsAcceptableGrpcCorsRequest(c.Request) || m.IsGrpcWebRequest(c.Request) {
			m.ServeHTTP(c.Writer, c.Request)
			return
		}

		c.Next()
	}
}

and updating Article method to server/proxy/article.go

-func Article(w http.ResponseWriter, r *http.Request) {
+func Article(c *gin.Context) {
+	r := c.Request
+	w := c.Writer
	queryValues := r.URL.Query()
	url := queryValues.Get("q")
	if url == "" {
		http.Error(w, "Must specify the url to request", 400)
	}
	response, err := http.Get(url)
	if err != nil {
		http.Error(w, "Failed to retrieve article", 500)
	}
	if response.StatusCode >= 400 {
		if err != nil {
			http.Error(w, response.Status, response.StatusCode)
		}
	}
	reader := bufio.NewReader(response.Body)
	reader.WriteTo(w)
}

Everything is compiling and running fine, but I get the following error in the browser, when loading the page: POST http://localhost:8900/grpc_web_hacker_news.HackerNewsService/ListStories 404 (Not Found)

Chrome debugger Netwrok trace:

General:
  Request URL: http://localhost:8900/grpc_web_hacker_news.HackerNewsService/ListStories
  Request Method: POST
  Status Code: 404 Not Found
  Remote Address: [::1]:8900
  Referrer Policy: no-referrer-when-downgrade

Response Headers:
  Access-Control-Allow-Credentials: true
  Access-Control-Allow-Origin: http://localhost:3000
  Content-Type: application/octet-stream
  Date: Sun, 03 Feb 2019 17:20:48 GMT
  Transfer-Encoding: chunked
  Vary: Origin

Request Headers:
  Accept: */*
  Accept-Encoding: gzip, deflate, br
  Accept-Language: en-US,en;q=0.9,fa-IR;q=0.8,fa;q=0.7
  Cache-Control: no-cache
  Connection: keep-alive
  Content-Length: 5
  content-type: application/grpc-web+proto
  Host: localhost:8900
  Origin: http://localhost:3000
  Pragma: no-cache
  Referer: http://localhost:3000/
  User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like 
  Gecko) Chrome/71.0.3578.98 Safari/537.36
  x-grpc-web: 1

Could someone help me find out what I am missing?

Installation of protoc-gen-grpc-web

Hi,

Sorry for an unrelated question
I am currently working on using grpc-web to write a simple client for my service. I have created a service.proto file which was successfully compiled using protoc. The problem arose when I tried to generate the gRPC-Web service client stub using the plugin protoc-gen-grpc-web which doesnt work despite having installed the latter.

Got the following error :
protoc -I=./ service.proto --grpc-web_out=import_style=commonjs,mode=grpcwebtext:./
protoc-gen-grpc-web: program not found or is not executable
--grpc-web_out: protoc-gen-grpc-web: Plugin failed with status code 1.

OS : Ubuntu 18.04

Any suggestions on how to solve this issue? Thank you!

Error running "go run main.go"

I'm seeing this error when I tried to run the server:

command-line-arguments

./main.go:20:47: cannot use grpcServer (type *"google.golang.org/grpc".Server) as type *"github.com/easyCZ/grpc-web-hacker-news/server/vendor/google.golang.org/grpc".Server in argument to grpc_web_hacker_news.RegisterHackerNewsServiceServer
./main.go:28:34: cannot use wrappedGrpc (type *"github.com/improbable-eng/grpc-web/go/grpcweb".WrappedGrpcServer) as type *"github.com/easyCZ/grpc-web-hacker-news/server/vendor/github.com/improbable-eng/grpc-web/go/grpcweb".WrappedGrpcServer in argument to "github.com/easyCZ/grpc-web-hacker-news/server/middleware".NewGrpcWebMiddleware

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.