1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | 'use strict';
|
14 |
|
15 | const path = require('path');
|
16 |
|
17 | const express = require('express');
|
18 | const fse = require('fs-extra');
|
19 | const morgan = require('morgan');
|
20 |
|
21 | const subdomainHandler = require('./subdomainHandler');
|
22 | const rawHandler = require('./rawHandler');
|
23 | const xferHandler = require('./xferHandler');
|
24 | const blobHandler = require('./blobHandler');
|
25 | const commitHandler = require('./commitHandler');
|
26 | const contentHandler = require('./contentHandler');
|
27 | const treeHandler = require('./treeHandler');
|
28 | const htmlHandler = require('./htmlHandler');
|
29 | const archiveLinkHandler = require('./archiveLinkHandler');
|
30 | const archiveHandler = require('./archiveHandler');
|
31 |
|
32 | function getLogDirectory(options) {
|
33 | return path.normalize((options.logs && options.logs.logsDir) || 'logs');
|
34 | }
|
35 |
|
36 | function getLogFile(options) {
|
37 | return path.join(getLogDirectory(options), 'request.log');
|
38 | }
|
39 |
|
40 | function getLogStream(options) {
|
41 | return fse.createWriteStream(getLogFile(options), { flags: 'a' });
|
42 | }
|
43 |
|
44 | function getMorganFormat(options) {
|
45 | return (options.logs && options.logs.reqLogFormat) || 'common';
|
46 | }
|
47 |
|
48 | function getMorganOptions(options) {
|
49 | return { stream: getLogStream(options) };
|
50 | }
|
51 |
|
52 | function createApp(options) {
|
53 | const app = express();
|
54 |
|
55 | app.disable('x-powered-by');
|
56 | app.set('title', options.appTitle || 'Helix Git Server');
|
57 |
|
58 |
|
59 | app.use(morgan(getMorganFormat(options), getMorganOptions(options)));
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | app.use(subdomainHandler(options));
|
65 |
|
66 |
|
67 | app.get('/raw/:owner/:repo/:ref/*', rawHandler(options));
|
68 | app.get('/:owner/:repo/raw/:ref/*', rawHandler(options));
|
69 |
|
70 |
|
71 | app.use('/:owner/:repo.git*', xferHandler(options));
|
72 |
|
73 |
|
74 | app.get('/api/repos/:owner/:repo/git/blobs/:file_sha', blobHandler(options));
|
75 | app.get('/api/repos/:owner/:repo/git/trees/:ref_or_sha(*)', treeHandler(options));
|
76 | app.get('/api/repos/:owner/:repo/contents*', contentHandler(options));
|
77 | app.get('/api/repos/:owner/:repo/commits', commitHandler(options));
|
78 |
|
79 |
|
80 |
|
81 | app.get('/api/repos/:owner/:repo/zipball/:ref(*)?', archiveLinkHandler(options, 'zip'));
|
82 | app.get('/api/repos/:owner/:repo/tarball/:ref(*)?', archiveLinkHandler(options, 'tar.gz'));
|
83 | app.get('/:owner/:repo/archive/:ref(*).zip', archiveLinkHandler(options, 'zip'));
|
84 | app.get('/:owner/:repo/archive/:ref(*).tar.gz', archiveLinkHandler(options, 'tar.gz'));
|
85 |
|
86 | app.get('/codeload/:owner/:repo/legacy.zip/:ref(*)', archiveHandler(options, 'zip'));
|
87 | app.get('/codeload/:owner/:repo/zip/:ref(*)', archiveHandler(options, 'zip'));
|
88 | app.get('/codeload/:owner/:repo/legacy.tar.gz/:ref(*)', archiveHandler(options, 'tar.gz'));
|
89 | app.get('/codeload/:owner/:repo/tar.gz/:ref(*)', archiveHandler(options, 'tar.gz'));
|
90 |
|
91 |
|
92 | app.get('/:owner/:repo/blob/:ref/*', htmlHandler(options, 'blob'));
|
93 | app.get('/:owner/:repo/tree/:ref*', htmlHandler(options, 'tree'));
|
94 | app.get('/:owner/:repo', htmlHandler(options, 'root'));
|
95 |
|
96 | return app;
|
97 | }
|
98 | module.exports = createApp;
|