1{-# LANGUAGE DeriveDataTypeable, RecordWildCards #-}
2-- | Command line version of wai-app-static, used for the warp-static server.
3module WaiAppStatic.CmdLine
8import Network.Wai (Middleware)
9import Network.Wai.Application.Static (staticApp, defaultFileServerSettings)
10import Network.Wai.Handler.Warp
11 ( runSettings, defaultSettings, setHost, setPort
13import Options.Applicative
14import Text.Printf (printf)
15import System.Directory (canonicalizePath)
16import Control.Monad (unless)
17import Network.Wai.Middleware.RequestLogger (logStdout)
18import Network.Wai.Middleware.Gzip
19import qualified Data.Map as Map
20import qualified Data.ByteString.Char8 as S8
21import Control.Arrow ((***))
22import Data.Text (pack)
23import Data.String (fromString)
24import Network.Mime (defaultMimeMap, mimeByExt, defaultMimeType)
25import WaiAppStatic.Types (ssIndices, toPiece, ssGetMimeType, fileName, fromPiece)
26import Data.Maybe (mapMaybe)
27import Control.Arrow (second)
28import Data.Monoid ((<>))
37 , mime :: [(String, String)]
41#if MIN_VERSION_optparse_applicative(0, 10, 0)
54 <> help "directory containing files to serve")
55 <*> (defIndex <$> many (strOption
59 <> help "index files to serve when a directory is required"
75 <*> many (toPair <$> strOption
79 <> help "extra file extension/mime type mappings"))
85 <> help "interface to bind to, special values: *, *4, *6")
87 toPair = second (drop 1) . break (== '=')
88 defIndex [] = ["index.html", "index.htm"]
91-- | Run with the given middleware and parsing options from the command line.
94runCommandLine :: (Args -> Middleware) -> IO ()
95runCommandLine middleware = do
96 args@Args {..} <- execParser $ info (helperOption <*> args) fullDesc
97 let mime' = map (pack *** S8.pack) mime
98 let mimeMap = Map.fromList mime' `Map.union` defaultMimeMap
99 docroot' <- canonicalizePath docroot
100 unless quiet $ printf "Serving directory %s on port %d with %s index files.\n" docroot' port (if noindex then "no" else show index)
101 let middle = gzip def { gzipFiles = GzipCompress }
102 . (if verbose then logStdout else id)
106 $ setHost (fromString host)
109 $ middle $ staticApp (defaultFileServerSettings $ fromString docroot)
110 { ssIndices = if noindex then [] else mapMaybe (toPiece . pack) index
111 , ssGetMimeType = return . mimeByExt mimeMap defaultMimeType . fromPiece . fileName
114 helperOption :: Parser (a -> a)
116 abortOption ShowHelpText $
117 mconcat [long "help", help "Show this help text", hidden]
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list