module Web.Scotty
(
scotty, scottyApp, scottyOpts, Options(..)
, middleware, get, post, put, delete, addroute, matchAny, notFound
, capture, regex, function, literal
, Action
, request, reqHeader, body, param, params, jsonData, files
, status, header, redirect
, text, html, file, json, source
, raise, rescue, next
, Param, Parsable(..), readEither
, ScottyM, ActionM, RoutePattern, File
) where
import Blaze.ByteString.Builder (fromByteString)
import Control.Monad (when)
import Control.Monad.State (execStateT, modify)
import Data.Default (def)
import Network.HTTP.Types (status404)
import Network.Wai
import Network.Wai.Handler.Warp (Port, runSettings, settingsPort)
import Web.Scotty.Action
import Web.Scotty.Route
import Web.Scotty.Types
scotty :: Port -> ScottyM () -> IO ()
scotty p = scottyOpts $ def { settings = (settings def) { settingsPort = p } }
scottyOpts :: Options -> ScottyM() -> IO ()
scottyOpts opts s = do
when (verbose opts > 0) $
putStrLn $ "Setting phasers to stun... (port " ++ show (settingsPort (settings opts)) ++ ") (ctrl-c to quit)"
runSettings (settings opts) =<< 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