-
Notifications
You must be signed in to change notification settings - Fork 32
/
app.js
146 lines (127 loc) · 4.73 KB
/
app.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
/**
* Module dependencies.
*/
var express = require('express');
var http = require('http');
var path = require('path');
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
var favicon = require('serve-favicon');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var serveStatic = require('serve-static');
var fs = require('fs-extra');
var spmjsioVersion = require('./package').version;
var gitRev = require('git-rev-sync').short();
// load global config
var yaml = require('node-yaml-config');
var CONFIG = yaml.load('./config/base.yaml');
global.CONFIG = CONFIG;
// start sync
require('./sync');
// mkdir data directory
if (!fs.existsSync(CONFIG.wwwroot)) {
fs.mkdirSync(CONFIG.wwwroot);
}
if (!fs.existsSync(path.join(CONFIG.wwwroot, 'db'))) {
fs.mkdirSync(path.join(CONFIG.wwwroot, 'db'));
}
if (!fs.existsSync(path.join(CONFIG.wwwroot, 'docs'))) {
fs.mkdirSync(path.join(CONFIG.wwwroot, 'docs'));
}
if (!fs.existsSync(path.join(CONFIG.wwwroot, 'repository'))) {
fs.mkdirSync(path.join(CONFIG.wwwroot, 'repository'));
}
var routes = require('./routes');
var account = require('./routes/account');
var repository = require('./routes/repository');
var docs = require('./routes/docs');
var app = express();
// all environments
app.set('port', CONFIG.server.port || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(morgan('combined'));
app.use(require('./middlewares/raw-body')({
contentTypes: ['application/x-tar'],
limit: '50mb'
}));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser('spmjs'));
var sessionStore;
if (CONFIG.sessionStore && CONFIG.sessionStore.type === 'redis') {
var RedisStore = require('connect-redis')(session);
sessionStore = new RedisStore(CONFIG.sessionStore);
} else {
var NedbStore = require('connect-nedb-session-two')(session);
sessionStore = new NedbStore({
filename: path.join(__dirname, './data/db/session.db'),
clearInterval: 24 * 3600 * 1000
});
}
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
store: sessionStore
}));
app.use(serveStatic(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(require('errorhandler')());
}
app.get('/', routes.index);
app.get('/search', routes.search);
app.get('/suggest', routes.suggest);
app.get('/packages', routes.all);
app.get('/package/:name', routes.project);
app.get('/package/:name/:version', routes.package);
app.get('/badge/:name', routes.badge);
app.get('/documentation', routes.documentation);
app.get('/documentation/:title', routes.documentation);
app.get('/login', account.login);
app.get('/callback', account.callback);
app.get('/logout', account.logout);
app.get('/account', account.index);
app.post('/account/login', account.authorize);
app.get('/account/:user', account.user);
app.post('/ownership', account.ownership);
app.delete('/ownership', account.ownership);
app.get('/repository', repository.index);
app.post('/repository/upload', multipartMiddleware, repository.package.checkPermission, repository.upload);
app.get('/repository/search', repository.search);
app.get('/repository/since', repository.since);
app.get('/repository/sync-from-npm/:name', repository.syncFromNpm);
app.get('/repository/sync-from-npm/:name/:version', repository.syncFromNpm);
app.get('/repository/:name', repository.project.get);
app.delete('/repository/:name', repository.package.checkPermission, repository.project.delete);
app.get('/repository/:name/:version', repository.package.get);
app.post('/repository/:name/:version', repository.package.checkPermission, repository.package.post);
app.put('/repository/:name/:version', repository.package.put);
app.delete('/repository/:name/:version', repository.package.checkPermission, repository.package.delete);
app.get('/repository/:name/:version/:filename', repository.filename.get);
app.get('/repositories', repository.data);
app.get('/docs/:name/:version/*', docs);
app.get('/docs/*', express.static(path.join(CONFIG.wwwroot)));
// 404
app.get('*', function(req, res) {
res.status(404).render('404.ejs', {
title: 'No Found - ' + CONFIG.website.title,
spmjsioVersion: spmjsioVersion,
gitRev: gitRev,
anonymous: CONFIG.authorize.type === 'anonymous',
user: req.session.user,
GA: CONFIG.website.GA
});
});
require('./models').sequelize.sync().then(function() {
// start index cache
require('./lib/cacheIndex')();
http.createServer(app).listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
});