mirror of
https://github.com/amark/gun.git
synced 2025-06-07 06:36:46 +00:00
HTTP/HTTPS ENV VAR, 8765 DEFAULT PORT
This commit is contained in:
parent
f572176e59
commit
6512f90fb9
@ -1,22 +1,20 @@
|
|||||||
var port = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765;
|
var fs = require('fs');
|
||||||
|
var config = {
|
||||||
|
port: process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765
|
||||||
|
};
|
||||||
|
var Gun = require('../'); // require('gun')
|
||||||
|
|
||||||
var Gun = require('../');
|
if(process.env.HTTPS_KEY){
|
||||||
|
config.key = fs.readFileSync(process.env.HTTPS_KEY);
|
||||||
var server = require('http').createServer(function(req, res){
|
config.cert = fs.readFileSync(process.env.HTTPS_CERT);
|
||||||
if(Gun.serve(req, res)){ return } // filters gun requests!
|
config.server = require('https').createServer(config, Gun.serve(__dirname));
|
||||||
require('fs').createReadStream(require('path').join(__dirname, req.url)).on('error',function(){ // static files!
|
} else {
|
||||||
res.writeHead(200, {'Content-Type': 'text/html'});
|
config.server = require('http').createServer(Gun.serve(__dirname));
|
||||||
res.end(require('fs')
|
}
|
||||||
.readFileSync(require('path')
|
|
||||||
.join(__dirname, 'index.html') // or default to index
|
|
||||||
));
|
|
||||||
}).pipe(res); // stream
|
|
||||||
});
|
|
||||||
|
|
||||||
var gun = Gun({
|
var gun = Gun({
|
||||||
web: server
|
web: config.server
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(port);
|
config.server.listen(config.port);
|
||||||
|
console.log('Server started on port ' + config.port + ' with /gun');
|
||||||
console.log('Server started on port ' + port + ' with /gun');
|
|
20
lib/serve.js
20
lib/serve.js
@ -1,4 +1,18 @@
|
|||||||
module.exports = function serve(req, res, next){
|
var fs = require('fs');
|
||||||
|
var path = require('path');
|
||||||
|
|
||||||
|
function CDN(dir){
|
||||||
|
return function(req, res){
|
||||||
|
if(serve(req, res)){ return } // filters GUN requests!
|
||||||
|
fs.createReadStream(path.join(dir, req.url)).on('error',function(){ // static files!
|
||||||
|
res.writeHead(200, {'Content-Type': 'text/html'});
|
||||||
|
res.end(fs.readFileSync(path.join(dir, 'index.html'))); // or default to index
|
||||||
|
}).pipe(res); // stream
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function serve(req, res, next){
|
||||||
|
if(typeof req === 'string'){ return CDN(req) }
|
||||||
if(!req || !res){ return false }
|
if(!req || !res){ return false }
|
||||||
next = next || serve;
|
next = next || serve;
|
||||||
if(!req.url){ return next() }
|
if(!req.url){ return next() }
|
||||||
@ -17,4 +31,6 @@ module.exports = function serve(req, res, next){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports = serve;
|
Loading…
x
Reference in New Issue
Block a user