finish up PaymentService
This commit is contained in:
parent
e5ea2154d2
commit
7a6edd751d
5 changed files with 896 additions and 0 deletions
69
src/paymentservice/server.js
Normal file
69
src/paymentservice/server.js
Normal file
|
@ -0,0 +1,69 @@
|
|||
const grpc = require('grpc');
|
||||
const protoLoader = require('@grpc/proto-loader');
|
||||
|
||||
class HipsterShopServer {
|
||||
constructor(protoFile, port = HipsterShopServer.DEFAULT_PORT) {
|
||||
this.port = port;
|
||||
|
||||
this.server = new grpc.Server();
|
||||
this.loadProto(protoFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handler for PaymentService.Charge.
|
||||
* @param {*} call { ChargeRequest }
|
||||
* @param {*} callback fn(err, ChargeResponse)
|
||||
*/
|
||||
static ChargeServiceHandler(call, callback) {
|
||||
try {
|
||||
const response = this.charge(call.request)
|
||||
callback(null, response);
|
||||
} catch (err) {
|
||||
callback(err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Charge function
|
||||
* @param {*} request
|
||||
* @return transaction_id
|
||||
*/
|
||||
static charge(request) {
|
||||
return { transaction_id: -1 }
|
||||
}
|
||||
|
||||
listen() {
|
||||
this.server.bind(`0.0.0.0:${this.port}`, grpc.ServerCredentials.createInsecure());
|
||||
this.server.start();
|
||||
}
|
||||
|
||||
loadProto(path) {
|
||||
const packageDefinition = protoLoader.loadSync(
|
||||
path,
|
||||
{
|
||||
keepCase: true,
|
||||
longs: String,
|
||||
enums: String,
|
||||
defaults: true,
|
||||
oneofs: true,
|
||||
},
|
||||
);
|
||||
const protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
|
||||
const hipsterShopPackage = protoDescriptor.hipstershop;
|
||||
|
||||
this.addProtoService(hipsterShopPackage.PaymentService.service);
|
||||
}
|
||||
|
||||
addProtoService(service) {
|
||||
this.server.addService(
|
||||
service,
|
||||
{
|
||||
charge: HipsterShopServer.ChargeServiceHandler.bind(this),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
HipsterShopServer.DEFAULT_PORT = 50051;
|
||||
|
||||
module.exports = HipsterShopServer;
|
Loading…
Add table
Add a link
Reference in a new issue