Git Product home page Git Product logo

grpc-gateway-boilerplate's Introduction

grpc-gateway-boilerplate

Run on Google Cloud

All the boilerplate you need to get started with writing grpc-gateway powered REST services in Go.

Requirements

Go 1.17+

Running

Running main.go starts a web server on https://0.0.0.0:11000/. You can configure the port used with the $PORT environment variable, and to serve on HTTP set $SERVE_HTTP=true.

$ go run main.go

An OpenAPI UI is served on https://0.0.0.0:11000/.

Running the standalone server

If you want to use a separate gRPC server, for example one written in Java or C++, you can run the standalone web server instead:

$ go run ./cmd/standalone/ --server-address dns:///0.0.0.0:10000

Getting started

After cloning the repo, there are a couple of initial steps;

  1. If you forked this repo, or cloned it into a different directory from the github structure, you will need to correct the import paths. Here's a nice find one-liner for accomplishing this (replace yourscmprovider.com/youruser/yourrepo with your cloned repo path):
    $ find . -path ./vendor -prune -o -type f \( -name '*.go' -o -name '*.proto' -o -name '*.yaml' \) -exec sed -i -e "s;github.com/johanbrandhorst/grpc-gateway-boilerplate;yourscmprovider.com/youruser/yourrepo;g" {} +
  2. Finally, generate the files with make generate.

Now you can run the web server with go run main.go.

Making it your own

The next step is to define the interface you want to expose in proto/users/v1/user.proto. See https://developers.google.com/protocol-buffers/ tutorials and guides on writing Protobuf files. See the Buf style guide for tips on how to structure your packages.

Once that is done, regenerate the files using make generate. This will mean you'll need to implement any functions in server/server.go, or else the build will fail since your struct won't be implementing the interface defined by the generated files anymore.

This should hopefully be all you need to get started playing around with the gRPC-Gateway!

grpc-gateway-boilerplate's People

Contributors

abhishekvrshny avatar clavinjune avatar dmitris avatar insanitywholesale avatar johanbrandhorst avatar mfridman avatar stefanvanburen 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-gateway-boilerplate's Issues

Cannot find module for path embed

Hi there, thanks for providing this boilerplate repo.

I've checked this repo out and followed the instructions, running make install then make generate.

When running go main.go run the command errors out with this message:

build github.com/johanbrandhorst/grpc-gateway-boilerplate: cannot find module for path embed

Do you have any idea what could be happening here?

Can I also use grpc-web?

I'm trying to figure out how I would ALSO be able to open up a way for the mux to grab http 1.1 requests that are sendt via grpc-web.

It's not an issue, but I'm asking for some help, even for a good place to gather wisdom from.

See grpc-web:

grpcServer := grpc.Server()
wrappedGrpc := grpcweb.WrapServer(grpcServer)
tlsHttpServer.Handler = http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
    if wrappedGrpc.IsGrpcWebRequest(req) {
	    wrappedGrpc.ServeHTTP(resp, req)
    }
    // Fall back to other servers.
    http.DefaultServeMux.ServeHTTP(resp, req)
})

I'm going to be trying it soon, but I'm still working on some architecture, and now I realize this has to be possible but I don't know how yet.

How to use w/ Java Server

Hi, when I run the command for a standalone server I get a transient failure message. I'm kind of confused how to incorporate my own server, I have the address/port that the server is running on but I don't see how to get the gateway to point to it. Any help would be great thanks!

Incompatibility with the new Go ProtoReflect API

Hi - great project! Unfortunately, I'm hitting a blocker when I try to get your project up and running:

$ go run main.go
go: finding golang.org/x/net v0.0.0-20191002035440-2ec189313ef0
# github.com/johanbrandhorst/grpc-gateway-boilerplate/proto
proto/example.pb.gw.go:45:2: cannot use msg (type *User) as type protoreflect.ProtoMessage in return argument:
	*User does not implement protoreflect.ProtoMessage (missing ProtoReflect method)
proto/example.pb.gw.go:62:2: cannot use msg (type *User) as type protoreflect.ProtoMessage in return argument:
	*User does not implement protoreflect.ProtoMessage (missing ProtoReflect method)
proto/example.pb.gw.go:192:104: cannot use *User value as type protoreflect.ProtoMessage in return argument:
	*User does not implement protoreflect.ProtoMessage (missing ProtoReflect method)

It seems like this may be an issue with the new Go API for Protocol Buffers? I'm not sure if this is a version incompatibility issue with the versions specified in go.mod or if I'm perhaps using the wrong version of protoc?

$ protoc --version
libprotoc 3.11.2

Any thoughts would be very much appreciated!

Cannot use this repo as a GitHub repo template

We cannot use this repo as a template as statik/statik.go has 13.7MB in this repo as of 2020-11-04, which was recently introduced in #17.

File size before #17 (= after #14) after #17
statik/statik.go 6.8MB 13.7MB

Anyone can reproduce this by starting with Use this template button to see the following error:

We're sorry, something went wrong.
We were unable to clone johanbrandhorst/grpc-gateway-boilerplate's contents into {username,orgname}/{reponame}. The template you used includes files that are larger than 10 megabytes. Please ask johanbrandhorst to remove those files from the template and try again.

cf. https://github.com/education/classroom/issues/2521

updating to new buf manifest/config versions

First off I'd like to thank you for your awesome work, this repository has been an incredibly valuable source for learning how to structure a gRPC project in Go that uses gRPC-gateway.

I started writing my own template last year to add a few more things that I wanted to be figured out when I want to write a new service and it can be found in this repository on gitlab (it's also mirrored here on github).

Currently, my buf.yaml and buf.gen.yaml look a lot like yours and there is also wouldbe-buf.work.yaml because it currently doesn't work if you try to upgrade or if you just follow the steps in the official tour.

This led me to spend many evenings trying to troubleshoot it until I read a blog post that provided just enough details to figure out the issue.
In this commit on a feature branch I discovered that there is indeed a way to upgrade to it although it's less than ideal.

In rough order:

  • In each directory holding .proto files, put a simple buf.yaml with the deps key and then run buf mod update to create the lock file
  • If you're using v1, v2 and so on for different versions of the API, you need to do the above step seemingly for all directories until the repo root but I haven't done extensive testing. I had to do it in my protos/ directory that has two subdirectories/packages called v1 and v2
  • At the root of the project, buf.work.yaml can be just
version: v1
directories:
  - proto
  • Again at the root of the project, buf.yaml doesn't need the build key so it can just have the necessary name, version and deps, of course with version going from v1beta1 to v1 (v1 is where build and consequently build.root was removed)
  • Finally, run buf mod update at the root and then buf generate which shouldn't result in any issues

That's what worked for me, I hope this helps somewhat in case you were holding back from upgrading due to the inability for buf to pull in .proto dependencies or generate files properly.
Let me know if there is any additional information I can provide in case you wish to update the way buf stuff is handled, otherwise feel free to close the issue, I just wanted to share my experience dealing with this problem and what I did to fix it.

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.