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.

79 lines
2.2 KiB

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