Skip to content

Commit

Permalink
Added a onlyAllowMethods function to filter for specific methods when…
Browse files Browse the repository at this point in the history
… building custom routing parsers
  • Loading branch information
mpscholten committed Sep 23, 2021
1 parent 1cd563f commit 1b82d4e
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions IHP/RouterSupport.hs
Expand Up @@ -23,6 +23,7 @@ CanRoute (..)
, parseText
, webSocketApp
, webSocketAppWithCustomPath
, onlyAllowMethods
) where

import qualified Prelude
Expand Down Expand Up @@ -599,6 +600,37 @@ post path action = do
_ -> fail "Invalid method, expected POST"
{-# INLINABLE post #-}

-- | Filter methods when writing a custom routing parser
--
-- __Example:__
--
-- > instance CanRoute ApiController where
-- > parseRoute' = do
-- > string "/api/"
-- > let
-- > createRecordAction = do
-- > onlyAllowMethods [POST]
-- >
-- > table <- parseText
-- > endOfInput
-- > pure CreateRecordAction { table }
-- >
-- > updateRecordAction = do
-- > onlyAllowMethods [PATCH]
-- >
-- > table <- parseText
-- > string "/"
-- > id <- parseUUID
-- > pure UpdateRecordAction { table, id }
-- >
-- > createRecordAction <|> updateRecordAction
--
onlyAllowMethods :: (?context :: RequestContext) => [StdMethod] -> Parser ()
onlyAllowMethods methods = do
method <- getMethod
unless (method `elem` methods) (fail ("Invalid method, expected one of: " <> show methods))
{-# INLINABLE onlyAllowMethods #-}

-- | Routes to a given WebSocket app if the path matches the WebSocket app name
--
-- __Example:__
Expand Down

0 comments on commit 1b82d4e

Please sign in to comment.