easycz / grpc-web-hacker-news Goto Github PK
View Code? Open in Web Editor NEWAn example app implementing a Hacker News reader in React with gRPC-Web and Go backend
License: MIT License
An example app implementing a Hacker News reader in React with gRPC-Web and Go backend
License: MIT License
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?
Getting the error below when running yarn install
in app
folder
node-pre-gyp ERR! Tried to download(404): https://fsevents-binaries.s3-us-west-2.amazonaws.com/v1.1.2/fse-v1.1.2-node-v67-darwin-x64.tar.gz
Hello,
I found this project does not work in go-chi v4 because their deprecation of http.CloseNotify
. Is this confirmed?
details: improbable-eng/grpc-web#337
Only the heading appears on the page. List_stories api call is failing.
I just cloned and executed the start.sh file. @easyCZ
I can't find your webpack configuration file. Your app/README mentions the path webpack.config.prod.js which appears to be missing.
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!
I didn't able to find the typescript version in package json. I tried using 2.8, 3.1.1, 4.4.4 versions but still getting type errors.
Also it would be helpful if you mention node version also.
I'm seeing this error when I tried to run the server:
./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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.