SimpleServer (empty) → 0.1.1.0
raw patch · 5 files changed
+147/−0 lines, 5 filesdep +basedep +cmdargsdep +dyresetup-changed
Dependencies added: base, cmdargs, dyre, transformers, wai-routes, warp
Files
- LICENSE +7/−0
- Setup.hs +2/−0
- SimpleServer.cabal +36/−0
- src/Main.hs +9/−0
- src/SimpleServer.hs +93/−0
+ LICENSE view
@@ -0,0 +1,7 @@+Copyright (c) 2012-2015 Anupam Jain++Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ SimpleServer.cabal view
@@ -0,0 +1,36 @@+-- SimpleServer: A simple static file server, for when apache is overkill+name : SimpleServer+version : 0.1.1.0+synopsis : A simple static file server, for when apache is overkill+description : A simple static file server, for when apache is overkill+license : MIT+license-file : LICENSE+author : Anupam Jain+maintainer : ajnsit@gmail.com+category : Web+build-type : Simple+cabal-version : >=1.16++library+ exposed-modules : SimpleServer+ build-depends : base+ , wai-routes+ , warp+ , cmdargs+ , dyre+ , transformers+ buildable : True+ hs-source-dirs : src+ default-language : Haskell2010++executable simpleserver+ main-is : Main.hs+ build-depends : base >= 4.7 && < 4.9+ , wai-routes+ , warp+ , cmdargs+ , dyre+ , transformers+ buildable : True+ hs-source-dirs : src+ default-language : Haskell2010
+ src/Main.hs view
@@ -0,0 +1,9 @@+module Main where++import SimpleServer++handlers :: Monad m => m ()+handlers = return ()++main :: IO ()+main = simpleServer handlers
+ src/SimpleServer.hs view
@@ -0,0 +1,93 @@+{-# LANGUAGE OverloadedStrings, DeriveDataTypeable #-}+module SimpleServer+( module Network.Wai.Middleware.Routes+, module Control.Monad.IO.Class+, simpleServer+)+where++import Control.Monad (when)+import Control.Monad.IO.Class (liftIO)++import Config.Dyre+import Config.Dyre.Paths+import System.Console.CmdArgs+import Network.Wai.Handler.Warp+import Network.Wai.Middleware.Routes+++---------------------+-- WAI Application --+---------------------++runHandlers :: Handlers -> IO ()+runHandlers handlers = do+ settings <- cmdArgs simpleServerCmdArgs+ if paths settings+ then do+ (_,_,p,_,_) <- getPaths simpleServerDyreParams+ putStrLn p+ else do+ v <- getVerbosity+ let p = port settings+ logVerbose v $ "SimpleServer running on port " ++ show p+ run p $ waiApp $ application settings v handlers++application :: SimpleServerConfig -> Verbosity -> Handlers -> RouteM ()+application settings v handlers = do+ when (v == Loud) $ middleware logStdoutDev+ handlers+ catchall $ staticApp $ defaultFileServerSettings $ static settings++logVerbose :: Verbosity -> String -> IO ()+logVerbose Quiet _ = return ()+logVerbose _ s = putStrLn s+++----------------------------+-- Command line arguments --+----------------------------++data SimpleServerConfig = SimpleServerConfig+ { port :: Int+ , static :: String+ , paths :: Bool+ }+ deriving (Data, Typeable)++simpleServerCmdArgs :: SimpleServerConfig+simpleServerCmdArgs = SimpleServerConfig+ { port = 8000+ &= help "Port on which the server runs (default 8000)"+ &= opt (8000::Int)+ &= name "p"+ , static = "."+ &= help "Folder with the static files (default (\".\"))"+ &= opt ("."::String)+ , paths = False+ &= help "Print the expected path to the simpleserver config"+ }+ &= verbosity+ &= program "simpleserver"+ &= summary "SimpleServer v0.1.1"+++-----------------+-- Dyre Config --+-----------------++type Handlers = RouteM ()++-- TODO: This only prints errors on incoming requests+confError :: Handlers -> String -> Handlers+confError _ err = handler $ runHandlerM $ liftIO $ putStrLn $ "Error:" ++ err++simpleServer :: Handlers -> IO ()+simpleServer = wrapMain simpleServerDyreParams++simpleServerDyreParams :: Params Handlers+simpleServerDyreParams = defaultParams+ { projectName = "simpleServer"+ , showError = confError+ , realMain = runHandlers+ }