Created
June 26, 2012 15:11
-
-
Save creationix/2996348 to your computer and use it in GitHub Desktop.
meta server for sites on creationix.com server.
This file contains hidden or 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
// Load some built-in modules | |
var HTTP = require('http'), | |
HTTPS = require('https'), | |
ChildProcess = require('child_process'), | |
QueryString = require('querystring'), | |
FS = require('fs'); | |
// Load some npm community modules | |
var Stack = require('stack'), | |
Wheat = require('wheat'), | |
Creationix = require('creationix'); | |
// Define our shared application structure that will be used for both servers | |
var handle = Stack( | |
// Log all requests that go through the system to stdout | |
require('./lib/log')(), | |
Creationix.vhost("howtonode.org", Stack.compose( | |
Creationix.postReceive("/post-receive", __dirname + "/howtonode.org/post-receive.sh"), | |
Wheat(__dirname + "/howtonode.git") | |
)), | |
// Redirect luvit.org to luvit.io | |
Creationix.vhost("luvit.org", function (req, res, next) { | |
res.writeHead(301, {Location: "http://luvit.io/"}); | |
res.end(); | |
}), | |
Creationix.vhost("luvit.io", Stack.compose( | |
Creationix.postReceive("/post-receive", __dirname + "/luvit.io/post-receive.sh"), | |
Creationix.static("/", __dirname + "/luvit.io", "index.html"), | |
Creationix.indexer("/", __dirname + "/luvit.io") | |
)), | |
Creationix.vhost("lily.timmir.com", Creationix.static("/", __dirname + "/lily.timmir.com", "index.html")), | |
Creationix.vhost("creationix.com", Stack.compose( | |
Creationix.static("/", __dirname + "/creationix.com/public", "index.html"), | |
Creationix.indexer("/", __dirname + "/creationix.com/public") | |
)) | |
); | |
var host = "creationix.com"; | |
// Detect if we're running as root or not | |
var isRoot = !process.getuid(); | |
// Serve over HTTP | |
var httpPort = isRoot ? 80: 8080; | |
HTTP.createServer(handle).listen(httpPort); | |
console.log("Server listening at http://" + host + (httpPort === 80 ? "" : ":" + httpPort) + "/"); | |
// Server over HTTPS | |
var httpsPort = isRoot ? 443 : 8443; | |
HTTPS.createServer({ | |
key: FS.readFileSync(__dirname + '/keys/privatekey.pem'), | |
cert: FS.readFileSync(__dirname + '/keys/certificate.pem') | |
}, handle).listen(httpsPort); | |
console.log("Server listening at https://" + host + (httpsPort === 443 ? "" : ":" + httpsPort) + "/"); | |
// It's not good to stay running as root, so we'd better drop privileges | |
if (isRoot) { | |
// Lets change to the owner of this file, whoever that may be | |
var stat = FS.statSync(__filename); | |
console.log("Changing gid to " + stat.gid); | |
process.setgid(stat.gid); | |
console.log("Changing uid to " + stat.uid); | |
process.setuid(stat.uid); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment