UNPKG

mof-proxy

Version:

proxy middleware for floodesh

38 lines (30 loc) 1 kB
'use strict'; const request = require('request') , os= require('os'); module.exports = (options)=>{ return function* (ctx, next){ if(ctx.app.config.downloader.proxy || ctx.opt.proxy){// do nothing if exists proxy in configuration return next(); } let filter = "", customedConfig = false; if(options.filter){ customedConfig = true; filter = JSON.stringify(options.filter); } yield new Promise((resolve) => { request({uri:`${options.server}/proxyapi/acquire?appName=${ctx.app.name}&uid=${os.hostname()}_${process.pid}&customedConfig=${customedConfig}&filter=${filter}`, json:true}, (err, res, body) => { if(err){ ctx.app.logger.error(err); }else if (!body.success) { ctx.app.logger.error(body.message); }else{ ctx.opt.proxy = body.proxy; ctx.opt.limiter = Math.ceil(Math.random()*ctx.app.config.gearman.jobs); ctx.app.logger.debug("Use proxy: %s",ctx.opt.proxy, ctx.job.uuid); } resolve(); }); }); return next(); }; };