]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - app.js
Provide separate healthcheck route
[perso/Immae/Projets/Nodejs/Surfer.git] / app.js
1 #!/usr/bin/env node
2
3 'use strict';
4
5
6 var express = require('express'),
7 morgan = require('morgan'),
8 passport = require('passport'),
9 path = require('path'),
10 ejs = require('ejs'),
11 fs = require('fs'),
12 compression = require('compression'),
13 session = require('express-session'),
14 bodyParser = require('body-parser'),
15 cookieParser = require('cookie-parser'),
16 lastMile = require('connect-lastmile'),
17 multipart = require('./src/multipart'),
18 mkdirp = require('mkdirp'),
19 auth = require('./src/auth.js'),
20 files = require('./src/files.js')(path.resolve(__dirname, process.argv[2] || 'files'));
21
22 var app = express();
23 var router = new express.Router();
24
25 var multipart = multipart({ maxFieldsSize: 2 * 1024, limit: '512mb', timeout: 3 * 60 * 1000 });
26
27 router.get ('/api/files/*', auth.ldap, files.get);
28 router.put ('/api/files/*', auth.ldap, multipart, files.put);
29 router.delete('/api/files/*', auth.ldap, files.del);
30 router.get ('/api/healthcheck', function (req, res) { res.status(200).send(); });
31
32 // welcome screen in case / does not serve up any file yet
33 var appUrl = process.env.APP_ORIGIN ? process.env.APP_ORIGIN : 'http://localhost:3000';
34 router.get('/', function (req, res) { res.status(200).send(ejs.render(fs.readFileSync(path.join(__dirname, '/app/welcome.html'), 'utf8'), { appUrl: appUrl })); });
35
36 app.use(morgan('dev'));
37 app.use(compression());
38 app.use('/settings', express.static(__dirname + '/app'));
39 app.use(express.static(path.resolve(__dirname, process.argv[2] || 'files')));
40 app.use(bodyParser.json());
41 app.use(bodyParser.urlencoded({ extended: false, limit: '100mb' }));
42 app.use(cookieParser());
43 app.use(session({ secret: 'surfin surfin', resave: false, saveUninitialized: false }));
44 app.use(passport.initialize());
45 app.use(passport.session());
46 app.use(router);
47 app.use(lastMile());
48
49 var server = app.listen(3000, function () {
50 var host = server.address().address;
51 var port = server.address().port;
52
53 var basePath = path.resolve(__dirname, process.argv[2] || 'files');
54 mkdirp.sync(basePath);
55
56 console.log('Surfer listening at http://%s:%s', host, port);
57 console.log('Using base path', basePath);
58 });