synquery-redirect-service
Version:
synquery redirect talent
47 lines (44 loc) • 1.94 kB
JavaScript
/**
* [synquery.redirect] start.js
* シンプルなリダイレクトサービスを提供する。
*/
(g=>{ // g === module.expots
const argv = require('minimist')(process.argv.slice(2));
// console.log('[synquery.redirect]', argv);
// Configuration must be specified by a static json file
// TODO this gateway will a temporary buffer for maintenance situation
const Default = {
Protocol: 'http',
Port: 8080, // Assume that WELL-KNOWN PORT is supported by port-forwarding not to root execute for the redirect process
// e.g.) ssh -L 0.0.0.0:80:127.0.0.1:8080 -i ${HOME}/.ssh/pf_rsa 0.0.0.0 -N or docker ports: 80:8080
StatusCode: 307, // 301 Moved Permanently 307 Temporary Redirect 308 Permanent Redirect
ResponseHeaders: { }
};
const config = {
protocol: Default.Protocol,
port: Default.Port,
statusCode: Default.StatusCode,
responseHeaders: Default.ResponseHeaders
};
try {
const configPath = argv._[0];
if(configPath) { Object.assign(config, require( configPath )); }
} catch(e) {
/*BASICALLY IGNORE*/
// Notificate only when File Not Found or Could Not Parse
console.log('[synquery.redirect][Warn]', e);
}
const ptcl = config.protocol.toLowerCase().replace(/\W/, '');
require(ptcl).createServer(config.serverOptions || { }, (req, res) => {
try {
const requestedOrigin = req.headers['origin'] || [ ptcl + '://', req.headers.host ].join(''), redirectOrigin = requestedOrigin.replace(ptcl + ':', 'http' + (ptcl == 'http' ? 's': '') + ':');
res.writeHead(config.statusCode, Object.assign(config.responseHeaders, { // Moved Permanently
'Location': [ redirectOrigin, req.url ].join('')
}));
res.end();
} catch(e) {
res.end(e ? e.message || e: 'redirect service error');
}
}).listen(config.port);
console.log(`[synquery.redirect][Info] redirect service is working at port:${config.port}`);
})(this);