-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
202 lines (177 loc) · 4.78 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*jslint node: true, indent: 2 */
'use strict'
const corsMiddleware = require('restify-cors-middleware')
const swaggerJSDoc = require('swagger-jsdoc')
var exports = module.exports = {}
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const url = process.env.MONGO_URL || 'mongodb://localhost:27017';
let db = null;
let cache = null;
const options = {
swaggerDefinition: {
info: {
title: 'Scraper API for ResearchGate', // Title (required)
description: 'Endpoints for extracting JSON data from ResearchGate endpoints',
version: '1.0.0' // Version (required)
},
tags: [
{
"name": "Weather",
"description": "Simple pass-through demo endpoint."
},
{
"name": "ResearchGate",
"description": "Endpoints to extract JSON from ResearchGate pages."
}
],
basePath: '',
schemes: ['http', 'https']
},
apis: [
__dirname + '/routes/weather.js',
__dirname + '/routes/rg.js',
__dirname + '/index.js'
] // Path to the API docs
}
const swaggerSpec = swaggerJSDoc(options)
/**
*
* @swagger
* definitions:
* report:
* type: object
* required:
* - coord
* - weather
* - base
* - main
* - visibility
* - wind
* - clouds
* - dt
* - sys
* - id
* - name
* - cod
* properties:
* coord:
* type: object
* weather:
* type: array
* items:
* type: string
* base:
* type: string
* main:
* type: object
* visibility:
* type: integer
* wind:
* type: object
* clouds:
* type: object
* dt: integer
* sys:
* type: object
* id: integer
* name: string
* cod: integer
*
*/
let restify, bunyan, routes, log, server
restify = require('restify')
bunyan = require('bunyan')
routes = require('./routes/')
log = bunyan.createLogger({
name : 'restify',
level : process.env.LOG_LEVEL || 'info',
stream : process.stdout,
serializers : bunyan.stdSerializers
})
server = restify.createServer({
name : 'restify',
log : log,
})
MongoClient.connect(url, function(err, client) {
// assert.equal(null, err);
if (err) {
console.log('No database available, results will not be cached.')
db = {}
cache = {}
cache.findOne = cache.insertOne = (o, cb) => { return cb() }
} else {
console.log('Connected successfully to server')
db = client.db('rg-cache')
cache = db.collection('documents');
}
server.use(corsMiddleware({
origins: [
'https://kaleguy.github.io',
'http://localhost:8080'
]// defaults to ['*']
// credentials: true, // defaults to false
// headers: ['x-foo'] // sets expose-headers
}).actual)
server.use(restify.plugins.bodyParser({ mapParams: false }))
server.use(restify.plugins.queryParser())
server.use(restify.plugins.gzipResponse())
server.pre(restify.plugins.pre.sanitizePath())
// This version of restify can't handle hyphens in url params... gross hack here to fix
function encodeHyphens(req, res, next) {
req.url = req.url.replace(/-/g, '__');
return next();
}
// server.pre(encodeHyphens)
/*jslint unparam:true*/
// Default error handler. Personalize according to your needs.
/* istanbul ignore next */
server.on('uncaughtException', function (req, res, route, err) {
console.log('******* Begin Error *******')
console.log(route)
console.log('*******')
console.log(err.stack)
console.log('******* End Error *******')
if (!res.headersSent) {
return res.send(500, { ok : false })
}
res.write("\n")
res.end()
})
/*jslint unparam:false*/
// server.on('after', restify.plugins.auditLogger({ log: log }))
routes(server)
// serve swagger docs
server.get('/public/swagger/*', restify.plugins.serveStatic({
directory: __dirname,
default: 'index.html'
}))
// serve swagger spec
server.get('/api-docs.json', function(req, res) {
res.setHeader('Content-Type', 'application/json')
res.send(swaggerSpec)
})
console.log('Server started.')
server.listen(process.env.PORT || 8888, function () {
log.info('%s listening at %s', server.name, server.url)
})
const app = server
function checkCache(req, res, next) {
const cb = (err, result) => {
if (result) {
delete result._id;
console.log(`Retrieving ${result.key} from cache.`)
res.json(result);
} else {
console.log(`Retrieving ${req.url}.`)
next();
}
}
cache.findOne( { key : req.url }, cb );
//next()
}
app.use(checkCache);
module.exports = app
exports.cache = cache
exports.close = server.close
});