egg-grpc-server
v1.7.0
Published
[![NPM version][npm-image]][npm-url] [![build status][travis-image]][travis-url] [![Test coverage][codecov-image]][codecov-url] [![David deps][david-image]][david-url] [![Known Vulnerabilities][snyk-image]][snyk-url] [![npm download][download-image]]
Downloads
36
Readme
egg-grpc-server
Install
$ npm i grpc -g
$ npm i egg-grpc-server --save
Usage
// {app_root}/config/plugin.js
exports.grpcServer = {
enable: true,
package: 'egg-grpc-server',
};
Configuration
// {app_root}/config/config.default.js
exports.grpcServer = {
protoPath: 'app/grpc', //*.proto path
extendPath: 'app/grpc', //service path
host: '0.0.0.0',
port: '50051',
loaderOption: {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
}
};
see config/config.default.js for more detail.
Example
// {app_root}/app/grpc/ProfileService.proto
syntax = "proto3";
package passport.auth;
service AuthService {
rpc roles (UserReq) returns (UserRes) {
}
}
message UserReq {
string userId = 1;
string clientId = 2;
}
message UserRes{
string userId = 1;
string clientId = 2;
}
// {app_root}/app/grpc/passport/profile/ProfileService.js
const BaseGrpc = require('egg-grpc-server').BaseGrpc;
class ProfileService extends BaseGrpc {
async getUserInfo() {
this.app.coreLogger.info("echo");
const params = this.call.request;
const user = await this.app.model.User.findOne({where: {userId: params.userId}});
if (!user) throw new Error('user_none');
return {
userId: user.userId,
username: user.username,
nickname: user.nickname,
avatar: user.avatar,
gender: user.gender
}
}
}
module.exports = ProfileService;
// {app_root}/app/grpc/[passport/profile/]ProfileService.js == `package passport.profile` in *.proto ;
see demo for more detail.
client
Please open an issue egg-grpc-client.
Questions & Suggestions
Please open an issue here.