Last active
July 27, 2017 12:35
-
-
Save ShikherVerma/e4d74094dd677d707ec2b945a00d5748 to your computer and use it in GitHub Desktop.
minimal git http server. Run with `node git-http-server.js` (npm modules : http, child_process, path, git-http-backend, zlib)
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
var http = require('http'); | |
var spawn = require('child_process').spawn; | |
var path = require('path'); | |
var backend = require('git-http-backend'); | |
var zlib = require('zlib'); | |
var server = http.createServer(function (req, res) { | |
var repo = req.url.split('/')[1]; | |
var dir = path.join(__dirname, 'repos', repo); | |
var reqStream = req.headers['content-encoding'] == 'gzip' ? req.pipe(zlib.createGunzip()) : req; | |
reqStream.pipe(backend(req.url, function (err, service) { | |
if (err) return res.end(err + '\n'); | |
res.setHeader('content-type', service.type); | |
console.log(service.action, repo, service.fields); | |
var ps = spawn(service.cmd, service.args.concat(dir)); | |
ps.stdout.pipe(service.createStream()).pipe(ps.stdin); | |
})).pipe(res); | |
}); | |
server.listen(5000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment