Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
147 lines (129 loc) · 4.54 KB
/
Copy pathserver.js
File metadata and controls
147 lines (129 loc) · 4.54 KB
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
147
#!/bin/env node
'use strict';
const express = require('express');
const path = require('path');
const pug = require('pug');
const fs = require('fs');
const shirts = require(path.join(__dirname, 'shirtsofanime'));
const bodyParser = require('body-parser');
const multer = require('multer');
const config = require(path.join(__dirname, 'config.json'));
const basicAuth = require('basic-auth');
const minifier = require('minifier');
const argv = require('minimist')(process.argv.slice(2));
function copyScripts() {
// minify JS
const scripts = fs.readdirSync(path.join(appRoot, 'scripts'));
minifier.on('error', (err) => {
console.log('Error minifying JS: ' + err);
process.exit();
});
scripts.forEach(s => {
let destFile;
const absPath = path.join(appRoot, 'scripts', s);
console.log('Minifying %s...', s);
if (s.indexOf('admin') > -1) {
destFile = path.join(appRoot, 'admin', s);
} else {
destFile = path.join(appRoot, 'public', s);
}
if (argv.dev) {
const content = fs.readFileSync(absPath);
fs.writeFileSync(destFile, content);
} else {
minifier.minify(absPath, {output: destFile});
}
});
}
function compileJade() {
// compile jade(pug) to html
const jadeFiles = fs.readdirSync(path.join(appRoot, 'templates'));
function jadeCompileError(jade) {
return function (err) {
if (err) {
console.log('Error compiling %s: %s', jade, err);
process.exit();
}
};
}
jadeFiles.forEach(jade => {
let htmlFile;
console.log('Compiling %s...', jade);
if (jade.indexOf('admin') > -1) {
htmlFile = path.join(appRoot, 'admin', jade.replace('.jade', '.html'));
} else {
htmlFile = path.join(appRoot, 'public', jade.replace('.jade', '.html'));
}
const html = pug.renderFile(path.join(appRoot, 'templates', jade));
fs.writeFile(htmlFile, html, jadeCompileError(jade));
});
}
function runServer() {
app.listen(app.get('port'), () => {
console.log('shirtsofanime now accepting connections on port %d ...',
app.get('port'));
});
}
function auth(req, res, next) {
// TODO: use stronger auth method and make proper login page
const username = config.server.username;
const password = config.server.password;
const user = basicAuth(req);
if (!user || user.name !== username || user.pass !== password) {
res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
return res.status(401).send('<h1>401 Unauthorized</h1>');
}
next();
}
function sendHTML(file, dir) {
return function(req, res) {
if (typeof dir === 'undefined') {
dir = publicDir;
}
res.sendFile(file + '.html', {root: dir});
};
}
const app = express();
// directory setup
const appRoot = path.join(__dirname, 'app');
const uploadsDir = path.join(__dirname, 'uploads');
const imgDir = path.join(__dirname, 'images');
const publicDir = path.join(appRoot, 'public');
const adminDir = path.join(appRoot, 'admin');
const upload = multer({ dest: uploadsDir });
[uploadsDir, imgDir, adminDir].forEach(d => {
if (!fs.existsSync(d)) {
fs.mkdirSync(d);
}
});
app.set('port', config.server.port);
app.set('trust proxy', 1);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use('/admin', auth);
// -- static content
app.use(express.static(publicDir));
app.use('/images', express.static(path.join(__dirname, 'images')));
app.use('/', express.static(path.join(__dirname, 'bower_components')));
app.use('/admin', express.static(path.join(appRoot, 'admin')));
// -- API routes
app.get('/api/shirts', shirts.getAllShirts);
app.get('/api/shirts/unpublished', auth, shirts.getUnpublished);
app.get('/api/shirts/:id', shirts.getShirtById);
app.post('/api/shirts', upload.single('file'), shirts.createShirt);
app.put('/api/shirts/:id/publish', auth, shirts.publishShirt);
app.delete('/api/shirts/:id/delete', auth, shirts.deleteShirt);
// -- Web UI routes
app.get('/', sendHTML('index'));
app.get('/random', sendHTML('index'));
app.get('/shirt/*', sendHTML('index'));
app.get('/browse', sendHTML('browse'));
app.get('/submit', sendHTML('submit'));
// admin routes... shhh
app.get('/admin', sendHTML('admin', adminDir));
app.get('/admin/*', sendHTML('shirtadmin', adminDir));
// catch-all
app.get('/*', sendHTML('404'));
compileJade();
copyScripts();
shirts.initDb().then(() => runServer());