on master help sign in
cmdargs
commitecb2a7fe7f4236b3985ce44bc18eacf0d373d387 authorMichael Snoyman <michael@snoyman.com> dateThu 27 Jan 2011, 19:17 (16 years ago) parent8ed6efc
4 files changed+48−8
LICENSE+30 Setup.hs+2 warp-static.cabal+1 warp.hs+15−8
LICENSEnew file+30
@@ -0,0 +1,30 @@
1+Copyright (c)2011, Michael Snoyman
2+
3+All rights reserved.
4+
5+Redistribution and use in source and binary forms, with or without
6+modification, are permitted provided that the following conditions are met:
7+
8+ * Redistributions of source code must retain the above copyright
9+ notice, this list of conditions and the following disclaimer.
10+
11+ * Redistributions in binary form must reproduce the above
12+ copyright notice, this list of conditions and the following
13+ disclaimer in the documentation and/or other materials provided
14+ with the distribution.
15+
16+ * Neither the name of Michael Snoyman nor the names of other
17+ contributors may be used to endorse or promote products derived
18+ from this software without specific prior written permission.
19+
20+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Setup.hsnew file+2
@@ -0,0 +1,2 @@
1+import Distribution.Simple
2+main = defaultMain
warp-static.cabal+1
@@ -16,3 +16,4 @@ Executable warp
1616 Build-depends: base >= 4 && < 5
1717 , warp >= 0.3 && < 0.4
1818 , wai-app-static >= 0.0 && < 0.1
19+ , cmdargs >= 0.6.7 && < 0.7
warp.hs+15−8
@@ -1,21 +1,28 @@
1+{-# LANGUAGE DeriveDataTypeable, RecordWildCards #-}
12import Network.Wai.Application.Static
23 ( StaticSettings (..), staticApp, defaultMimeTypeByExt, defaultListing
34 )
45import Network.Wai.Handler.Warp (run)
56import System.Environment (getArgs)
7+import System.Console.CmdArgs
8+
9+data Args = Args
10+ { docroot :: FilePath
11+ , index :: [FilePath]
12+ , port :: Int
13+ , noindex :: Bool
14+ }
15+ deriving (Show, Data, Typeable)
16+
17+defaultArgs = Args "." ["index.html", "index.htm"] 3000 False
618
719main :: IO ()
820main = do
21+ Args {..} <- cmdArgs defaultArgs
922 args <- getArgs
10− (folder, port) <- return $
11− case args of
12− [] -> (".", 3000)
13− [f] -> (f, 3000)
14− [f, p] -> (f, read p)
15− _ -> error "Usage: warp-static [root [port]]"
1623 run port $ staticApp StaticSettings
17− { ssFolder = folder
18− , ssIndices = ["index.html", "index.htm"]
24+ { ssFolder = docroot
25+ , ssIndices = if noindex then [] else index
1926 , ssListing = Just defaultListing
2027 , ssGetMimeType = return . defaultMimeTypeByExt
2128 }