server.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. var noWhitelist = config.whitelist == null;
  33. // filter out only zones in the whitelist
  34. if(path === '/zones') {
  35. var params = {uri: apiEndpoint+path, headers: headers, json: true};
  36. if(noWhitelist) {
  37. request.get(params).pipe(res);
  38. }
  39. else {
  40. request.get(params, function(err, inc, body) {
  41. var filtered = body.result.filter(function(zone) {
  42. return config.whitelist.indexOf(zone.name) >= 0;
  43. });
  44. // TODO prefetch entire zone list
  45. if(identifierWhitelist === null) {
  46. identifierWhitelist = filtered.map(function(zone) {
  47. return zone.id;
  48. });
  49. }
  50. body.result = filtered;
  51. body.result_info.count = filtered.length;
  52. res.setHeader('Content-Type', 'application/json');
  53. res.end(JSON.stringify(body));
  54. });
  55. }
  56. }
  57. else if(path.startsWith('/zones')) {
  58. // allow any requests for zones in whitelist
  59. var identifier = path.replace(/^\/zones\/([0-9a-f]+).*$/, "$1");
  60. if(noWhitelist || identifierWhitelist.indexOf(identifier) >= 0) {
  61. request({
  62. method: req.method,
  63. uri: apiEndpoint+path,
  64. headers: headers,
  65. qs: req.method == 'GET' ? {per_page: 999} : {},
  66. body: req.body,
  67. json: true
  68. }).pipe(res);
  69. }
  70. else {
  71. // deny otherwise
  72. next();
  73. }
  74. }
  75. // deny other request paths for now
  76. else {
  77. next();
  78. }
  79. }
  80. else {
  81. next();
  82. }
  83. });
  84. app.use(serve);
  85. app.use(serveIndex);
  86. if(!isProd) {
  87. var WebpackDevServer = require('webpack-dev-server');
  88. var HotModuleReplacementPlugin = require('webpack/lib/HotModuleReplacementPlugin');
  89. var webpack = require('webpack');
  90. var webpackConfig = require('./webpack.config.js');
  91. webpackConfig.entry = [
  92. "webpack-dev-server/client?http://localhost:8001",
  93. "webpack/hot/dev-server",
  94. webpackConfig.entry
  95. ];
  96. webpackConfig.output.path = '/';
  97. webpackConfig.output.publicPath = 'http://localhost:8001/assets/';
  98. webpackConfig.plugins = webpackConfig.plugins || [];
  99. webpackConfig.plugins.push(new HotModuleReplacementPlugin());
  100. webpackConfig.devtool = 'eval';
  101. var devServer = new WebpackDevServer(webpack(webpackConfig), {
  102. contentBase: 'http://localhost:8000',
  103. publicPath: webpackConfig.output.publicPath,
  104. hot: true
  105. })
  106. devServer.listen(8001);
  107. }
  108. app.listen(port);