server.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. var fs = require('fs');
  2. var request = require('request');
  3. var connect = require('connect');
  4. var serveStatic = require('serve-static');
  5. var bodyParser = require('body-parser');
  6. var config = require('./config.json');
  7. var apiEndpoint = 'https://www.cloudflare.com/api_json.html';
  8. var isProd = config.isProd === undefined || config.isProd;
  9. var port = config.port || 8000;
  10. var app = connect();
  11. var serve = serveStatic('.', {'index': []});
  12. var serveIndex = function(req, res, next) {
  13. if(isProd) {
  14. req.url = '/index.html';
  15. serve(req, res, next);
  16. }
  17. else {
  18. var html = fs.readFileSync('./index.html', {encoding: 'utf8'});
  19. res.setHeader('Content-Type', 'text/html; charset=utf8');
  20. res.end(html.replace('/assets/bundle.js', 'http://localhost:8001/assets/bundle.js'));
  21. }
  22. };
  23. app.use(bodyParser.urlencoded({extended: false}));
  24. app.use(function(req, res, next) {
  25. if(req.url === '/api') {
  26. req.body.email = config.email;
  27. req.body.tkn = config.token;
  28. // filter out only zones in the whitelist
  29. if(req.body.a === 'zone_load_multi') {
  30. request.post({uri: apiEndpoint, form: req.body, json: true}, function(err, inc, body) {
  31. var filtered = body.response.zones.objs.filter(function(zone) {
  32. return config.whitelist.indexOf(zone.zone_name) >= 0;
  33. });
  34. body.response.zones.objs = filtered;
  35. body.response.zones.count = filtered.length;
  36. res.setHeader('Content-Type', 'application/json');
  37. res.end(JSON.stringify(body));
  38. });
  39. }
  40. // allow any requests for zones in whitelist
  41. else if(config.whitelist.indexOf(req.body.z) >= 0) {
  42. request.post(apiEndpoint).form(req.body).pipe(res);
  43. }
  44. // deny otherwise
  45. else {
  46. next();
  47. }
  48. }
  49. else {
  50. next();
  51. }
  52. });
  53. app.use(serve);
  54. app.use(serveIndex);
  55. if(!isProd) {
  56. var WebpackDevServer = require('webpack-dev-server');
  57. var HotModuleReplacementPlugin = require('webpack/lib/HotModuleReplacementPlugin');
  58. var webpack = require('webpack');
  59. var webpackConfig = require('./webpack.config.js');
  60. webpackConfig.entry = [
  61. "webpack-dev-server/client?http://localhost:8001",
  62. "webpack/hot/dev-server",
  63. webpackConfig.entry
  64. ];
  65. webpackConfig.output.path = '/';
  66. webpackConfig.output.publicPath = 'http://localhost:8001/assets/';
  67. webpackConfig.plugins = webpackConfig.plugins || [];
  68. webpackConfig.plugins.push(new HotModuleReplacementPlugin());
  69. webpackConfig.devtool = 'eval';
  70. var devServer = new WebpackDevServer(webpack(webpackConfig), {
  71. contentBase: 'http://localhost:8000',
  72. publicPath: webpackConfig.output.publicPath,
  73. hot: true
  74. })
  75. devServer.listen(8001);
  76. }
  77. app.listen(port);