-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor middlewares in their own folder, move buildFiles to libs/bui…
…ldFiles
- Loading branch information
Showing
16 changed files
with
275 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var nconf = require('nconf'); | ||
var _ = require('lodash'); | ||
var manifestFiles = require("../../public/manifest.json"); | ||
|
||
var IS_PROD = nconf.get('NODE_ENV') === 'production'; | ||
var buildFiles = []; | ||
|
||
var walk = function(folder){ | ||
var res = fs.readdirSync(folder); | ||
var files = []; | ||
|
||
res.forEach(function(fileName){ | ||
file = folder + '/' + fileName; | ||
if(fs.statSync(file).isDirectory()){ | ||
walk(file); | ||
}else{ | ||
var relFolder = path.relative(path.join(__dirname, "/../../build"), folder); | ||
var old = fileName.replace(/-.{8}(\.[\d\w]+)$/, '$1'); | ||
|
||
if(relFolder){ | ||
old = relFolder + '/' + old; | ||
fileName = relFolder + '/' + fileName; | ||
} | ||
|
||
buildFiles[old] = fileName | ||
} | ||
}); | ||
|
||
return files; | ||
} | ||
|
||
walk(path.join(__dirname, "/../../build")); | ||
|
||
var getBuildUrl = module.exports.getBuildUrl = function(url){ | ||
if(buildFiles[url]) return '/' + buildFiles[url]; | ||
|
||
return '/' + url; | ||
} | ||
|
||
module.exports.getManifestFiles = function(page){ | ||
var files = manifestFiles[page]; | ||
|
||
if(!files) throw new Error("Page not found!"); | ||
|
||
var code = ''; | ||
|
||
if(IS_PROD){ | ||
code += '<link rel="stylesheet" type="text/css" href="' + getBuildUrl(page + '.css') + '">'; | ||
code += '<script type="text/javascript" src="' + getBuildUrl(page + '.js') + '"></script>'; | ||
}else{ | ||
_.each(files.css, function(file){ | ||
code += '<link rel="stylesheet" type="text/css" href="' + getBuildUrl(file) + '">'; | ||
}); | ||
_.each(files.js, function(file){ | ||
code += '<script type="text/javascript" src="' + getBuildUrl(file) + '"></script>'; | ||
}); | ||
} | ||
|
||
return code; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var nconf = require('nconf'); | ||
|
||
var IS_PROD = nconf.get('NODE_ENV') === 'production'; | ||
|
||
module.exports = function(app) { | ||
if (!IS_PROD) return; | ||
app.use(limiter({ | ||
end:false, | ||
catagories:{ | ||
normal: { | ||
// 2 req/s, but split as minutes | ||
totalRequests: 80, | ||
every: 60000 | ||
} | ||
} | ||
})).use(function(req,res,next){ | ||
//logging.info(res.ratelimit); | ||
if (res.ratelimit.exceeded) return res.json(429,{err:'Rate limit exceeded'}); | ||
next(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = function(req, res, next) { | ||
res.header("Access-Control-Allow-Origin", req.headers.origin || "*"); | ||
res.header("Access-Control-Allow-Methods", "OPTIONS,GET,POST,PUT,HEAD,DELETE"); | ||
res.header("Access-Control-Allow-Headers", "Content-Type,Accept,Content-Encoding,X-Requested-With,x-api-user,x-api-key"); | ||
if (req.method === 'OPTIONS') return res.send(200); | ||
return next(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
var nconf = require('nconf'); | ||
var moment = require('moment'); | ||
var domainMiddleware = require('domain-middleware'); | ||
var os = require('os'); | ||
var request = require('request'); | ||
|
||
var IS_PROD = nconf.get('NODE_ENV') === 'production'; | ||
|
||
module.exports = function(server,mongoose) { | ||
if (IS_PROD) { | ||
var mins = 3, // how often to run this check | ||
useAvg = false, // use average over 3 minutes, or simply the last minute's report | ||
url = 'https://api.newrelic.com/v2/applications/'+nconf.get('NEW_RELIC_APPLICATION_ID')+'/metrics/data.json?names[]=Apdex&values[]=score'; | ||
setInterval(function(){ | ||
// see https://docs.newrelic.com/docs/apm/apis/api-v2-examples/average-response-time-examples-api-v2, https://rpm.newrelic.com/api/explore/applications/data | ||
request({ | ||
url: useAvg ? url+'&from='+moment().subtract({minutes:mins}).utc().format()+'&to='+moment().utc().format()+'&summarize=true' : url, | ||
headers: {'X-Api-Key': nconf.get('NEW_RELIC_API_KEY')} | ||
}, function(err, response, body){ | ||
var ts = JSON.parse(body).metric_data.metrics[0].timeslices, | ||
score = ts[ts.length-1].values.score, | ||
apdexBad = score < .75 || score == 1, | ||
memory = os.freemem() / os.totalmem(), | ||
memoryHigh = memory < 0.1; | ||
if (/*apdexBad || */memoryHigh) throw "[Memory Leak] Apdex="+score+" Memory="+parseFloat(memory).toFixed(3)+" Time="+moment().format(); | ||
}) | ||
}, mins*60*1000); | ||
} | ||
|
||
return domainMiddleware({ | ||
server: { | ||
close:function(){ | ||
server.close(); | ||
mongoose.connection.close(); | ||
} | ||
}, | ||
killTimeout: 10000 | ||
}); | ||
} |
Oops, something went wrong.