1 | 'use strict';
|
2 |
|
3 | var koa = require('koa');
|
4 | var app = module.exports = koa();
|
5 | var http = require('http');
|
6 | var middlewares = require('koa-middlewares');
|
7 | var routes = require('../routes/registry');
|
8 | var logger = require('../common/logger');
|
9 | var config = require('../config');
|
10 | var block = require('../middleware/block');
|
11 | var auth = require('../middleware/auth');
|
12 | var staticCache = require('../middleware/static');
|
13 | var notFound = require('../middleware/registry_not_found');
|
14 | var cors = require('kcors');
|
15 | var proxyToNpm = require('../middleware/proxy_to_npm');
|
16 | var maxrequests = require('koa-maxrequests');
|
17 |
|
18 | app.use(maxrequests());
|
19 | app.use(block());
|
20 | middlewares.jsonp(app);
|
21 | app.use(middlewares.rt({ headerName: 'X-ReadTime' }));
|
22 | app.use(middlewares.rewrite('/favicon.ico', '/favicon.png'));
|
23 | app.use(staticCache);
|
24 |
|
25 | app.keys = ['todokey', config.sessionSecret];
|
26 | app.proxy = true;
|
27 | app.use(middlewares.bodyParser({ jsonLimit: config.jsonLimit }));
|
28 | app.use(cors({
|
29 | allowMethods: 'GET,HEAD',
|
30 | }));
|
31 | app.use(auth());
|
32 | app.use(proxyToNpm());
|
33 | app.use(notFound);
|
34 |
|
35 | if (config.enableCompress) {
|
36 | app.use(middlewares.compress({ threshold: 150 }));
|
37 | }
|
38 | app.use(middlewares.conditional());
|
39 | app.use(middlewares.etag());
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | app.use(middlewares.router(app));
|
46 | routes(app);
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | app.on('error', function (err, ctx) {
|
53 | console.log(err);
|
54 | console.log(err.stack);
|
55 | err.url = err.url || ctx.request.url;
|
56 | logger.error(err);
|
57 | });
|
58 |
|
59 | app = http.createServer(app.callback());
|
60 |
|
61 | if (!module.parent) {
|
62 | app.listen(config.registryPort);
|
63 | }
|
64 |
|
65 | module.exports = app;
|