Main.hs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. {-# LANGUAGE DataKinds #-}
  2. {-# LANGUAGE OverloadedStrings #-}
  3. {-# LANGUAGE TypeOperators #-}
  4. module Main where
  5. import API
  6. import Control.Exception (bracket)
  7. import Control.Monad.IO.Class (liftIO)
  8. import Data.Text (Text, pack)
  9. import Database.PostgreSQL.Simple (Connection, close)
  10. import Network.Wai (Application)
  11. import Network.Wai.Handler.Warp (run)
  12. import Pubs
  13. import Servant
  14. import Servant.JQuery (jsForAPI)
  15. import Servant.Server
  16. pubsHandler :: Connection -> Server PubAPI
  17. pubsHandler conn =
  18. (liftIO $ getAllPubs conn)
  19. :<|> (liftIO . getPubsByTag conn)
  20. -- * Static file handling
  21. type StaticRoutes = "static" :> "pubs.js" :> Get '[PlainText] Text
  22. :<|> "static" :> Raw
  23. staticHandler :: Server StaticRoutes
  24. staticHandler = (return $ pack getJSClient)
  25. :<|> serveDirectory "/home/vincent/source/olkartet/static"
  26. -- | Generate foreign code for Mapbox demo
  27. getJSClient :: String
  28. getJSClient = jsForAPI pubAPI
  29. -- -- * Server setup
  30. -- | The server type combines the pub API with the serving of static
  31. -- assets for the web demo.
  32. -- These are not part of the pub API of course, so here they are a
  33. -- separate "API" that we combine with the real one.
  34. serverType :: Proxy (PubAPI :<|> StaticRoutes)
  35. serverType = Proxy
  36. ølkartetServer :: Connection -> Application
  37. ølkartetServer c = serve serverType
  38. (pubsHandler c :<|> staticHandler)
  39. main :: IO ()
  40. main = bracket connectØlkartetDB close
  41. (run 8080 . ølkartetServer)