Base.hs.template 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. -- vim: ft=haskell
  2. {-# LANGUAGE RecordWildCards #-}
  3. {-# LANGUAGE OverloadedStrings #-}
  4. module $module_path$.Base where
  5. import Snap.Core (Snap,route)
  6. import qualified Snap.Http.Server as HTTP
  7. import Snap.Util.FileServe (serveDirectory)
  8. data Config = Config { cfgPort :: !Int
  9. -- ^ The port to run on
  10. , cfgStaticDir :: Maybe FilePath
  11. -- ^ Content to be served off of the root, relative to
  12. -- the directory that the server was started in
  13. } deriving (Show)
  14. -- | A default @Config@ value that will produce a server that runs on port 8080,
  15. -- and serves no static content.
  16. defaultConfig :: Config
  17. defaultConfig = Config { cfgPort = 8080, cfgStaticDir = Nothing }
  18. -- | Spawn a snap server, and run the given RPC action.
  19. runServer :: Config -> Snap () -> IO ()
  20. runServer Config { .. } serveRpc =
  21. do let snapCfg :: HTTP.Config Snap ()
  22. snapCfg = HTTP.setPort cfgPort HTTP.defaultConfig
  23. HTTP.simpleHttpServe snapCfg body
  24. where
  25. body =
  26. do serveRpc
  27. case cfgStaticDir of
  28. Just path -> route [ ("", serveDirectory path) ]
  29. Nothing -> return ()