]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blame_incremental - server.js
Fixup tests
[perso/Immae/Projets/Nodejs/Surfer.git] / server.js
... / ...
CommitLineData
1#!/usr/bin/env node
2
3'use strict';
4
5
6var express = require('express'),
7 morgan = require('morgan'),
8 passport = require('passport'),
9 path = require('path'),
10 fs = require('fs'),
11 compression = require('compression'),
12 session = require('express-session'),
13 bodyParser = require('body-parser'),
14 cookieParser = require('cookie-parser'),
15 lastMile = require('connect-lastmile'),
16 HttpError = require('connect-lastmile').HttpError,
17 HttpSuccess = require('connect-lastmile').HttpSuccess,
18 multipart = require('./src/multipart'),
19 mkdirp = require('mkdirp'),
20 auth = require('./src/auth.js'),
21 serveIndex = require('serve-index'),
22 files = require('./src/files.js')(path.resolve(__dirname, process.argv[2] || 'files'));
23
24
25const ROOT_FOLDER = path.resolve(__dirname, process.argv[2] || 'files');
26const CONFIG_FILE = path.resolve(__dirname, process.argv[3] || '.config.json');
27
28// Ensure the root folder exists
29mkdirp.sync(ROOT_FOLDER);
30
31var config = {
32 folderListingEnabled: false
33};
34
35function getSettings(req, res, next) {
36 res.send({ folderListingEnabled: !!config.folderListingEnabled });
37}
38
39function setSettings(req, res, next) {
40 if (typeof req.body.folderListingEnabled === 'undefined') return next(new HttpError(400, 'missing folderListingEnabled boolean'));
41
42 config.folderListingEnabled = !!req.body.folderListingEnabled;
43
44 fs.writeFile(CONFIG_FILE, JSON.stringify(config), function (error) {
45 if (error) return next(new HttpError(500, 'unable to save settings'));
46
47 next(new HttpSuccess(201, {}));
48 });
49}
50
51// Load the config file
52try {
53 console.log(`Using config file: ${CONFIG_FILE}`);
54 config = require(CONFIG_FILE);
55} catch (e) {
56 if (e.code === 'MODULE_NOT_FOUND') console.log(`Config file ${CONFIG_FILE} not found`);
57 else console.log(`Cannot load config file ${CONFIG_FILE}`, e);
58}
59
60if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true;
61
62// Setup the express server and routes
63var app = express();
64var router = new express.Router();
65
66var multipart = multipart({ maxFieldsSize: 2 * 1024, limit: '512mb', timeout: 3 * 60 * 1000 });
67
68router.post ('/api/login', auth.login);
69router.post ('/api/logout', auth.verify, auth.logout);
70router.get ('/api/settings', auth.verify, getSettings);
71router.put ('/api/settings', auth.verify, setSettings);
72router.get ('/api/profile', auth.verify, auth.getProfile);
73router.get ('/api/files/*', auth.verify, files.get);
74router.post ('/api/files/*', auth.verify, multipart, files.post);
75router.put ('/api/files/*', auth.verify, files.put);
76router.delete('/api/files/*', auth.verify, files.del);
77router.get ('/api/healthcheck', function (req, res) { res.status(200).send(); });
78
79app.use(morgan('dev'));
80app.use(compression());
81app.use('/api', bodyParser.json());
82app.use('/api', bodyParser.urlencoded({ extended: false, limit: '100mb' }));
83app.use('/api', cookieParser());
84app.use('/api', session({ secret: 'surfin surfin', resave: false, saveUninitialized: false }));
85app.use('/api', passport.initialize());
86app.use('/api', passport.session());
87app.use(router);
88app.use('/_admin', express.static(__dirname + '/frontend'));
89app.use('/', express.static(ROOT_FOLDER));
90app.use('/', function welcomePage(req, res, next) {
91 if (config.folderListingEnabled || req.path !== '/') return next();
92 res.status(200).sendFile(path.join(__dirname, '/frontend/welcome.html'));
93});
94app.use('/', function (req, res, next) {
95 if (config.folderListingEnabled) return next();
96 res.status(404).sendFile(__dirname + '/frontend/404.html');
97});
98app.use('/', serveIndex(ROOT_FOLDER, { icons: true }));
99app.use(lastMile());
100
101var server = app.listen(3000, function () {
102 var host = server.address().address;
103 var port = server.address().port;
104
105 console.log('Surfer listening on http://%s:%s', host, port);
106 console.log('Using base path', ROOT_FOLDER);
107});