Old ngn4 website
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.

70 lines
2.0 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. var fs = require('fs');
  2. var path = require('path');
  3. var express = require('express');
  4. var morgan = require('morgan')
  5. var ngn4 = require('ngn4');
  6. var config = require('./config.json');
  7. var util = require('./util.js');
  8. var scriptMethods = {};
  9. var app = express();
  10. var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'});
  11. app.use(require('compression')());
  12. app.use(require('cookie-parser')());
  13. app.use(morgan('combined', {stream: accessLogStream}));
  14. // Load data
  15. var set = new ngn4.NameGeneratorSet();
  16. var dir = path.join(__dirname, 'data');
  17. fs.readdirSync(dir).forEach(function(file) {
  18. console.log('Loading ' + file + '...');
  19. set.import(require('./data/' + file), true);
  20. });
  21. // Prepare merged data file for client to use.
  22. var data = set.export();
  23. var indexFile = null;
  24. // Get index.html
  25. fs.readFile(path.join(__dirname + '/www-root/index.html'), 'utf8', function (err, data) {
  26. if (err) {
  27. throw err;
  28. }
  29. indexFile = data;
  30. });
  31. // Load scripts
  32. dir = path.join(__dirname, 'scripts');
  33. fs.readdirSync(dir).forEach(function(file) {
  34. console.log('Running ' + file + '...');
  35. require('./scripts/' + file)(set, app, data, scriptMethods);
  36. });
  37. // Prepare static directories
  38. app.use('/js', express.static('www-root/js'));
  39. app.use('/css', express.static('www-root/css'));
  40. app.use('/img/', express.static('www-root/img'));
  41. // Set up index
  42. function sendIndexHtml(req, res) {
  43. res.header("Content-Type", "text/html; charset=utf-8");
  44. res.end(util.formatTemplate(indexFile, {
  45. generatorStyle: scriptMethods.getBackgroundFromCookie(req.cookies),
  46. inited: true,
  47. initData: scriptMethods.getInitDataFromCookie(req.cookies),
  48. genList: scriptMethods.getGeneratorListJsonData()
  49. }));
  50. }
  51. app.get('/index.html', sendIndexHtml);
  52. app.get('/', sendIndexHtml);
  53. var server = app.listen(config.port, function () {
  54. var host = server.address().address;
  55. var port = server.address().port;
  56. console.log('Server listening at http://%s:%s', host, port);
  57. });