Giter Site home page Giter Site logo

grpc-scaffold's Introduction

grpc-scaffold

grpc-scaffold aims to bring a opinionated structure to grpc (+grpc-gateway) repositories.

GoDoc Build Status codecov

Each service protobuf definition gets its own struct which may implement the endpoint.Registerer or even gateway.Controller. By doing so, a common structure is achieved for each service.

Example

Beginning with the protobuf definition, we define a simple service:

syntax = "proto3";

package proto;

import "google/api/annotations.proto";

service HelloService {
    rpc GetHelloMessage(SayHello) return (ReceiveHello) {
        option (google.api.http) = {
            post: "/api/v1/hello"
            body: "*"
        };
    }
}

message SayHello {
    string user_name = 1;
}

message ReceiveHello {
    string message = 1;
}

To implement the service in go, a single struct (one struct per service) is implemented, featuring the functions required for endpoint.Registerer and gateway.Registerer.

package main

import (
    "fmt"
    "database/sql"

	"path/to/protobuf/compile/directory/proto"

	"github.com/grpc-ecosystem/grpc-gateway/runtime"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
)

type HelloService struct {
    Database *sql.DB // db will be initialized later
}

func (h HelloService) GetHelloMessage(c context.Context, request *proto.SayHello) (*proto.ReceiveHello, error) {
    return &proto.ReceiveHello{
        Message: fmt.Sprintf("hello %s", request.UserName),
    }, nil
}

// functions required to implement the gateway.Controller interface:

func (h HelloService) RegisterEndpoint(server *grpc.Server) {
    proto.RegisterHelloServer(server, h)
}

func (h HelloService) RegisterGateway(ctx context.Context, mux *runtime.ServeMux, addr string, opts []grpc.DialOption) error {
    return proto.RegisterHelloHandlerFromEndpoint(ctx, mux, addr, opts)
}

During bootstrapping, the service struct is registered as a controller.

package main

import (
    "flag"

    "github.com/jakoblorz/grpc-scaffold/gateway"
    "github.com/jakoblorz/grpc-scaffold/endpoint"

	"google.golang.org/grpc"
)

var (
    grpcPort = flag.Int("grpcport", "8080", "port to listen on with the grpc server")
    gatePort = flag.Int("gateport", "8081", "port to listen on with the gateway")
    host     = flag.String("host", "127.0.0.1", "host to bind to")
)

func main() {
    flag.Parse()


    // db init code
    db := init()

    endpointLoader := endpoint.NewGRPCLoader(fmt.Sprintf("%s:%d", *host, *grpcPort), []grpc.ServerOption{})
    loader := gateway.NewGRPCLoader(fmt.Sprintf("%s:%d", *host, *gatePort), &endpointLoader, []grpc.DialOption{grpc.WithInsecure()})

    loader.RegisterController(HelloService{
        Database: db,
    })

    go loader.Listen()
}

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.