1 {-# LANGUAGE DataKinds #-} 2 {-# LANGUAGE DeriveGeneric #-} 3 {-# LANGUAGE FlexibleInstances #-} 4 {-# LANGUAGE GeneralizedNewtypeDeriving #-} 5 {-# LANGUAGE MultiParamTypeClasses #-} 6 {-# LANGUAGE OverloadedStrings #-} 7 {-# LANGUAGE RankNTypes #-} 8 {-# LANGUAGE ScopedTypeVariables #-} 9 {-# LANGUAGE TypeOperators #-} 10 11 -- | Utilities for serving a Servant API with Action handlers. 12 module Databrary.Action.Servant 13 ( 14 -- * Building the application 15 servantApp 16 ) where 17 18 import Servant 19 20 import Databrary.API 21 import Databrary.Action (ActionRouteApp (..)) 22 23 -- | Create a server to serve the ServantAPI. 24 servantServer :: ActionRouteApp -> Server ServantAPI 25 servantServer (ActionRouteApp app) = preferAngularServer :<|> Tagged app 26 27 preferAngularServer :: Server PreferAngularAPI 28 preferAngularServer _ _ = emptyServer 29 -- Up next: Serve routes that prefer Angular. 30 -- preferAngularServer :: Server PreferAngularAPI 31 -- preferAngularServer u j = 32 -- -- userLogin 33 -- :<|> foo 34 -- :<|> bar 35 -- where 36 -- angular' = preferAngular u j 37 -- userLogin = angular' (pure "no angular, no") 38 -- 39 -- enableAngular :: Maybe Text -> Maybe Int -> Bool 40 -- enableAngular _ jsOpt = maybe True (/= 0) jsOpt 41 -- 42 -- preferAngular mUserAgent mJsParam fallback = 43 -- if enableAngular mUserAgent mJsParam 44 -- then pure "I am an Angular!" 45 -- else fallback 46 47 -- | Boilerplate. This will go away when Servant adopts type application. 48 servantAPI :: Proxy ServantAPI 49 servantAPI = Proxy 50 51 -- | The lowest level of the Databrary 'web framework'. It simply builds a Wai 52 -- Application out of the 'servantServer' description. 53 -- 54 -- Since Servant does not control most routes yet, this function delegates the 55 -- rest to the original ActionRoute-based app. 56 servantApp :: ActionRouteApp -> Application 57 servantApp arApp = 58 serve servantAPI (servantServer arApp)