Giter Club home page Giter Club logo

grpc-gateway-generator's Introduction

grpc-gateway-generator

A script to generate a ready to use grpc-gateway, with swagger, by just providing the protos and a simple configuration file.

Status: Alpha, it's not production ready yet. See TODO list below.

Installation

Prerequisites

  • If you don't have go installed, install go
  • Install grpc-gateway dependencies. (ProtocolBuffers 3.0.0-beta-3 or later), and the following packages:
go get -u google.golang.org/grpc
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
go get -u github.com/golang/protobuf/protoc-gen-go
  • Install the script dependencies:

Clone

git clone https://github.com/devsu/grpc-gateway-generator

Usage

./generate.sh protos-path config-file project-path  

Arguments

  • protos-path: The path to the protos folder.

    The package names of the protos files must correspond to the folder structure. For example:

    • /protos/myapp/my-file.proto must have the package myapp, and
    • /protos/myapp/mymicroservice/another-file.proto must have the package myapp.mymicroservice
  • config-file: Path to a config.json file. The config file has two sections: gateway and backends.

    In the gateway section, you define the address that the gateway will listen into.

    The backends is an array of objects with 3 properties:

    • package: name of the package.
    • backend: what addres the GRPC server is running.
    • services: An object with the services names as keys, and the base paths of the services as values.

    See config.json for an example of how it should look:

    {
     "gateway": {
       "listen": ":9090"
     },
     "backends": [
       {
         "package": "myapp",
         "backend": "localhost:3000",
         "services": {
           "GreeterService": "/sample/greeter/"
         }
       },
       {
         "package": "myapp.mysubapp",
         "backend": "localhost:3000",
         "services": {
           "AnotherGreeterService": "/sample/another-greeter/"
         }
       }
     ]
    }
  • project-path: The path of the go project to be created. The gateway will be created in ${GOPATH}/src/${projectPath}. Read How to write Go Code to learn how to choose a proper project path.

Step by step guide

  • First, create your protos and add the corresponding annotations. You can see the grpc-gateway documentation or the sample file in examples/protos/myapp for an example.

  • Then you need to create a config file. (See instructions above)

  • Then you just need to run the command with the right arguments.

    For example, to create a grpc-gateway using the information in the example:

    ./generate.sh example/protos example/config.json github.com/my-user/my-gateway

TODO

  • Support TLS in both, the gateway, and the backends
  • Make swagger creation optional
  • Test on more OSs, currently only tested on Mac an Alpine
  • Clean up the script
  • Allow to configure authorization (currently it activates bearer authorization by default)
  • Upgrade swagger-ui?

License and Credits

Devsu LLC. MIT License. Copyright 2017.

Built by the GRPC experts at Devsu.

grpc-gateway-generator's People

Contributors

c3s4r avatar

Stargazers

 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

grpc-gateway-generator's Issues

Help wanted. connect to backends fails.

I have a nodejs backend grpc-server (condor) that is up and running, but it seems the grpc-gateway is unable to establish connection to the backend(s). There are no information whatsoever about what goes wrong.

You can look at the configurations etc, but it would be sufficient if I can get some help making the gateway server more verbose for debugging as I have very limited go-experience.

PS
I've forked docker-grpc-gateway to make it useable in our workflow. There may be some obvious mistakes made by the config-generator script, but as far as I can see the config file generated is ok. Also manually creating the config files makes no difference.

config generated / used

{
  "gateway": {
    "listen": ":8081"
  },
  "backends": [{
    "package": "info",
    "backend": "localhost:5100",
    "services": {
      "InfoService": "/info/"
    }
  }]
}

protofile: protos/info/informer.proto

syntax = "proto3";

package info;

import "google/api/annotations.proto";
////
//// Service definition
////
service InfoService {

    // setinfo adds a Info item to the database.
    rpc SetInfo (AddInfoRequest) returns (Confirmation){
        option (google.api.http) = {
            put: "/info/setinfo/{info.uuid}"
            body: "auth"
        };
    }

    // getInfo retrieves a Info item from database based on uuid.
    rpc GetInfo (GetInfoRequest) returns (InfoResponse){
        option (google.api.http) = {
            post: "/info/getinfo/{uuid}"
            body: ""
        };
    }

    // listCategories retrieves list of all categories available in database
    rpc ListCategories (CategoryListRequest) returns (CategoryList){
        option (google.api.http) = {
            post: "/info/listcategories"
            body: ""
        };
    }

    // listHeader retrieves list of all info headers in database, with uuid.
    rpc ListHeaders (HeaderListRequest) returns (HeaderList){
        option (google.api.http) = {
            post: "/info/listheaders"
            body: ""
        };
    }

}

////
//// Message definitions
////

// SetInfo messages

message AddInfoRequest{
    Info info = 1;
    Auth auth = 2;
//    Timestamp timestamp = 3;
}

message Confirmation {
    string type = 1;
    string comment = 2;
    string error = 3;
//    Timestamp timestamp = 5;
}

// GetInfo messages

message GetInfoRequest{
    string uuid = 1;
    Auth auth = 2;
}

message InfoResponse{
    Info info = 1;
    string error = 2;
}

// ListCategories messages

message CategoryListRequest{
    Auth auth = 2;
}

message CategoryList{
    repeated string categories = 1;
    string error = 2 ;
}

// listHeaders messages

message HeaderListRequest{
    repeated string categories = 1;
    Auth auth = 2;
}

message HeaderList{
    repeated UuidAndHeader headers = 1;
    string error = 2;
}

message UuidAndHeader{
    string uuid = 1;
    string header = 2;
}

// global messages

message Info {
    string uuid = 1;
    string category = 2;
    repeated string subcategories = 3;
    string header = 4;
    string summary = 5;
    string body = 6;
    string author = 7;
}

message Auth{
    string user = 1;
    string password = 2;
    string token = 3;
}

Generate.sh should generate go file together

I have some proto files , and A import B in same package.
It will break in go import
I change the generate.sh

find ${PROTOS_PATH} -type f -name "*.proto" -exec protoc -I${PROTOS_PATH} \

to

find ${PROTOS_PATH} -type f -name "*.proto"|xargs protoc -I${PROTOS_PATH} \

golang/protobuf#10

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.