Sample gRPC / Rest API project in Go
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
3rdparty/swagger-ui
cli
db abab Oct 29, 2017
pb
README.md
data.json
goapi.go

README.md

go-ws-api

Sample gRPC / Rest API project in Go

Uses protoc, gRPC and grpc-gateway for Rest and OpenAPI/Swagger

Context

In this example we have a small "home control" system that simply turn on/off some lights.

As our system grows in popularity, we want to open it to the world, and to achieve this, we will develop an API. For the first mileston, we want to expose the following endpoints:

  • list all registered devices
  • find a device by it's id
  • switch a device on, off or dim
  • create / register a new device

The following is a device's JSON representation we use in this project

{
    "id": 1,
    "hardware": "philips",
    "name": "light",
    "location" : "kitchen",
    "type": "onOff",
    "state": 100
}

How it was built

  • write the protoc spec

  • generate pb services and message types file

    protoc -I. -I%GOPATH%\src -I%GOPATH%\src\github.com\grpc-ecosystem\grpc-gateway\third_party\googleapis --go_out=plugins=grpc:. device.proto
  • implement the code

  • generate the grpc-gateway file

    protoc -I. -I%GOPATH%\src -I%GOPATH%\src\github.com\grpc-ecosystem\grpc-gateway\third_party\googleapis --grpc-gateway_out=logtostderr=true:. device.proto
  • generate the OpenAPI spec/doc

    protoc -I. -I%GOPATH%\src -I%GOPATH%\src\github.com\grpc-ecosystem\grpc-gateway\third_party\googleapis -I%GOPATH%\src\github.com\grpc-ecosystem\grpc-gateway --swagger_out=logtostderr=true:. device.proto