]>
git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - server.js
5 var express
= require('express'),
6 morgan
= require('morgan'),
7 path
= require('path'),
9 compression
= require('compression'),
10 session
= require('express-session'),
11 bodyParser
= require('body-parser'),
12 cookieParser
= require('cookie-parser'),
13 lastMile
= require('connect-lastmile'),
14 HttpError
= require('connect-lastmile').HttpError
,
15 HttpSuccess
= require('connect-lastmile').HttpSuccess
,
16 multipart
= require('./src/multipart'),
17 mkdirp
= require('mkdirp'),
18 auth
= require('./src/auth.js'),
19 webdav
= require('webdav-server').v2
,
20 files
= require('./src/files.js')(path
.resolve(__dirname
, process
.argv
[2] || 'files'));
23 const ROOT_FOLDER
= path
.resolve(__dirname
, process
.argv
[2] || 'files');
24 const CONFIG_FILE
= path
.resolve(__dirname
, process
.argv
[3] || '.config.json');
26 // Ensure the root folder exists
27 mkdirp
.sync(ROOT_FOLDER
);
30 folderListingEnabled: false
33 function getSettings(req
, res
, next
) {
34 res
.send({ folderListingEnabled: !!config
.folderListingEnabled
});
37 function setSettings(req
, res
, next
) {
38 if (typeof req
.body
.folderListingEnabled
=== 'undefined') return next(new HttpError(400, 'missing folderListingEnabled boolean'));
40 config
.folderListingEnabled
= !!req
.body
.folderListingEnabled
;
42 fs
.writeFile(CONFIG_FILE
, JSON
.stringify(config
), function (error
) {
43 if (error
) return next(new HttpError(500, 'unable to save settings'));
45 next(new HttpSuccess(201, {}));
49 // Load the config file
51 console
.log(`Using config file at: ${CONFIG_FILE}`);
52 config
= require(CONFIG_FILE
);
54 if (e
.code
=== 'MODULE_NOT_FOUND') console
.log(`Config file ${CONFIG_FILE} not found`);
55 else console
.log(`Cannot load config file ${CONFIG_FILE}`, e
);
58 if (typeof config
.folderListingEnabled
=== 'undefined') config
.folderListingEnabled
= true;
60 // Setup the express server and routes
62 var router
= new express
.Router();
64 var webdavServer
= new webdav
.WebDAVServer({
65 requireAuthentification: true,
66 httpAuthentication: new webdav
.HTTPBasicAuthentication(new auth
.WebdavUserManager(), 'Cloudron Surfer')
69 webdavServer
.setFileSystem('/', new webdav
.PhysicalFileSystem(ROOT_FOLDER
), function (success
) {
70 if (success
) console
.log(`Mounting webdav resource from: ${ROOT_FOLDER}`);
73 var multipart
= multipart({ maxFieldsSize: 2 * 1024, limit: '512mb', timeout: 3 * 60 * 1000 });
75 router
.post ('/api/login', auth
.login
);
76 router
.post ('/api/logout', auth
.verify
, auth
.logout
);
77 router
.get ('/api/settings', auth
.verify
, getSettings
);
78 router
.put ('/api/settings', auth
.verify
, setSettings
);
79 router
.get ('/api/tokens', auth
.verify
, auth
.getTokens
);
80 router
.post ('/api/tokens', auth
.verify
, auth
.createToken
);
81 router
.delete('/api/tokens/:token', auth
.verify
, auth
.delToken
);
82 router
.get ('/api/profile', auth
.verify
, auth
.getProfile
);
83 router
.get ('/api/files/*', auth
.verifyIfNeeded
, files
.get);
84 router
.post ('/api/files/*', auth
.verify
, multipart
, files
.post
);
85 router
.put ('/api/files/*', auth
.verify
, files
.put
);
86 router
.delete('/api/files/*', auth
.verify
, files
.del
);
88 app
.use('/api/healthcheck', function (req
, res
) { res
.status(200).send(); });
89 app
.use(morgan('dev'));
90 app
.use(compression());
91 app
.use('/api', bodyParser
.json());
92 app
.use('/api', bodyParser
.urlencoded({ extended: false, limit: '100mb' }));
93 app
.use('/api', cookieParser());
94 app
.use('/api', session({ secret: 'surfin surfin', resave: false, saveUninitialized: false }));
96 app
.use(webdav
.extensions
.express('/_webdav', webdavServer
));
97 app
.use('/_admin', express
.static(__dirname
+ '/frontend'));
98 app
.use('/', express
.static(ROOT_FOLDER
));
99 app
.use('/', function welcomePage(req
, res
, next
) {
100 if (config
.folderListingEnabled
|| req
.path
!== '/') return next();
101 res
.status(200).sendFile(path
.join(__dirname
, '/frontend/welcome.html'));
103 app
.use('/', function (req
, res
) {
104 if (!config
.folderListingEnabled
) return res
.status(404).sendFile(__dirname
+ '/frontend/404.html');
106 if (!fs
.existsSync(path
.join(ROOT_FOLDER
, req
.path
))) return res
.status(404).sendFile(__dirname
+ '/frontend/404.html');
108 res
.status(200).sendFile(__dirname
+ '/frontend/public.html');
112 var server
= app
.listen(3000, function () {
113 var host
= server
.address().address
;
114 var port
= server
.address().port
;
116 console
.log(`Base path: ${ROOT_FOLDER}`);
118 console
.log(`Listening on http://${host}:${port}`);