scotty 0.2.0 → 0.3.0
raw patch · 5 files changed
+28/−11 lines, 5 filesdep ~http-types
Dependency ranges changed: http-types
Files
- README +1/−1
- Web/Scotty.hs +11/−7
- examples/basic.hs +13/−0
- examples/urlshortener.hs +1/−1
- scotty.cabal +2/−2
README view
@@ -1,5 +1,5 @@ Scotty-=====+====== A Haskell web framework inspired by Ruby's Sinatra, using WAI and Warp.
Web/Scotty.hs view
@@ -12,7 +12,7 @@ , middleware, get, post, put, delete, addroute -- * Defining Actions -- ** Accessing the Request, Captures, and Query Parameters- , request, param, jsonData+ , request, body, param, jsonData -- ** Modifying the Response and Redirecting , status, header, redirect -- ** Setting Response Body@@ -156,18 +156,22 @@ -- OR -- -- > redirect "/foo/bar"-redirect :: T.Text -> ActionM ()+redirect :: T.Text -> ActionM a redirect = throwError . Redirect -- | Get the 'Request' object. request :: ActionM Request request = getReq <$> ask +-- | Get the request body.+body :: ActionM BL.ByteString+body = getBody <$> ask+ -- | Parse the request body as a JSON object and return it. Raises an exception if parse is unsuccessful. jsonData :: (A.FromJSON a) => ActionM a jsonData = do- body <- getBody <$> ask- maybe (raise "jsonData: no parse") return $ A.decode body+ b <- body+ maybe (raise "jsonData: no parse") return $ A.decode b -- | Get a parameter. First looks in captures, then form data, then query parameters. --@@ -266,15 +270,15 @@ mkEnv :: StdMethod -> Request -> [Param] -> ResourceT IO ActionEnv mkEnv method req captures = do- body <- BL.fromChunks <$> (lazyConsume $ requestBody req)+ b <- BL.fromChunks <$> (lazyConsume $ requestBody req) let params = captures ++ formparams ++ queryparams formparams = case (method, lookup "Content-Type" [(CI.mk k, CI.mk v) | (k,v) <- requestHeaders req]) of- (POST, Just "application/x-www-form-urlencoded") -> parseEncodedParams $ mconcat $ BL.toChunks body+ (POST, Just "application/x-www-form-urlencoded") -> parseEncodedParams $ mconcat $ BL.toChunks b _ -> [] queryparams = parseEncodedParams $ rawQueryString req - return $ Env req params body+ return $ Env req params b parseEncodedParams :: B.ByteString -> [Param] parseEncodedParams bs = [ (T.fromStrict k, T.fromStrict $ fromMaybe "" v) | (k,v) <- parseQueryText bs ]
examples/basic.hs view
@@ -9,6 +9,8 @@ import Network.HTTP.Types (status302) +import Data.Text.Lazy.Encoding (decodeUtf8)+ main :: IO () main = scotty 3000 $ do -- Add any WAI middleware, they are run top-down.@@ -65,6 +67,17 @@ get "/ints/:is" $ do is <- param "is" json $ [(1::Int)..10] ++ is++ get "/setbody" $ do+ html $ mconcat ["<form method=POST action=\"readbody\">"+ ,"<input type=text name=something>"+ ,"<input type=submit>"+ ,"</form>"+ ]++ post "/readbody" $ do+ b <- body+ text $ decodeUtf8 b {- If you don't want to use Warp as your webserver, you can use any WAI handler.
examples/urlshortener.hs view
@@ -23,7 +23,7 @@ middleware logStdoutDev middleware static - m <- liftIO $ newMVar (0::Int,M.empty)+ m <- liftIO $ newMVar (0::Int,M.empty :: M.Map Int T.Text) get "/" $ do html $ renderHtml
scotty.cabal view
@@ -1,5 +1,5 @@ Name: scotty-Version: 0.2.0+Version: 0.3.0 Synopsis: Haskell web framework inspired by Ruby's Sinatra, using WAI and Warp Homepage: https://github.com/xich/scotty Bug-reports: https://github.com/xich/scotty/issues@@ -72,7 +72,7 @@ case-insensitive >= 0.4, conduit >= 0.1.1.1, data-default >= 0.3,- http-types >= 0.6.8,+ http-types >= 0.6.8 && < 0.7, mtl >= 2.0.1, text >= 0.11.1, wai >= 1.0.0,