-
Notifications
You must be signed in to change notification settings - Fork 6
/
Main.hs
64 lines (55 loc) · 2.41 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ExtendedDefaultRules #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}
module Main where
import Control.Concurrent.STM (TVar, newTVarIO)
import qualified Data.Map.Strict as Map
import Data.UUID (toString)
import Lucid (Html, body_, content_, doctypehtml_,
head_, href_, link_, meta_, name_,
rel_, script_, src_, title_)
import Network.Wai (Application)
import Network.Wai.Handler.Warp (run)
import Servant ((:<|>) ((:<|>)), (:>), Get,
Proxy (Proxy), Raw, Server, serve,
serveDirectory)
import Servant.HTML.Lucid (HTML)
import System.Random (randomIO)
import qualified Api.Server
import qualified Api.Types
type SiteApi = "api" :> Api.Types.Api
:<|> Get '[HTML] (Html ())
:<|> "assets" :> Raw
siteApi :: Proxy SiteApi
siteApi = Proxy
server :: TVar Api.Types.BookDB -> Server SiteApi
server bookDb = apiServer :<|> home :<|> assets
where home = return homePage
apiServer = Api.Server.server bookDb
assets = serveDirectory "frontend/dist"
homePage :: Html ()
homePage =
doctypehtml_ $ do
head_ $ do
title_ "Example Servant-Elm App"
meta_ [ name_ "viewport"
, content_ "width=device-width, initial-scale=1" ]
link_ [ rel_ "stylesheet"
, href_ "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"
]
script_ [src_ "assets/app.js"] ""
body_ (script_ "var elmApp = Elm.Main.fullscreen()")
app :: TVar Api.Types.BookDB -> Application
app bookDb = serve siteApi (server bookDb)
main :: IO ()
main = do
let port = 8000
uuid1 <- toString <$> randomIO
uuid2 <- toString <$> randomIO
let books = [ Api.Types.Book (Just uuid1) "Real World Haskell" (Api.Types.Author "Bryan O'Sullivan, Don Stewart, and John Goerzen" 1970)
, Api.Types.Book (Just uuid2) "Learn You a Haskell for Great Good" (Api.Types.Author "Miran Lipovača" 1970)
]
bookDb <- newTVarIO (Map.fromList (zip [uuid1, uuid2] books))
putStrLn $ "Serving on port " ++ show port ++ "..."
run port (app bookDb)