egg-grpc-server

1.7.0 • Public • Published

egg-grpc-server

NPM version build status Test coverage David deps Known Vulnerabilities npm download

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.

License

MIT

Dependencies (1)

Dev Dependencies (10)

Package Sidebar

Install

npm i egg-grpc-server

Weekly Downloads

0

Version

1.7.0

License

MIT

Unpacked Size

12.3 kB

Total Files

9

Last publish

Collaborators

  • tristanwong