server.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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://api.cloudflare.com/client/v4';
  8. var isProd = config.isProd === undefined || config.isProd;
  9. var port = config.port || 8000;
  10. var identifierWhitelist = null;
  11. var app = connect();
  12. var serve = serveStatic('.', {'index': []});
  13. var serveIndex = function(req, res, next) {
  14. if(isProd) {
  15. req.url = '/index.html';
  16. serve(req, res, next);
  17. }
  18. else {
  19. var html = fs.readFileSync('./index.html', {encoding: 'utf8'});
  20. res.setHeader('Content-Type', 'text/html; charset=utf8');
  21. res.end(html.replace('/assets/bundle.js', 'http://localhost:8001/assets/bundle.js'));
  22. }
  23. };
  24. app.use(bodyParser.json());
  25. app.use(function(req, res, next) {
  26. if(req.url.startsWith('/api')) {
  27. var headers = {
  28. 'X-Auth-Email': config.email,
  29. 'X-Auth-Key': config.token
  30. }
  31. var path = req.url.substring(4);
  32. // filter out only zones in the whitelist
  33. if(path === '/zones') {
  34. request.get({uri: apiEndpoint+path, headers: headers, json: true}, function(err, inc, body) {
  35. var filtered = body.result.filter(function(zone) {
  36. return config.whitelist.indexOf(zone.name) >= 0;
  37. });
  38. // TODO prefetch entire zone list
  39. if(identifierWhitelist === null) {
  40. identifierWhitelist = filtered.map(function(zone) {
  41. return zone.id;
  42. });
  43. }
  44. body.result = filtered;
  45. body.result_info.count = filtered.length;
  46. res.setHeader('Content-Type', 'application/json');
  47. res.end(JSON.stringify(body));
  48. });
  49. }
  50. else if(path.startsWith('/zones')) {
  51. // allow any requests for zones in whitelist
  52. var identifier = path.replace(/^\/zones\/([0-9a-f]+).*$/, "$1");
  53. if(identifierWhitelist.indexOf(identifier) >= 0) {
  54. request({
  55. method: req.method,
  56. uri: apiEndpoint+path,
  57. headers: headers,
  58. qs: req.method == 'GET' ? {per_page: 999} : {},
  59. body: req.body,
  60. json: true
  61. }).pipe(res);
  62. }
  63. else {
  64. // deny otherwise
  65. next();
  66. }
  67. }
  68. // deny other request paths for now
  69. else {
  70. next();
  71. }
  72. }
  73. else {
  74. next();
  75. }
  76. });
  77. app.use(serve);
  78. app.use(serveIndex);
  79. if(!isProd) {
  80. var WebpackDevServer = require('webpack-dev-server');
  81. var HotModuleReplacementPlugin = require('webpack/lib/HotModuleReplacementPlugin');
  82. var webpack = require('webpack');
  83. var webpackConfig = require('./webpack.config.js');
  84. webpackConfig.entry = [
  85. "webpack-dev-server/client?http://localhost:8001",
  86. "webpack/hot/dev-server",
  87. webpackConfig.entry
  88. ];
  89. webpackConfig.output.path = '/';
  90. webpackConfig.output.publicPath = 'http://localhost:8001/assets/';
  91. webpackConfig.plugins = webpackConfig.plugins || [];
  92. webpackConfig.plugins.push(new HotModuleReplacementPlugin());
  93. webpackConfig.devtool = 'eval';
  94. var devServer = new WebpackDevServer(webpack(webpackConfig), {
  95. contentBase: 'http://localhost:8000',
  96. publicPath: webpackConfig.output.publicPath,
  97. hot: true
  98. })
  99. devServer.listen(8001);
  100. }
  101. app.listen(port);