Skip to content

Instantly share code, notes, and snippets.

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
@mightybyte
mightybyte / gist:866161
Created March 11, 2011 16:44
Useful Heist Splices
------------------------------------------------------------------------------
-- | Renders the child nodes only if the request comes from an authenticated
-- user.
ifLoggedIn :: Splice Application
ifLoggedIn = do
node <- getParamNode
res <- lift $ requireUser (return []) (return $ childNodes node)
return res
@mightybyte
mightybyte / gist:871084
Created March 15, 2011 17:25
mapBind variations
------------------------------------------------------------------------------
-- | Runs the parameter node's children and returns the resulting node list.
-- By itself this function is a simple passthrough splice that makes the
-- spliced node disappear. In combination with locally bound splices, this
-- function makes it easier to pass the desired view into your splices.
runChildren :: Monad m => Splice m
runChildren = runNodeList . X.childNodes =<< getParamNode
------------------------------------------------------------------------------
@mightybyte
mightybyte / gist:873828
Created March 17, 2011 04:19
iterative inorder tree traversal
#include <stdio.h>
#include <string.h>
struct node {
char c;
node * left;
node * right;
};
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Numeric
import Data.Bits
import Data.List
import Text.Blaze
import Text.Blaze.Html5 hiding (map)
import qualified Text.Blaze.Html5 as H
import qualified Text.Blaze.Html5.Attributes as A
bindStrict :: Monad m => Splice m
bindStrict = do
node <- getParamNode
cs <- runChildren
maybe (return ()) (add cs)
(X.getAttribute bindAttr node)
return []
where
add cs nm = modifyTS $ bindSplice nm $ do
@mightybyte
mightybyte / Lensed.hs
Created August 12, 2011 14:29
Lensed
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Snap.Snaplet.Internal.Lensed where
import Control.Applicative
import Control.Monad
import Control.Monad.Trans
import Data.Lens.Lazy
import Data.Functor
@mightybyte
mightybyte / gist:1243114
Created September 26, 2011 19:19
MonadSnapletState
class Monad m => MonadSnapletState s m | m -> s where
getSnapletState :: m (Snaplet s)
putSnapletState :: (Snaplet s) -> m ()
modifySnapletState :: (MonadSnapletState s m) => (Snaplet s -> Snaplet s) -> m ()
modifySnapletState f = do
s <- getSnapletState
putSnapletState (f s)
@mightybyte
mightybyte / MongoDB.hs
Created April 9, 2012 22:18
Snap.Snaplet.Auth.Backends.MongoDB
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Snap.Snaplet.Auth.Backends.MongoDB
( initMongoAuth
) where
------------------------------------------------------------------------------
import Control.Arrow
routes = [ ("login", with auth loginHandler)
, ("register", with auth registerHandler)
, ("logout", with auth logout >> redirect ".")
]
passParam paramName = maybe pass return =<< getParam paramName
loginHandler = do
username <- passParam "username"
password <- passParam "password"