module Web.Scotty
(
scotty, scottyApp
, middleware, get, post, put, delete, addroute, matchAny, notFound
, capture, regex, function, literal
, Action
, request, body, param, jsonData
, status, header, redirect
, text, html, file, json
, raise, rescue, next
, ScottyM, ActionM, Param, Parsable, RoutePattern
) where
import Blaze.ByteString.Builder (fromByteString)
import Control.Monad.State (execStateT, modify)
import Data.Default (def)
import Network.HTTP.Types (status404)
import Network.Wai
import Network.Wai.Handler.Warp (Port, run)
import Web.Scotty.Action
import Web.Scotty.Route
import Web.Scotty.Types
scotty :: Port -> ScottyM () -> IO ()
scotty p s = do
putStrLn $ "Setting phasers to stun... (ctrl-c to quit) (port " ++ show p ++ ")"
run p =<< scottyApp s
scottyApp :: ScottyM () -> IO Application
scottyApp defs = do
s <- execStateT (runS defs) def
return $ foldl (flip ($)) notFoundApp $ routes s ++ middlewares s
notFoundApp :: Application
notFoundApp _ = return $ ResponseBuilder status404 [("Content-Type","text/html")]
$ fromByteString "<h1>404: File Not Found!</h1>"
middleware :: Middleware -> ScottyM ()
middleware = modify . addMiddleware