How to create a new JoeDemoService
backend service in 7 easy steps...
- 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")
- 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
- Scaffold out a new grpc NodeJS app
cd ~/development/uc-node-services
./scripts/initService.sh
- Generate typescript thrift from local thrift directory
cd ~/uc-node-services/services/grpc-joe-demo
pnpm start gen-thrift.local
- 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');
- Start the nodejs service, pointing to local thrift
pnpm start dev
- 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'