Skip to content

Instantly share code, notes, and snippets.

@notmedia
Created August 29, 2022 15:11
Show Gist options
  • Save notmedia/25bb4d183a5c62084e0b9d795585f75e to your computer and use it in GitHub Desktop.
Save notmedia/25bb4d183a5c62084e0b9d795585f75e to your computer and use it in GitHub Desktop.
import { Server, ServerCredentials } from '@grpc/grpc-js';
import * as fs from 'fs';
import * as path from 'path';
import { TLSServiceServer, TLSServiceService } from './generated/proto/tls_service';
const TLSService: TLSServiceServer = {
unary(call, callback) {
callback(null, call.request);
},
};
function getServerCredentials(): ServerCredentials {
const serverCert = fs.readFileSync(path.resolve(__dirname, '../certs/server-cert.pem'));
const serverKey = fs.readFileSync(path.resolve(__dirname, '../certs/server-key.pem'));
const serverCredentials = ServerCredentials.createSsl(
null,
[
{
cert_chain: serverCert,
private_key: serverKey,
},
],
false
);
return serverCredentials;
}
function main() {
const server = new Server();
const serverCredentials = getServerCredentials();
server.addService(TLSServiceService, TLSService);
server.bindAsync('0.0.0.0:4000', serverCredentials, () => {
server.start();
console.log('gRPC server started on 0.0.0.0:4000');
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment