-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from cofacts/grpc
Using gRPC instead of GraphQL for url resolver
- Loading branch information
Showing
36 changed files
with
2,882 additions
and
2,695 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
!package.json | ||
!package-lock.json | ||
!index.js | ||
!docker-test.js | ||
!schema.js | ||
!src/* | ||
!docker-test.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
vendor | ||
*pb.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
node_modules | ||
.env | ||
coverage | ||
*pb.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ language: node_js | |
node_js: '10' | ||
|
||
script: | ||
- npm run compile | ||
- npm run lint | ||
- npm t -- --coverage | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require('dotenv').config(); | ||
|
||
const grpc = require('grpc'); | ||
const protoLoader = require('@grpc/proto-loader'); | ||
|
||
const PORT = process.env.PORT || 4000; | ||
|
||
const PROTO_PATH = __dirname + '/src/typeDefs/url_resolver.proto'; | ||
const packageDefinition = protoLoader.loadSync(PROTO_PATH, { | ||
keepCase: true, | ||
longs: String, | ||
enums: String, | ||
defaults: true, | ||
oneofs: true, | ||
}); | ||
|
||
const urlResolverProto = grpc.loadPackageDefinition(packageDefinition) | ||
.url_resolver; | ||
|
||
const testResolveUrl = () => { | ||
const client = new urlResolverProto.UrlResolver( | ||
`localhost:${PORT}`, | ||
grpc.credentials.createInsecure() | ||
); | ||
const urls = []; | ||
const call = client.ResolveUrl({ urls }); | ||
const responses = []; | ||
call.on('data', response => { | ||
responses.push(response); | ||
}); | ||
call.on('error', err => { | ||
throw new Error(`Test failed: ${err}`); | ||
}); | ||
// eslint-disable-next-line no-console | ||
call.on('end', () => console.log(JSON.stringify(responses, null, 4))); | ||
}; | ||
|
||
testResolveUrl(); | ||
|
||
/** For browser stats; uncomment to use it **/ | ||
/* | ||
const BROWSER_PROTO_PATH = __dirname + '/src/typeDefs/browser_stats.proto'; | ||
const browserPackageDefinition = protoLoader.loadSync(BROWSER_PROTO_PATH, { | ||
keepCase: true, | ||
longs: String, | ||
enums: String, | ||
defaults: true, | ||
oneofs: true, | ||
}); | ||
const browserProto = grpc.loadPackageDefinition(browserPackageDefinition) | ||
.browser_stats; | ||
const testGetBrowserStats = () => { | ||
const client = new browserProto.BrowserStats( | ||
`localhost:${PORT}`, | ||
grpc.credentials.createInsecure() | ||
); | ||
client.GetStats({}, (err, res) => { | ||
if (err) { | ||
throw new Error(err); | ||
} | ||
// eslint-disable-next-line no-console | ||
console.log(JSON.stringify(res, null, 4)); | ||
}); | ||
}; | ||
testGetBrowserStats(); | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#/bin/bash | ||
|
||
npm start & | ||
wget --post-data='{"query":"{ resolvedUrls(urls: [\"example.com\"]) { url }}"}' --header='Content-Type: application/json' -O - http://localhost:4000 | ||
node index.js & | ||
sleep 10 | ||
node docker-test.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,44 @@ | ||
require('dotenv').config(); | ||
|
||
const schema = require('./src/schema'); | ||
const { ApolloServer } = require('apollo-server'); | ||
const PORT = process.env.PORT || 4000; | ||
const grpc = require('grpc'); | ||
const protoLoader = require('@grpc/proto-loader'); | ||
const { resolveUrls } = require('./src/resolvers/resolveUrls'); | ||
const { getBrowserStats } = require('./src/resolvers/browser'); | ||
|
||
const PROTO_PATHS = { | ||
urlResolver: __dirname + '/src/typeDefs/url_resolver.proto', | ||
browserStats: __dirname + '/src/typeDefs/browser_stats.proto', | ||
}; | ||
const protoLoaderOptions = { | ||
keepCase: true, | ||
longs: String, | ||
enums: String, | ||
defaults: true, | ||
oneofs: true, | ||
}; | ||
|
||
const packageDefinitions = {}; | ||
Object.keys(PROTO_PATHS).map(key => { | ||
packageDefinitions[key] = protoLoader.loadSync( | ||
PROTO_PATHS[key], | ||
protoLoaderOptions | ||
); | ||
}); | ||
|
||
const apolloOption = { schema }; | ||
if (process.env.ENGINE_API_KEY) { | ||
apolloOption.engine = { apiKey: process.env.ENGINE_API_KEY }; | ||
} | ||
const urlResolverProto = grpc.loadPackageDefinition( | ||
packageDefinitions.urlResolver | ||
).url_resolver; | ||
const browserProto = grpc.loadPackageDefinition(packageDefinitions.browserStats) | ||
.browser_stats; | ||
|
||
const PORT = process.env.PORT || 4000; | ||
|
||
const server = new ApolloServer(apolloOption); | ||
server | ||
.listen({ | ||
port: PORT, | ||
}) | ||
.then(({ url }) => { | ||
// eslint-disable-next-line no-console | ||
console.log(`🚀 Server ready at ${url}`); | ||
}); | ||
const server = new grpc.Server(); | ||
server.addService(urlResolverProto.UrlResolver.service, { | ||
ResolveUrl: resolveUrls, | ||
}); | ||
server.addService(browserProto.BrowserStats.service, { | ||
GetStats: getBrowserStats, | ||
}); | ||
server.bind(`0.0.0.0:${PORT}`, grpc.ServerCredentials.createInsecure()); | ||
server.start(); |
Oops, something went wrong.