Created
January 11, 2019 12:22
-
-
Save jhrcek/9e7f85a5a2cd4ae15340cd61569a3473 to your computer and use it in GitHub Desktop.
Servant + blaze-html example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env stack | |
| -- stack script --resolver lts-13.2 --package blaze-html,servant,servant-blaze,servant-server,wai,warp | |
| {-# LANGUAGE DataKinds #-} | |
| {-# LANGUAGE OverloadedStrings #-} | |
| {-# LANGUAGE TypeOperators #-} | |
| {-# OPTIONS_GHC -Wall #-} | |
| module Main (main) where | |
| import Network.Wai (Application) | |
| import qualified Network.Wai.Handler.Warp as Warp | |
| import Prelude hiding (head) | |
| import Servant (Get, Proxy (Proxy), Server, serve) | |
| import Servant.HTML.Blaze (HTML) | |
| import Text.Blaze.Html5 (Html, body, docTypeHtml, head, li, p, title, toHtml, | |
| ul) | |
| main :: IO () | |
| main = do | |
| let port = 8000 | |
| putStrLn $ "http://localhost:" ++ show port ++ "/" | |
| Warp.run port app | |
| type API = Get '[HTML] Html | |
| app :: Application | |
| app = serve api server | |
| api :: Proxy API | |
| api = Proxy | |
| server :: Server API | |
| server = return html | |
| html :: Html | |
| html = docTypeHtml $ do | |
| head $ | |
| title "Natural numbers" | |
| body $ do | |
| p "A list of natural numbers:" | |
| ul $ mapM_ (li . toHtml) [1::Int ..10] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment