Created
February 4, 2011 16:18
-
-
Save simonmichael/811291 to your computer and use it in GitHub Desktop.
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
A micro-web-app in several frameworks. | |
(if copy/testing the haskell examples, use ghc -XUnicodeSyntax) | |
-------------------------------------------------------------------------------- | |
FLASK: | |
#!/usr/bin/env python | |
# A github post-receive hook handler, runs some shell command on each HTTP POST to PORT. | |
# github-listener.py PORT 'SOME SHELL COMMAND' | |
import sys | |
from subprocess import * | |
from flask import Flask | |
def system(cmd): | |
print ''.join(Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, close_fds=True).communicate()) | |
app = Flask(__name__) | |
@app.route("/", methods=['POST']) | |
def post(): | |
cmd = sys.argv[2] | |
print "Received POST, running: "+cmd | |
system(cmd) | |
return 'ok' | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0', port=int(sys.argv[1]), debug=True) | |
-------------------------------------------------------------------------------- | |
PYRAMID: | |
#!/usr/bin/env python | |
# A github post-receive hook handler, runs some shell command on each HTTP POST to PORT. | |
# github-listener.py PORT 'SOME SHELL COMMAND' | |
import sys | |
from subprocess import * | |
from paste.httpserver import serve | |
from pyramid.config import Configurator | |
from pyramid.response import Response | |
def system(cmd): | |
print ''.join(Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, close_fds=True).communicate()) | |
def post(request): | |
cmd = sys.argv[2] | |
print "Received POST, running: "+cmd | |
system(cmd) | |
return Response('ok') | |
if __name__ == '__main__': | |
config = Configurator() | |
config.add_view(post) | |
app = config.make_wsgi_app() | |
serve(app, host='0.0.0.0',port=int(sys.argv[1])) | |
-------------------------------------------------------------------------------- | |
YESOD: | |
#!/usr/bin/env runhaskell | |
{-# LANGUAGE TypeFamilies, QuasiQuotes #-} | |
-- A github post-receive hook handler, runs some shell command on each HTTP POST to PORT. | |
-- github-listener.hs PORT 'SOME SHELL COMMAND' | |
import System.Environment (getArgs) | |
import System.Process (system) | |
import Yesod | |
data App = App {cmd∷String} | |
mkYesod "App" [$parseRoutes| | |
/ Home POST | |
|] | |
instance Yesod App where approot _ = "" | |
postHome = do | |
app ← getYesod | |
liftIO $ do | |
putStrLn $ "Received POST, running: " ++ cmd app | |
system $ cmd app | |
defaultLayout [$hamlet|ok|] | |
main = do | |
port:cmd:_ ← getArgs | |
basicHandler (read port) App{cmd=cmd} | |
-------------------------------------------------------------------------------- | |
HAPPSTACK: | |
#!/usr/bin/env runhaskell | |
-- A github post-receive hook handler, runs some shell command on each HTTP POST to PORT. | |
-- github-listener.hs PORT 'SOME SHELL COMMAND' | |
import Control.Monad.Trans (liftIO) | |
import System.Environment (getArgs) | |
import System.Process (system) | |
import Happstack.Server | |
post cmd = do | |
methodM POST | |
liftIO $ do putStrLn $ "Received POST, running: " ++ cmd | |
system cmd | |
ok "ok" | |
main = do | |
port:cmd:_ ← getArgs | |
simpleHTTP nullConf {port=read port} (post cmd) | |
-------------------------------------------------------------------------------- | |
SNAP: | |
#!/usr/bin/env runhaskell | |
-- A github post-receive hook handler, runs some shell command on each HTTP POST to PORT. $ | |
-- github-listener-snap.hs -p PORT 'SOME SHELL COMMAND' $ | |
import Control.Monad.Trans (liftIO) | |
import Snap.Http.Server (quickHttpServe) | |
import System.Environment (getArgs) | |
import System.Process (system) | |
post cmd = do | |
liftIO $ do | |
putStrLn $ "Received POST, running: " ++ cmd | |
system cmd | |
return ∅ | |
main = do | |
cmd ← getCmd | |
quickHttpServe $ post cmd | |
where | |
getCmd = do | |
args ← getArgs | |
return $ case args of | |
"-p":_:cmd:_ → cmd | |
"--port":_:cmd:_ → cmd | |
otherwise → case head args of | |
'-':_ → args ‼ 1 | |
otherwise → head args | |
-------------------------------------------------------------------------------- | |
SINATRA: | |
#!/usr/bin/env ruby | |
# A github post-receive hook handler, runs some shell command on each HTTP POST to PORT. | |
# github-listener.rb PORT 'SOME SHELL COMMAND' | |
%w{rubygems sinatra}.each{|x| require x} | |
set :port, ARGV[0] | |
post '/' do | |
cmd = ARGV[1] | |
print "Received POST, running: "+cmd | |
puts `#{cmd}` | |
"ok" | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment