]>
Commit | Line | Data |
---|---|---|
ca2d3b4d JZ |
1 | #!/usr/bin/env node |
2 | ||
3 | 'use strict'; | |
4 | ||
5 | var express = require('express'), | |
6 | morgan = require('morgan'), | |
8c3ae071 | 7 | path = require('path'), |
552d44bb | 8 | fs = require('fs'), |
ca2d3b4d | 9 | compression = require('compression'), |
591ad40c | 10 | session = require('express-session'), |
ca2d3b4d | 11 | bodyParser = require('body-parser'), |
591ad40c | 12 | cookieParser = require('cookie-parser'), |
ca2d3b4d | 13 | lastMile = require('connect-lastmile'), |
552d44bb JZ |
14 | HttpError = require('connect-lastmile').HttpError, |
15 | HttpSuccess = require('connect-lastmile').HttpSuccess, | |
ca2d3b4d | 16 | multipart = require('./src/multipart'), |
7bb99aff | 17 | mkdirp = require('mkdirp'), |
591ad40c | 18 | auth = require('./src/auth.js'), |
7af3d855 | 19 | webdav = require('webdav-server').v2, |
fb372a32 | 20 | files = require('./src/files.js')(path.resolve(__dirname, process.argv[2] || 'files')); |
ca2d3b4d | 21 | |
552d44bb | 22 | |
b3ff26fb JZ |
23 | const ROOT_FOLDER = path.resolve(__dirname, process.argv[2] || 'files'); |
24 | const CONFIG_FILE = path.resolve(__dirname, process.argv[3] || '.config.json'); | |
552d44bb JZ |
25 | |
26 | // Ensure the root folder exists | |
b3ff26fb | 27 | mkdirp.sync(ROOT_FOLDER); |
552d44bb JZ |
28 | |
29 | var config = { | |
8a5c7d41 | 30 | folderListingEnabled: false |
552d44bb JZ |
31 | }; |
32 | ||
33 | function getSettings(req, res, next) { | |
34 | res.send({ folderListingEnabled: !!config.folderListingEnabled }); | |
35 | } | |
36 | ||
37 | function setSettings(req, res, next) { | |
38 | if (typeof req.body.folderListingEnabled === 'undefined') return next(new HttpError(400, 'missing folderListingEnabled boolean')); | |
39 | ||
40 | config.folderListingEnabled = !!req.body.folderListingEnabled; | |
41 | ||
b3ff26fb | 42 | fs.writeFile(CONFIG_FILE, JSON.stringify(config), function (error) { |
552d44bb JZ |
43 | if (error) return next(new HttpError(500, 'unable to save settings')); |
44 | ||
45 | next(new HttpSuccess(201, {})); | |
46 | }); | |
47 | } | |
48 | ||
49 | // Load the config file | |
50 | try { | |
313dfe99 | 51 | console.log(`Using config file at: ${CONFIG_FILE}`); |
b3ff26fb | 52 | config = require(CONFIG_FILE); |
552d44bb | 53 | } catch (e) { |
b3ff26fb JZ |
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); | |
552d44bb JZ |
56 | } |
57 | ||
58 | if (typeof config.folderListingEnabled === 'undefined') config.folderListingEnabled = true; | |
59 | ||
60 | // Setup the express server and routes | |
ca2d3b4d JZ |
61 | var app = express(); |
62 | var router = new express.Router(); | |
63 | ||
7af3d855 JZ |
64 | var webdavServer = new webdav.WebDAVServer({ |
65 | requireAuthentification: true, | |
66 | httpAuthentication: new webdav.HTTPBasicAuthentication(new auth.WebdavUserManager(), 'Cloudron Surfer') | |
67 | }); | |
68 | ||
69 | webdavServer.setFileSystem('/', new webdav.PhysicalFileSystem(ROOT_FOLDER), function (success) { | |
313dfe99 | 70 | if (success) console.log(`Mounting webdav resource from: ${ROOT_FOLDER}`); |
7af3d855 JZ |
71 | }); |
72 | ||
ca2d3b4d JZ |
73 | var multipart = multipart({ maxFieldsSize: 2 * 1024, limit: '512mb', timeout: 3 * 60 * 1000 }); |
74 | ||
4a27fce7 JZ |
75 | router.post ('/api/login', auth.login); |
76 | router.post ('/api/logout', auth.verify, auth.logout); | |
552d44bb JZ |
77 | router.get ('/api/settings', auth.verify, getSettings); |
78 | router.put ('/api/settings', auth.verify, setSettings); | |
c2c00fca JZ |
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); | |
4a27fce7 | 82 | router.get ('/api/profile', auth.verify, auth.getProfile); |
313dfe99 | 83 | router.get ('/api/files/*', auth.verifyIfNeeded, files.get); |
e628921a JZ |
84 | router.post ('/api/files/*', auth.verify, multipart, files.post); |
85 | router.put ('/api/files/*', auth.verify, files.put); | |
3d716d9e | 86 | router.delete('/api/files/*', auth.verify, files.del); |
ca2d3b4d | 87 | |
870c3b66 | 88 | app.use('/api/healthcheck', function (req, res) { res.status(200).send(); }); |
ca2d3b4d JZ |
89 | app.use(morgan('dev')); |
90 | app.use(compression()); | |
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 })); | |
ca2d3b4d | 95 | app.use(router); |
f5f2a699 | 96 | app.use(webdav.extensions.express('/_webdav', webdavServer)); |
74c0064c | 97 | app.use('/_admin', express.static(__dirname + '/frontend')); |
b3ff26fb | 98 | app.use('/', express.static(ROOT_FOLDER)); |
552d44bb | 99 | app.use('/', function welcomePage(req, res, next) { |
8a5c7d41 | 100 | if (config.folderListingEnabled || req.path !== '/') return next(); |
552d44bb JZ |
101 | res.status(200).sendFile(path.join(__dirname, '/frontend/welcome.html')); |
102 | }); | |
313dfe99 JZ |
103 | app.use('/', function (req, res) { |
104 | if (!config.folderListingEnabled) return res.status(404).sendFile(__dirname + '/frontend/404.html'); | |
105 | ||
106 | if (!fs.existsSync(path.join(ROOT_FOLDER, req.path))) return res.status(404).sendFile(__dirname + '/frontend/404.html'); | |
107 | ||
108 | res.status(200).sendFile(__dirname + '/frontend/public.html'); | |
552d44bb | 109 | }); |
ca2d3b4d JZ |
110 | app.use(lastMile()); |
111 | ||
112 | var server = app.listen(3000, function () { | |
113 | var host = server.address().address; | |
114 | var port = server.address().port; | |
115 | ||
313dfe99 JZ |
116 | console.log(`Base path: ${ROOT_FOLDER}`); |
117 | console.log(); | |
118 | console.log(`Listening on http://${host}:${port}`); | |
119 | ||
120 | auth.init(config); | |
5c6f8c0a | 121 | }); |