Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rodoufu/rest2grpc

Convert HTTP REST to gRPC
https://github.com/rodoufu/rest2grpc

bridge express-js gateway grpc grpc-gateway rest rest-api restful

Last synced: about 2 months ago
JSON representation

Convert HTTP REST to gRPC

Awesome Lists containing this project

README

        

# rest2grpc



Current TravisCI build status.


Current version.



Current total lines.


License.

Available at https://www.npmjs.com/package/rest2grpc

## Example

Let's say we have a simple gRPC backend service that we want to expose as a REST endpoint,
something like this protos file (`Example.proto`):
```proto
syntax = "proto3";

package example;

service Example {
rpc SayHello (SayHelloRequest) returns (SayHelloResponse) {}
}

message SayHelloRequest {
string name = 1;
}

message SayHelloResponse {
string msg = 1;
}
```

We can use the following configuration (`Example.yaml`) to do so:
```yaml
http:
rules:
- selector: example.Example.SayHello
get: /sayHelloGet
post: /sayHelloPost
```

That tells rest2grpc to create 2 endpoints:
- `/sayHelloGet` answering to GET requests.
- `/sayHelloPost` answering to POST requests.

Both requests are translated into gRPC and sent to the namespace `example`, service `Example`,
and call the method `SayHello`.

Now to call everything you only need to:
```ts
import {Rest2gRPCServer} from 'rest2grpc';
(async () => {
const address = 'localhost:50051';

let configFile = `${__dirname}/Example.yaml`;
const restServer = new Rest2gRPCServer(console);
const protoPath = [`${__dirname}/protos`];
const protoFile = "Example.proto";
await restServer.register(configFile, protoPath, protoFile, address);

restServer.start(3000);
})();
```

## References

- https://github.com/grpc-ecosystem/grpc-gateway