Guest User

Untitled

a guest
Jul 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.37 KB | None | 0 0
  1. A micro-web-app in several frameworks.
  2.  
  3. (if copy/testing the haskell examples, use ghc -XUnicodeSyntax)
  4.  
  5. --------------------------------------------------------------------------------
  6. FLASK:
  7.  
  8. #!/usr/bin/env python
  9. # A github post-receive hook handler, runs some shell command on each HTTP POST to PORT.
  10. # github-listener.py PORT 'SOME SHELL COMMAND'
  11.  
  12. import sys
  13. from subprocess import *
  14. from flask import Flask
  15.  
  16. def system(cmd):
  17. print ''.join(Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, close_fds=True).communicate())
  18.  
  19. app = Flask(__name__)
  20.  
  21. @app.route("/", methods=['POST'])
  22. def post():
  23. cmd = sys.argv[2]
  24. print "Received POST, running: "+cmd
  25. system(cmd)
  26. return 'ok'
  27.  
  28. if __name__ == "__main__":
  29. app.run(host='0.0.0.0', port=int(sys.argv[1]), debug=True)
  30.  
  31. --------------------------------------------------------------------------------
  32. PYRAMID:
  33.  
  34. #!/usr/bin/env python
  35. # A github post-receive hook handler, runs some shell command on each HTTP POST to PORT.
  36. # github-listener.py PORT 'SOME SHELL COMMAND'
  37.  
  38. import sys
  39. from subprocess import *
  40. from paste.httpserver import serve
  41. from pyramid.config import Configurator
  42. from pyramid.response import Response
  43.  
  44. def system(cmd):
  45. print ''.join(Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, close_fds=True).communicate())
  46.  
  47. def post(request):
  48. cmd = sys.argv[2]
  49. print "Received POST, running: "+cmd
  50. system(cmd)
  51. return Response('ok')
  52.  
  53. if __name__ == '__main__':
  54. config = Configurator()
  55. config.add_view(post)
  56. app = config.make_wsgi_app()
  57. serve(app, host='0.0.0.0',port=int(sys.argv[1]))
  58.  
  59. --------------------------------------------------------------------------------
  60. YESOD:
  61.  
  62. #!/usr/bin/env runhaskell
  63. {-# LANGUAGE TypeFamilies, QuasiQuotes #-}
  64. -- A github post-receive hook handler, runs some shell command on each HTTP POST to PORT.
  65. -- github-listener.hs PORT 'SOME SHELL COMMAND'
  66.  
  67. import System.Environment (getArgs)
  68. import System.Process (system)
  69. import Yesod
  70.  
  71. data App = App {cmd∷String}
  72. mkYesod "App" [$parseRoutes|
  73. / Home POST
  74. |]
  75. instance Yesod App where approot _ = ""
  76.  
  77. postHome = do
  78. app ← getYesod
  79. liftIO $ do
  80. putStrLn $ "Received POST, running: " ++ cmd app
  81. system $ cmd app
  82. defaultLayout [$hamlet|ok|]
  83.  
  84. main = do
  85. port:cmd:_ ← getArgs
  86. basicHandler (read port) App{cmd=cmd}
  87.  
  88. --------------------------------------------------------------------------------
  89. HAPPSTACK:
  90.  
  91. #!/usr/bin/env runhaskell
  92. -- A github post-receive hook handler, runs some shell command on each HTTP POST to PORT.
  93. -- github-listener.hs PORT 'SOME SHELL COMMAND'
  94.  
  95. import Control.Monad.Trans (liftIO)
  96. import System.Environment (getArgs)
  97. import System.Process (system)
  98. import Happstack.Server
  99.  
  100. post cmd = do
  101. methodM POST
  102. liftIO $ do putStrLn $ "Received POST, running: " ++ cmd
  103. system cmd
  104. ok "ok"
  105.  
  106. main = do
  107. port:cmd:_ ← getArgs
  108. simpleHTTP nullConf {port=read port} (post cmd)
  109.  
  110. --------------------------------------------------------------------------------
  111. SNAP:
  112.  
  113. #!/usr/bin/env runhaskell
  114. -- A github post-receive hook handler, runs some shell command on each HTTP POST to PORT. $
  115. -- github-listener-snap.hs -p PORT 'SOME SHELL COMMAND' $
  116.  
  117. import Control.Monad.Trans (liftIO)
  118. import Snap.Http.Server (quickHttpServe)
  119. import System.Environment (getArgs)
  120. import System.Process (system)
  121.  
  122. post cmd = do
  123. liftIO $ do
  124. putStrLn $ "Received POST, running: " ++ cmd
  125. system cmd
  126. return ∅
  127.  
  128. main = do
  129. cmd ← getCmd
  130. quickHttpServe $ post cmd
  131. where
  132. getCmd = do
  133. args ← getArgs
  134. return $ case args of
  135. "-p":_:cmd:_ → cmd
  136. "--port":_:cmd:_ → cmd
  137. otherwise → case head args of
  138. '-':_ → args ‼ 1
  139. otherwise → head args
  140.  
  141. --------------------------------------------------------------------------------
  142. SINATRA:
  143.  
  144. #!/usr/bin/env ruby
  145. # A github post-receive hook handler, runs some shell command on each HTTP POST to PORT.
  146. # github-listener.rb PORT 'SOME SHELL COMMAND'
  147.  
  148. %w{rubygems sinatra}.each{|x| require x}
  149.  
  150. set :port, ARGV[0]
  151.  
  152. post '/' do
  153. cmd = ARGV[1]
  154. print "Received POST, running: "+cmd
  155. puts `#{cmd}`
  156. "ok"
  157. end
Add Comment
Please, Sign In to add comment