Base.hs.template 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. -- vim: ft=haskell
  2. {-# LANGUAGE RecordWildCards #-}
  3. module $module_prefix$Base where
  4. import System.Environment
  5. import System.Console.GetOpt
  6. import Snap.Http.Server (simpleHttpServe,defaultConfig)
  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 = simpleHttpServe snapConfig server
  21. where
  22. server =
  23. do let snapCfg = setPort cfgPort defaultConfig
  24. simpleHttpServe snapCfg body
  25. body =
  26. do serveRpc
  27. case cfgStaticDir of
  28. Just path -> route [ ("", serveDirectory path) ]
  29. Nothing -> return ()