UNPKG

3.93 kBJavaScriptView Raw
1/*
2 * Copyright 2018 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13'use strict';
14
15const path = require('path');
16
17const express = require('express');
18const fse = require('fs-extra');
19const morgan = require('morgan');
20
21const subdomainHandler = require('./subdomainHandler');
22const rawHandler = require('./rawHandler');
23const xferHandler = require('./xferHandler');
24const blobHandler = require('./blobHandler');
25const commitHandler = require('./commitHandler');
26const contentHandler = require('./contentHandler');
27const treeHandler = require('./treeHandler');
28const htmlHandler = require('./htmlHandler');
29const archiveLinkHandler = require('./archiveLinkHandler');
30const archiveHandler = require('./archiveHandler');
31
32function getLogDirectory(options) {
33 return path.normalize((options.logs && options.logs.logsDir) || 'logs');
34}
35
36function getLogFile(options) {
37 return path.join(getLogDirectory(options), 'request.log');
38}
39
40function getLogStream(options) {
41 return fse.createWriteStream(getLogFile(options), { flags: 'a' });
42}
43
44function getMorganFormat(options) {
45 return (options.logs && options.logs.reqLogFormat) || 'common';
46}
47
48function getMorganOptions(options) {
49 return { stream: getLogStream(options) };
50}
51
52function createApp(options) {
53 const app = express();
54
55 app.disable('x-powered-by');
56 app.set('title', options.appTitle || 'Helix Git Server');
57
58 // request logger
59 app.use(morgan(getMorganFormat(options), getMorganOptions(options)));
60
61 // setup routing
62
63 // subdomain handler (e.g. http://<subdomain>.localtest.me/foo/bar -> /<subdomain>/foo/bar)
64 app.use(subdomainHandler(options));
65
66 // raw content handler
67 app.get('/raw/:owner/:repo/:ref/*', rawHandler(options));
68 app.get('/:owner/:repo/raw/:ref/*', rawHandler(options));
69
70 // git transfer protocol handler (git clone, pull, push)
71 app.use('/:owner/:repo.git*', xferHandler(options));
72
73 // github api handlers
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 // github archive handlers
80 // archive link handlers (redirect to /codeload/...)
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 // archive request handlers
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 // github html handlers (github-like web server)
92 app.get('/:owner/:repo/blob/:ref/*', htmlHandler(options, 'blob')); // single file
93 app.get('/:owner/:repo/tree/:ref*', htmlHandler(options, 'tree')); // directory
94 app.get('/:owner/:repo', htmlHandler(options, 'root')); // home/root directory
95
96 return app;
97}
98module.exports = createApp;