A simple file sharing site with an easy to use API and online panel.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.1KB

  1. const express = require('express');
  2. const bodyParser = require('body-parser');
  3. const methodOverride = require('method-override');
  4. const mongoose = require('mongoose');
  5. const morgan = require('morgan');
  6. const passport = require('passport');
  7. const session = require('express-session');
  8. const sanitizer = require('express-sanitizer');
  9. const helmet = require('helmet');
  10. const app = express();
  11. const config = require('config');
  12. // MongoDB
  13. const dbHost = config.get('Database.host');
  14. let db;
  15. mongoose.connect(dbHost, {useNewUrlParser: true})
  16. .then(() => {
  17. console.log('Connected to database ' + dbHost + '\n');
  18. db = mongoose.connection;
  19. });
  20. const MongoStore = require('connect-mongo')(session);
  21. const mongoStore = new MongoStore({url: dbHost});
  22. // HTTP Request Logging
  23. if (config.get('Log.http'))
  24. app.use(morgan(config.get('Log.httpLevel')));
  25. // Session setup
  26. app.use(helmet());
  27. app.set('trust proxy', 1);
  28. app.use(session({
  29. secret: 'secret',
  30. name: 'session.id',
  31. resave: false,
  32. saveUninitialized: false,
  33. store: mongoStore,
  34. cookie: {
  35. //secure: true,
  36. httpOnly: true,
  37. //domain: 'shimapan.rocks',
  38. maxAge: 1000 * 60 * 60
  39. }
  40. }));
  41. // Middleware
  42. app.use(passport.initialize(null));
  43. app.use(passport.session(null));
  44. app.use(bodyParser.json());
  45. app.use(bodyParser.json({ type: 'application/*+json' }));
  46. app.use(bodyParser.urlencoded({ extended: true }));
  47. app.use(bodyParser.text());
  48. app.use(sanitizer());
  49. app.use(methodOverride('X-HTTP-Method-Override'));
  50. // Static directories and favicon
  51. //app.use(favicon(__dirname + '/public/img/favicon.ico'));
  52. app.use(express.static(__dirname + '/public'));
  53. // Install routes and configure authentication strategy
  54. require('./app/routes/routes.js')(app);
  55. require('./config/passport.js');
  56. // Error handler
  57. app.use((err, req, res, next) => {
  58. console.error(err.stack);
  59. res.status(500).json({'message': 'Internal server error.'});
  60. });
  61. // Start app
  62. const port = config.get('Server.port');
  63. const server = app.listen(port, () => {
  64. console.log('Listening on port ' + port + '...\n');
  65. });
  66. // Expose app
  67. module.exports.app = app;
  68. module.exports.server = server;