Skip to content

Instantly share code, notes, and snippets.

@josephschmitt
Last active April 14, 2020 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephschmitt/415746579a66a38a534ab746a88fb06d to your computer and use it in GitHub Desktop.
Save josephschmitt/415746579a66a38a534ab746a88fb06d to your computer and use it in GitHub Desktop.
How to create a new `JoeDemoService` backend service in 7 easy steps...

Node GRPC Demo Script

How to create a new JoeDemoService backend service in 7 easy steps...

  1. Create new thrift file in ~/development/urbancompass/src/thrift/urbancompass/joe_demo:
namespace go joe_demo.joe_demo_service
namespace java com.urbancompass.joe_demo
namespace js joe_demo.joe_demo_service
namespace py gen.urbancompass.joe_demo.joe_demo_service

include "common/base.thrift"

struct JoeDemoRequest {
  1: optional string msg;
}

struct JoeDemoResponse {
  1: optional string msg;
  2: optional base.ResponseStatus status;
}

service JoeDemoService extends base.BaseService {
  JoeDemoResponse foo(1: JoeDemoRequest request)
    (api.url = "/joe_demo", api.method = "GET");
} (api.url = "/joe_demo")
  1. Re-build local apiv3 with new thrift and start it pointing to new service at 127.0.0.1:1337:
./scripts/go install compass.com/api/cmd/apid && \
  ./build-support/go/bin/apid --joe-demo-service=grpc://127.0.0.1:1337
  1. Scaffold out a new grpc NodeJS app
cd ~/development/uc-node-services
./scripts/initService.sh
  1. Generate typescript thrift from local thrift directory
cd ~/uc-node-services/services/grpc-joe-demo
pnpm start gen-thrift.local
  1. Update service Node service implementation to implement JoeDemoService
import * as grpc from 'grpc';

import {ServiceHealth, ServiceStatus, SystemHealth, EchoRequest, EchoResponse} from '../thrift-gen/ts/common/base';

import {JoeDemoRequest, JoeDemoResponse} from '../thrift-gen/ts/joe_demo/joe_demo_service';
import {createJoeDemoServiceImplementation, JoeDemoServiceDefinition} from '../thrift-gen/ts/joe_demo/joe_demo_service/JoeDemoService__grpc';

const implementation = createJoeDemoServiceImplementation({
  foo(request: JoeDemoRequest) {
    console.log('JoeDemoRequest', request);

    return new JoeDemoResponse({
      msg: request.msg === 'foo' ? 'bar' : 'Hello World!',
    });
  },

  getServiceHealth(): ServiceHealth | Promise<ServiceHealth> {
    return new ServiceHealth({
      name: 'GrpcJoeDemoService',
      status: ServiceStatus.OK,
      statusDetails: 'Service is okay',
    });
  },

  getSystemHealth(): SystemHealth | Promise<SystemHealth> {
    return new SystemHealth({
      overallStatus: ServiceStatus.OK,
      serviceName: 'GrpcJoeDemoService',
      subsystemsStatus: [],
    });
  },

  echo(request: EchoRequest): EchoResponse | Promise<EchoResponse> {
    return new EchoResponse({
      request,
    });
  },
});

const server = new grpc.Server();
server.addService(JoeDemoServiceDefinition, implementation);
server.bind('127.0.0.1:1337', grpc.ServerCredentials.createInsecure());
server.start();

console.log('Server started at 127.0.0.1:1337');
  1. Start the nodejs service, pointing to local thrift
pnpm start dev
  1. Send the service traffic via apiv3
curl 'http://127.0.0.1:4000/joe_demo'
curl 'http://127.0.0.1:4000/joe_demo?msg=foo'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment