module HsKu.Web where

import HsKu
import HsKu.JSON
import Data.Text
import Servant

type API = "haiku"  :> QueryParam' '[Required] "input" Text
                    :> Get '[JSON] HaikuResult

handler :: Languages -> Text -> Handler HaikuResult
handler :: Languages -> Text -> Handler HaikuResult
handler Languages
langs = HaikuResult -> Handler HaikuResult
forall a. a -> Handler a
forall (m :: * -> *) a. Monad m => a -> m a
return (HaikuResult -> Handler HaikuResult)
-> (Text -> HaikuResult) -> Text -> Handler HaikuResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Haiku -> HaikuResult
HaikuResult (Maybe Haiku -> HaikuResult)
-> (Text -> Maybe Haiku) -> Text -> HaikuResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Language, Haiku) -> Haiku)
-> Maybe (Language, Haiku) -> Maybe Haiku
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Language, Haiku) -> Haiku
forall a b. (a, b) -> b
snd (Maybe (Language, Haiku) -> Maybe Haiku)
-> (Text -> Maybe (Language, Haiku)) -> Text -> Maybe Haiku
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Languages -> Text -> Maybe (Language, Haiku)
parseHaiku Languages
langs

hskuWebService :: Languages -> Application
hskuWebService :: Languages -> Application
hskuWebService = Proxy API -> Server API -> Application
forall api.
HasServer api '[] =>
Proxy api -> Server api -> Application
serve Proxy API
api ((Text -> Handler HaikuResult) -> Application)
-> (Languages -> Text -> Handler HaikuResult)
-> Languages
-> Application
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Languages -> Text -> Handler HaikuResult
handler
  where api :: Proxy API
api = Proxy API
forall {k} (t :: k). Proxy t
Proxy :: Proxy API