-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (48 loc) · 1.73 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
const v1 = require('./v1');
const { db, Package } = require('./db');
const restify = require('restify');
const schedule = require('node-schedule');
const ver = require('./package.json').version;
const Router = require('restify-router').Router;
const corsMiddleware = require('restify-cors-middleware');
const { graphqlRestify, graphiqlRestify } = require('graphql-server-restify');
const schema = require('./v2');
schedule.scheduleJob('0 0 * * 0', async () => {
for (const pkg of await Package.findAll()) {
pkg.update({
weeklyDownloads: 0,
});
}
});
const router = new Router();
const server = restify.createServer({
name: 'diamond-registry',
version: ver,
});
const cdnRouter = new Router();
const cdnServer = restify.createServer({
name: 'diamond-registry',
version: ver,
});
server.use(restify.bodyParser({ mapFiles: true }));
server.use(restify.queryParser());
const cors = corsMiddleware({
origins: ['https://diamond.js.org', 'http://diamondpkg.org', 'https://diamondpkg.org', 'http://localhost:8080'],
credentials: true,
allowHeaders: ['Authorization'],
});
server.pre(cors.preflight);
server.pre(cors.actual);
router.add('/', v1.registry);
router.add('/v1', v1.registry);
router.applyRoutes(server);
cdnRouter.add('/', v1.cdn);
cdnRouter.add('/v1', v1.cdn);
cdnRouter.applyRoutes(cdnServer);
server.post('/graphql', (req, res, next) => graphqlRestify({ schema, context: { req } })(req, res, next));
server.get('/graphql', (req, res, next) => graphqlRestify({ schema, context: { req } })(req, res, next));
server.get('/graphiql', graphiqlRestify({ endpointURL: '/graphql' }));
db.sync().then(() => {
cdnServer.listen(9000, () => console.log('CDN Ready'));
server.listen(8000, () => console.log('Registry Ready'));
});