]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blobdiff - server.js
Bump version
[perso/Immae/Projets/Nodejs/Surfer.git] / server.js
index d3e4c6c16b4053c3c35efd2a1f85296cdcf0bafe..dd7d7cba8c2c898b8e87dc68259ccb2a2acc820e 100755 (executable)
--- a/server.js
+++ b/server.js
@@ -16,7 +16,6 @@ var express = require('express'),
     multipart = require('./src/multipart'),
     mkdirp = require('mkdirp'),
     auth = require('./src/auth.js'),
-    serveIndex = require('serve-index'),
     webdav = require('webdav-server').v2,
     files = require('./src/files.js')(path.resolve(__dirname, process.argv[2] || 'files'));
 
@@ -49,7 +48,7 @@ function setSettings(req, res, next) {
 
 // Load the config file
 try {
-    console.log(`Using config file: ${CONFIG_FILE}`);
+    console.log(`Using config file at: ${CONFIG_FILE}`);
     config = require(CONFIG_FILE);
 } catch (e) {
     if (e.code === 'MODULE_NOT_FOUND') console.log(`Config file ${CONFIG_FILE} not found`);
@@ -68,7 +67,7 @@ var webdavServer = new webdav.WebDAVServer({
 });
 
 webdavServer.setFileSystem('/', new webdav.PhysicalFileSystem(ROOT_FOLDER), function (success) {
-    console.log(`Mounting ${ROOT_FOLDER} as webdav resource`, success);
+    if (success) console.log(`Mounting webdav resource from: ${ROOT_FOLDER}`);
 });
 
 var multipart = multipart({ maxFieldsSize: 2 * 1024, limit: '512mb', timeout: 3 * 60 * 1000 });
@@ -77,13 +76,16 @@ router.post  ('/api/login', auth.login);
 router.post  ('/api/logout', auth.verify, auth.logout);
 router.get   ('/api/settings', auth.verify, getSettings);
 router.put   ('/api/settings', auth.verify, setSettings);
+router.get   ('/api/tokens', auth.verify, auth.getTokens);
+router.post  ('/api/tokens', auth.verify, auth.createToken);
+router.delete('/api/tokens/:token', auth.verify, auth.delToken);
 router.get   ('/api/profile', auth.verify, auth.getProfile);
-router.get   ('/api/files/*', auth.verify, files.get);
+router.get   ('/api/files/*', auth.verifyIfNeeded, files.get);
 router.post  ('/api/files/*', auth.verify, multipart, files.post);
 router.put   ('/api/files/*', auth.verify, files.put);
 router.delete('/api/files/*', auth.verify, files.del);
-router.get   ('/api/healthcheck', function (req, res) { res.status(200).send(); });
 
+app.use('/api/healthcheck', function (req, res) { res.status(200).send(); });
 app.use(morgan('dev'));
 app.use(compression());
 app.use('/api', bodyParser.json());
@@ -98,17 +100,22 @@ app.use('/', function welcomePage(req, res, next) {
     if (config.folderListingEnabled || req.path !== '/') return next();
     res.status(200).sendFile(path.join(__dirname, '/frontend/welcome.html'));
 });
-app.use('/', function (req, res, next) {
-    if (config.folderListingEnabled) return next();
-    res.status(404).sendFile(__dirname + '/frontend/404.html');
+app.use('/', function (req, res) {
+    if (!config.folderListingEnabled) return res.status(404).sendFile(__dirname + '/frontend/404.html');
+
+    if (!fs.existsSync(path.join(ROOT_FOLDER, req.path))) return res.status(404).sendFile(__dirname + '/frontend/404.html');
+
+    res.status(200).sendFile(__dirname + '/frontend/public.html');
 });
-app.use('/', serveIndex(ROOT_FOLDER, { icons: true }));
 app.use(lastMile());
 
 var server = app.listen(3000, function () {
     var host = server.address().address;
     var port = server.address().port;
 
-    console.log('Surfer listening on http://%s:%s', host, port);
-    console.log('Using base path', ROOT_FOLDER);
+    console.log(`Base path: ${ROOT_FOLDER}`);
+    console.log();
+    console.log(`Listening on http://${host}:${port}`);
+
+    auth.init(config);
 });