]>
Commit | Line | Data |
---|---|---|
ca2d3b4d JZ |
1 | #!/usr/bin/env node |
2 | ||
3 | 'use strict'; | |
4 | ||
5 | var express = require('express'), | |
6 | morgan = require('morgan'), | |
591ad40c | 7 | passport = require('passport'), |
8c3ae071 | 8 | path = require('path'), |
552d44bb | 9 | fs = require('fs'), |
ca2d3b4d | 10 | compression = require('compression'), |
591ad40c | 11 | session = require('express-session'), |
ca2d3b4d | 12 | bodyParser = require('body-parser'), |
591ad40c | 13 | cookieParser = require('cookie-parser'), |
ca2d3b4d | 14 | lastMile = require('connect-lastmile'), |
552d44bb JZ |
15 | HttpError = require('connect-lastmile').HttpError, |
16 | HttpSuccess = require('connect-lastmile').HttpSuccess, | |
ca2d3b4d | 17 | multipart = require('./src/multipart'), |
7bb99aff | 18 | mkdirp = require('mkdirp'), |
591ad40c | 19 | auth = require('./src/auth.js'), |
490f71d7 | 20 | serveIndex = require('serve-index'), |
7af3d855 | 21 | webdav = require('webdav-server').v2, |
fb372a32 | 22 | files = require('./src/files.js')(path.resolve(__dirname, process.argv[2] || 'files')); |
ca2d3b4d | 23 | |
552d44bb | 24 | |
b3ff26fb JZ |
25 | const ROOT_FOLDER = path.resolve(__dirname, process.argv[2] || 'files'); |
26 | const CONFIG_FILE = path.resolve(__dirname, process.argv[3] || '.config.json'); | |
552d44bb JZ |
27 | |
28 | // Ensure the root folder exists | |
b3ff26fb | 29 | mkdirp.sync(ROOT_FOLDER); |
552d44bb JZ |
30 | |
31 | var config = { | |
8a5c7d41 | 32 | folderListingEnabled: false |
552d44bb JZ |
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 | ||
b3ff26fb | 44 | fs.writeFile(CONFIG_FILE, JSON.stringify(config), function (error) { |
552d44bb JZ |
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 { | |
b3ff26fb JZ |
53 | console.log(`Using config file: ${CONFIG_FILE}`); |
54 | config = require(CONFIG_FILE); | |
552d44bb | 55 | } catch (e) { |
b3ff26fb JZ |
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); | |
552d44bb JZ |
58 | } |
59 | ||
60 | if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true; | |
61 | ||
62 | // Setup the express server and routes | |
ca2d3b4d JZ |
63 | var app = express(); |
64 | var router = new express.Router(); | |
65 | ||
7af3d855 JZ |
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 | ||
ca2d3b4d JZ |
75 | var multipart = multipart({ maxFieldsSize: 2 * 1024, limit: '512mb', timeout: 3 * 60 * 1000 }); |
76 | ||
4a27fce7 JZ |
77 | router.post ('/api/login', auth.login); |
78 | router.post ('/api/logout', auth.verify, auth.logout); | |
552d44bb JZ |
79 | router.get ('/api/settings', auth.verify, getSettings); |
80 | router.put ('/api/settings', auth.verify, setSettings); | |
4a27fce7 | 81 | router.get ('/api/profile', auth.verify, auth.getProfile); |
3d716d9e | 82 | router.get ('/api/files/*', auth.verify, files.get); |
e628921a JZ |
83 | router.post ('/api/files/*', auth.verify, multipart, files.post); |
84 | router.put ('/api/files/*', auth.verify, files.put); | |
3d716d9e | 85 | router.delete('/api/files/*', auth.verify, files.del); |
1a8d99e4 | 86 | router.get ('/api/healthcheck', function (req, res) { res.status(200).send(); }); |
ca2d3b4d JZ |
87 | |
88 | app.use(morgan('dev')); | |
89 | app.use(compression()); | |
7af3d855 | 90 | app.use(webdav.extensions.express('/webdav', webdavServer)); |
74c0064c JZ |
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 })); | |
95 | app.use('/api', passport.initialize()); | |
96 | app.use('/api', passport.session()); | |
ca2d3b4d | 97 | app.use(router); |
74c0064c | 98 | app.use('/_admin', express.static(__dirname + '/frontend')); |
b3ff26fb | 99 | app.use('/', express.static(ROOT_FOLDER)); |
552d44bb | 100 | app.use('/', function welcomePage(req, res, next) { |
8a5c7d41 | 101 | if (config.folderListingEnabled || req.path !== '/') return next(); |
552d44bb JZ |
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(); | |
4d8f08f9 | 106 | res.status(404).sendFile(__dirname + '/frontend/404.html'); |
552d44bb | 107 | }); |
b3ff26fb | 108 | app.use('/', serveIndex(ROOT_FOLDER, { icons: true })); |
ca2d3b4d JZ |
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 | ||
552d44bb | 115 | console.log('Surfer listening on http://%s:%s', host, port); |
b3ff26fb | 116 | console.log('Using base path', ROOT_FOLDER); |
5c6f8c0a | 117 | }); |