on 44b2e45 help sign in
elk/src/Server.hs
58 lines 1.7 kB history raw
2module Main where
4import Control.Monad (msum,guard)
5import Control.Monad.Trans (MonadIO(liftIO))
6import Data.List (isPrefixOf, isSuffixOf)
7import Happstack.Server
8import Happstack.Server.Compression
9import System.Environment
10import Language.Elm
12serve :: String -> IO ()
13serve libLoc = do
14 putStrLn "Elm Server 0.1.1.6: running at <http://localhost:8000>"
15 simpleHTTP nullConf $ do
16 compressedResponseFilter
17 msum [ uriRest (serveElm libLoc)
18 , serveDirectory EnableBrowsing [] "."
19 ]
21pageTitle fp =
22 reverse . takeWhile (/='/') . drop 1 . dropWhile (/='.') $ reverse fp
24serveElm libLoc fp = do
25 let ('/':path) = fp
26 guard (".elm" `isSuffixOf` path)
27 content <- liftIO (readFile path)
28 ok . toResponse $ compileToHtml libLoc (pageTitle path) content
31main = getArgs >>= parse
33parse ("--help":_) = putStrLn usage
34parse ("--version":_) = putStrLn "The Elm Server 0.1.1.6"
35parse [] = serve "/elm-mini.js"
36parse [arg]
37 | "--runtime-location=" `isPrefixOf` arg =
38 serve . tail $ dropWhile (/='=') arg
39 | otherwise = putStrLn usageMini
40parse _ = putStrLn usageMini
42usageMini =
43 "Usage: elm-server [OPTIONS]\n\
44 \Try `elm-server --help' for more information."
46usage =
47 "Usage: elm-server [OPTIONS]\n\
48 \Compiles and serves .elm files from the current directory.\n\
49 \Example: elm-server\n\
50 \\n\
51 \Resource Locations:\n\
52 \ --runtime-location set the location of the Elm runtime (elm-mini.js)\n\
53 \\n\
54 \Compiler Information:\n\
55 \ --version print the version information and exit\n\
56 \ --help display this help and exit\n\
57 \\n\
58 \Elm home page: <http://elm-lang.org>"