elm/elm-server/Server.hs

64 lines
1.8 KiB
Haskell
Raw Normal View History

2012-04-19 06:32:10 +00:00
module Main where
import Control.Monad (msum,guard)
import Control.Monad.Trans (MonadIO(liftIO))
import Data.List (isPrefixOf, isSuffixOf)
import Happstack.Server
2012-04-19 06:32:10 +00:00
import Happstack.Server.Compression
import System.Environment
import qualified Language.Elm as Elm
2012-04-19 06:32:10 +00:00
serve :: String -> IO ()
serve libLoc = do
2012-09-14 04:42:34 +00:00
putStrLn "Elm Server 0.4.0: running at <http://localhost:8000>"
2012-04-19 06:32:10 +00:00
simpleHTTP nullConf $ do
_ <- compressedResponseFilter
2012-04-19 06:32:10 +00:00
msum [ uriRest (serveElm libLoc)
, serveDirectory EnableBrowsing [] "."
]
pageTitle :: String -> String
pageTitle fp =
reverse . takeWhile (/='/') . drop 1 . dropWhile (/='.') $ reverse fp
2012-04-19 06:32:10 +00:00
serveElm libLoc fp = do
let ('/':filePath) = fp
guard (".elm" `isSuffixOf` filePath)
content <- liftIO (readFile filePath)
ok . toResponse $ Elm.toHtml libLoc (pageTitle filePath) content
2012-04-19 06:32:10 +00:00
main :: IO ()
2012-04-19 06:32:10 +00:00
main = getArgs >>= parse
parse :: [String] -> IO ()
2012-04-19 06:32:10 +00:00
parse ("--help":_) = putStrLn usage
parse ("--version":_) = putStrLn "The Elm Server 0.3.5"
2012-04-19 06:32:10 +00:00
parse [] = serve "/elm-mini.js"
parse [arg]
| "--runtime-location=" `isPrefixOf` arg =
serve . tail $ dropWhile (/='=') arg
| otherwise = putStrLn usageMini
parse _ = putStrLn usageMini
usageMini :: String
2012-04-19 06:32:10 +00:00
usageMini =
"Usage: elm-server [OPTIONS]\n\
\Try `elm-server --help' for more information."
usage :: String
2012-04-19 06:32:10 +00:00
usage =
"Usage: elm-server [OPTIONS]\n\
\Compiles and serves .elm files from the current directory.\n\
\Example: elm-server\n\
\\n\
\Resource Locations:\n\
\ --runtime-location set the location of the Elm runtime (elm-mini.js)\n\
\\n\
\Compiler Information:\n\
\ --version print the version information and exit\n\
\ --help display this help and exit\n\
\\n\
\Elm home page: <http://elm-lang.org>"