This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (56 loc) · 1.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict';
const http = require( 'http' );
const https = require( 'https' );
const process = require( 'process' );
const defaultQueryHandlerChain = require( './lib/defaultQueryHandlerChain' );
const stats = require( './lib/stats' );
const port = process.argv[ 2 ] || 8080;
function isJsonType( mimeType ) {
switch ( mimeType ) {
case 'application/sparql-results+json': return true;
case 'application/json': return true;
default: return false;
}
}
const server = http.createServer( function ( clientRequest, clientResponse ) {
if ( clientRequest.method === 'GET' && isJsonType( clientRequest.headers.accept ) ) {
const url = new URL( clientRequest.url, 'http://localhost' );
if ( url.pathname === '/sparql' && url.searchParams.has( 'query' ) ) {
const result = defaultQueryHandlerChain.getResult( url.searchParams.get( 'query' ) );
stats.incrementCounter( `handler.result.${result.constructor.name}` );
result.respond( clientRequest, clientResponse );
return;
}
}
if ( clientRequest.url === '/queripulator-stats' ) {
clientResponse.writeHead( 200, { 'content-type': 'application/json' } );
stats.dump( clientResponse );
clientResponse.end();
return;
}
const wdqsRequest = https.request(
'https://query.wikidata.org',
{
path: clientRequest.url,
method: clientRequest.method,
headers: {
...clientRequest.headers,
host: 'query.wikidata.org',
},
},
function ( wdqsResponse ) {
clientResponse.writeHead(
wdqsResponse.statusCode,
wdqsResponse.headers,
);
wdqsResponse.pipe( clientResponse );
},
);
clientRequest.pipe( wdqsRequest );
} );
process.on( 'SIGTERM', () => {
server.close( () => {
stats.dump( process.stdout );
} );
} );
server.listen( port );