diff --git a/ebook-manager.cabal b/ebook-manager.cabal index 23ec6f8..82d261d 100644 --- a/ebook-manager.cabal +++ b/ebook-manager.cabal @@ -20,9 +20,11 @@ executable ebook-manager other-modules: Devel.Main , Server , API + , Types -- other-extensions: build-depends: base >=4.10 && <4.11 , servant + , monad-logger , servant-server , servant-docs , classy-prelude diff --git a/src/API.hs b/src/API.hs index 1b5dcc5..4928c28 100644 --- a/src/API.hs +++ b/src/API.hs @@ -17,6 +17,7 @@ import Servant import Servant.HTML.Lucid (HTML) import Lucid (HtmlT, ToHtml(..)) import qualified Lucid.Html5 as H +import Types data Index = Index @@ -39,5 +40,5 @@ instance ToHtml Index where type API = Get '[HTML] Index -handler :: ServerT API Handler +handler :: ServerT API AppM handler = return Index diff --git a/src/Main.hs b/src/Main.hs index fedc74f..c75a7c4 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -2,9 +2,10 @@ module Main where import Server (server) import Network.Wai.Handler.Warp (run) +import Types defaultMain :: IO () -defaultMain = run 8080 server +defaultMain = run 8080 (server App) main :: IO () main = defaultMain diff --git a/src/Server.hs b/src/Server.hs index 845f6a9..d824cac 100644 --- a/src/Server.hs +++ b/src/Server.hs @@ -13,15 +13,18 @@ module Server where import qualified API as API import Servant +import Types +import ClassyPrelude hiding (Handler) +import Control.Monad.Logger +import Control.Monad.Except type API = API.API :<|> "static" :> Raw -handler :: ServerT API Handler -handler = API.handler :<|> serveDirectoryFileServer "static" - -server :: Application -server = serve api handler +server :: App -> Application +server app = serve api (enter server' API.handler :<|> serveDirectoryFileServer "static") where + server' :: AppM :~> Servant.Handler + server' = NT (Handler . ExceptT . try . (`runReaderT` app) . (runFileLoggingT "logs/server.log")) api :: Proxy API api = Proxy diff --git a/src/Types.hs b/src/Types.hs new file mode 100644 index 0000000..af201f3 --- /dev/null +++ b/src/Types.hs @@ -0,0 +1,9 @@ +{-# Language NoImplicitPrelude #-} +module Types where + +import ClassyPrelude +import Control.Monad.Logger + +data App = App + +type AppM = LoggingT (ReaderT App IO)