]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - server.js
Put webdav under /_webdav to avoid name clash
[perso/Immae/Projets/Nodejs/Surfer.git] / server.js
1 #!/usr/bin/env node
2
3 'use strict';
4
5 var 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
25 const ROOT_FOLDER = path.resolve(__dirname, process.argv[2] || 'files');
26 const CONFIG_FILE = path.resolve(__dirname, process.argv[3] || '.config.json');
27
28 // Ensure the root folder exists
29 mkdirp.sync(ROOT_FOLDER);
30
31 var config = {
32 folderListingEnabled: false
33 };
34
35 function getSettings(req, res, next) {
36 res.send({ folderListingEnabled: !!config.folderListingEnabled });
37 }
38
39 function 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
52 try {
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
60 if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true;
61
62 // Setup the express server and routes
63 var app = express();
64 var router = new express.Router();
65
66 var webdavServer = new webdav.WebDAVServer({
67 requireAuthentification: true,
68 httpAuthentication: new webdav.HTTPBasicAuthentication(new auth.WebdavUserManager(), 'Cloudron Surfer')
69 });
70
71 webdavServer.setFileSystem('/', new webdav.PhysicalFileSystem(ROOT_FOLDER), function (success) {
72 console.log(`Mounting ${ROOT_FOLDER} as webdav resource`, success);
73 });
74
75 var multipart = multipart({ maxFieldsSize: 2 * 1024, limit: '512mb', timeout: 3 * 60 * 1000 });
76
77 router.post ('/api/login', auth.login);
78 router.post ('/api/logout', auth.verify, auth.logout);
79 router.get ('/api/settings', auth.verify, getSettings);
80 router.put ('/api/settings', auth.verify, setSettings);
81 router.get ('/api/profile', auth.verify, auth.getProfile);
82 router.get ('/api/files/*', auth.verify, files.get);
83 router.post ('/api/files/*', auth.verify, multipart, files.post);
84 router.put ('/api/files/*', auth.verify, files.put);
85 router.delete('/api/files/*', auth.verify, files.del);
86 router.get ('/api/healthcheck', function (req, res) { res.status(200).send(); });
87
88 app.use(morgan('dev'));
89 app.use(compression());
90 app.use('/api', bodyParser.json());
91 app.use('/api', bodyParser.urlencoded({ extended: false, limit: '100mb' }));
92 app.use('/api', cookieParser());
93 app.use('/api', session({ secret: 'surfin surfin', resave: false, saveUninitialized: false }));
94 app.use('/api', passport.initialize());
95 app.use('/api', passport.session());
96 app.use(router);
97 app.use(webdav.extensions.express('/_webdav', webdavServer));
98 app.use('/_admin', express.static(__dirname + '/frontend'));
99 app.use('/', express.static(ROOT_FOLDER));
100 app.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 });
104 app.use('/', function (req, res, next) {
105 if (config.folderListingEnabled) return next();
106 res.status(404).sendFile(__dirname + '/frontend/404.html');
107 });
108 app.use('/', serveIndex(ROOT_FOLDER, { icons: true }));
109 app.use(lastMile());
110
111 var 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 });