Hello all,
I am trying to serve html with servant using lucid,
Here is the code
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module ServantLucidExample where
import Data.Proxy ( Proxy(Proxy) )
import qualified Lucid
import qualified Network.Wai.Handler.Warp as Warp
import Servant ( Handler, serve, Server )
import Servant.API ( Get, JSON, type (:<|>)(..), type (:>) )
import qualified Servant.HTML.Lucid
type NumberAPI
= Get '[JSON] Int
type LoginAPI
= "login" :> Get '[Servant.HTML.Lucid.HTML] (Lucid.Html ())
type API = NumberAPI :<|> LoginAPI
numberHandler :: Server NumberAPI
numberHandler = pure 42
loginHandler :: Server LoginAPI
loginHandler = pure someHtml
where
someHtml :: Lucid.Html ()
someHtml =
Lucid.div_ do
Lucid.p_ "Hello"
Lucid.p_ "World"
handler :: Server API
handler = numberHandler :<|> loginHandler
main :: IO ()
main = Warp.run 8080 (serve (Proxy @API) handler)
using ghc 9.4.8
and latest servant, servant-lucid, lucid2, wai, warp libraries
it throws the following error
app/Main.hs:40:23: error:
• No instance for (lucid-2.11.20230408:Lucid.Base.ToHtml
(Lucid.HtmlT Data.Functor.Identity.Identity ()))
arising from a use of ‘serve’
There are instances for similar types:
instance (a ~ (), m ~ Data.Functor.Identity.Identity) =>
lucid-2.11.20230408:Lucid.Base.ToHtml
(lucid-2.11.20230408:Lucid.Base.HtmlT m a)
-- Defined in ‘lucid-2.11.20230408:Lucid.Base’
• In the second argument of ‘Warp.run’, namely
‘(serve (Proxy @API) handler)’
In the expression: Warp.run 8080 (serve (Proxy @API) handler)
In an equation for ‘main’:
main = Warp.run 8080 (serve (Proxy @API) handler)
|
40 | main = Warp.run 8080 (serve (Proxy @API) handler)
| ^^^^^
I have no idea why this is throwing this error and how to fix it.
Please help