]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blame_incremental - server.js
Add 5.5 changes
[perso/Immae/Projets/Nodejs/Surfer.git] / server.js
... / ...
CommitLineData
1#!/usr/bin/env node
2
3'use strict';
4
5var express = require('express'),
6 morgan = require('morgan'),
7 passport = require('passport'),
8 path = require('path'),
9 fs = require('fs'),
10 compression = require('compression'),
11 session = require('express-session'),
12 bodyParser = require('body-parser'),
13 cookieParser = require('cookie-parser'),
14 lastMile = require('connect-lastmile'),
15 HttpError = require('connect-lastmile').HttpError,
16 HttpSuccess = require('connect-lastmile').HttpSuccess,
17 multipart = require('./src/multipart'),
18 mkdirp = require('mkdirp'),
19 auth = require('./src/auth.js'),
20 serveIndex = require('serve-index'),
21 webdav = require('webdav-server').v2,
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 webdavServer = new webdav.WebDAVServer({
67 requireAuthentification: true,
68 httpAuthentication: new webdav.HTTPBasicAuthentication(new auth.WebdavUserManager(), 'Cloudron Surfer')
69});
70
71webdavServer.setFileSystem('/', new webdav.PhysicalFileSystem(ROOT_FOLDER), function (success) {
72 console.log(`Mounting ${ROOT_FOLDER} as webdav resource`, success);
73});
74
75var multipart = multipart({ maxFieldsSize: 2 * 1024, limit: '512mb', timeout: 3 * 60 * 1000 });
76
77router.post ('/api/login', auth.login);
78router.post ('/api/logout', auth.verify, auth.logout);
79router.get ('/api/settings', auth.verify, getSettings);
80router.put ('/api/settings', auth.verify, setSettings);
81router.get ('/api/profile', auth.verify, auth.getProfile);
82router.get ('/api/files/*', auth.verify, files.get);
83router.post ('/api/files/*', auth.verify, multipart, files.post);
84router.put ('/api/files/*', auth.verify, files.put);
85router.delete('/api/files/*', auth.verify, files.del);
86router.get ('/api/healthcheck', function (req, res) { res.status(200).send(); });
87
88app.use(morgan('dev'));
89app.use(compression());
90app.use(webdav.extensions.express('/webdav', webdavServer));
91app.use('/api', bodyParser.json());
92app.use('/api', bodyParser.urlencoded({ extended: false, limit: '100mb' }));
93app.use('/api', cookieParser());
94app.use('/api', session({ secret: 'surfin surfin', resave: false, saveUninitialized: false }));
95app.use('/api', passport.initialize());
96app.use('/api', passport.session());
97app.use(router);
98app.use('/_admin', express.static(__dirname + '/frontend'));
99app.use('/', express.static(ROOT_FOLDER));
100app.use('/', function welcomePage(req, res, next) {
101 if (config.folderListingEnabled || req.path !== '/') return next();
102 res.status(200).sendFile(path.join(__dirname, '/frontend/welcome.html'));
103});
104app.use('/', function (req, res, next) {
105 if (config.folderListingEnabled) return next();
106 res.status(404).sendFile(__dirname + '/frontend/404.html');
107});
108app.use('/', serveIndex(ROOT_FOLDER, { icons: true }));
109app.use(lastMile());
110
111var server = app.listen(3000, function () {
112 var host = server.address().address;
113 var port = server.address().port;
114
115 console.log('Surfer listening on http://%s:%s', host, port);
116 console.log('Using base path', ROOT_FOLDER);
117});