Webanwendung mit FastCGI und Haskell
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

49 Zeilen
1.3KB

  1. {-# LANGUAGE FlexibleInstances,
  2. GeneralizedNewtypeDeriving #-}
  3. module AppMonad (
  4. AppState(..), App, runApp, setHeistState, setNumVisited
  5. ) where
  6. import Control.Monad.State
  7. import Control.Monad.Catch
  8. import Network.FastCGI
  9. import Network.CGI.Monad
  10. import Heist
  11. import DatabaseData
  12. data AppState = AppState { heist :: Maybe (HeistState App),
  13. numVisited :: Int,
  14. connection :: Connection }
  15. setHeistState :: HeistState App -> AppState -> AppState
  16. setHeistState hs as = as { heist = Just hs }
  17. setNumVisited :: Int -> AppState -> AppState
  18. setNumVisited n as = as { numVisited = n }
  19. newtype AppT m a = App { getState :: StateT AppState (CGIT m) a }
  20. deriving (Monad, MonadIO, MonadState AppState,
  21. Applicative, Functor)
  22. type App = AppT IO
  23. instance MonadCGI (AppT IO) where
  24. cgiAddHeader n v = App . lift $ cgiAddHeader n v
  25. cgiGet x = App . lift $ cgiGet x
  26. instance MonadThrow (AppT IO) where
  27. throwM e = App . lift $ throwM e
  28. instance MonadCatch (AppT IO) where
  29. {-catch :: AppT IO a -> (e -> AppT IO a) -> AppT IO a-}
  30. catch (App x) f = App $ catch x (getState . f)
  31. runApp :: AppState -> App CGIResult -> IO ()
  32. runApp as x = do
  33. let (App handled) = handleErrors x
  34. y = evalStateT handled as
  35. runFastCGIorCGI y