module Network.HTTP.Lucu.Router
(
Router
, runRouter
, anyScheme
, scheme
, http
, http'
, https
)
where
import Control.Applicative
import Control.Category
import Control.Arrow
import Control.Arrow.ArrowKleisli
import Control.Arrow.List
import Control.Monad.IO.Class
import Data.Ascii (CIAscii)
import Data.Maybe
import Network.URI hiding (scheme)
import Network.HTTP.Lucu.Utils
import Prelude.Unicode
newtype Router m α β
= Router {
unRouter ∷ ListTArrow m α β
}
deriving ( Arrow
, ArrowKleisli m
, ArrowZero
, ArrowPlus
, ArrowChoice
, ArrowApply
, Category
)
runRouter ∷ (Applicative m, MonadIO m) ⇒ Router m α β → α → m (Maybe β)
runRouter = ((listToMaybe <$>) ∘) ∘ runListTArrow ∘ unRouter
anyScheme ∷ Arrow (⇝) ⇒ URI ⇝ (Host, Path)
anyScheme = arr uriHost &&& arr uriPathSegments
scheme ∷ (ArrowChoice (⇝), ArrowZero (⇝)) ⇒ CIAscii → URI ⇝ (Host, Path)
scheme s
= proc uri →
if s ≡ uriCIScheme uri then
anyScheme ⤙ uri
else
zeroArrow ⤙ (⊥)
http ∷ (ArrowChoice (⇝), ArrowPlus (⇝), ArrowZero (⇝)) ⇒ URI ⇝ (Host, Path)
http = scheme "http" <+> scheme "https"
http' ∷ (ArrowChoice (⇝), ArrowZero (⇝)) ⇒ URI ⇝ (Host, Path)
http' = scheme "http"
https ∷ (ArrowChoice (⇝), ArrowZero (⇝)) ⇒ URI ⇝ (Host, Path)
https = scheme "https"