2012-05-20 05:14:54 +00:00
|
|
|
|
2012-04-19 06:32:10 +00:00
|
|
|
module Main where
|
|
|
|
|
|
|
|
import Control.Monad (msum,guard)
|
|
|
|
import Control.Monad.Trans (MonadIO(liftIO))
|
2013-06-04 09:37:20 +00:00
|
|
|
import Data.List (isPrefixOf, isSuffixOf, (\\))
|
2012-10-03 07:17:09 +00:00
|
|
|
import Data.Version (showVersion)
|
2012-05-20 05:14:54 +00:00
|
|
|
import Happstack.Server
|
2012-04-19 06:32:10 +00:00
|
|
|
import Happstack.Server.Compression
|
2013-08-10 07:51:29 +00:00
|
|
|
import System.Directory
|
2012-04-19 06:32:10 +00:00
|
|
|
import System.Environment
|
2012-12-08 20:53:16 +00:00
|
|
|
import System.FilePath
|
2013-08-10 07:51:29 +00:00
|
|
|
import System.Process
|
2012-05-29 18:25:43 +00:00
|
|
|
import qualified Language.Elm as Elm
|
2012-10-03 06:47:46 +00:00
|
|
|
import Paths_elm_server
|
|
|
|
|
2013-08-10 07:51:29 +00:00
|
|
|
runtime = "/elm-runtime.js"
|
2012-04-19 06:32:10 +00:00
|
|
|
|
2013-06-04 09:37:20 +00:00
|
|
|
serve :: Int -> String -> IO ()
|
|
|
|
serve portNumber libLoc = do
|
2013-08-10 07:51:29 +00:00
|
|
|
putStrLn $ "Elm Server " ++ showVersion version ++
|
|
|
|
": running at <http://localhost:" ++ show portNumber ++ ">"
|
2012-12-13 06:52:32 +00:00
|
|
|
putStrLn "Just refresh a page to recompile it!"
|
2013-06-04 09:37:20 +00:00
|
|
|
simpleHTTP httpConf $ do
|
2012-05-30 22:01:54 +00:00
|
|
|
_ <- compressedResponseFilter
|
2012-10-02 19:59:06 +00:00
|
|
|
msum [ uriRest serveElm
|
|
|
|
, uriRest (serveLib libLoc)
|
2012-04-19 06:32:10 +00:00
|
|
|
, serveDirectory EnableBrowsing [] "."
|
|
|
|
]
|
2013-06-04 09:37:20 +00:00
|
|
|
where httpConf = nullConf { port = portNumber }
|
2012-04-19 06:32:10 +00:00
|
|
|
|
2012-05-30 22:01:54 +00:00
|
|
|
pageTitle :: String -> String
|
2012-12-08 20:53:16 +00:00
|
|
|
pageTitle = dropExtension . takeBaseName
|
2012-05-23 03:45:19 +00:00
|
|
|
|
2013-08-10 07:51:29 +00:00
|
|
|
serveElm :: FilePath -> ServerPartT IO Response
|
2012-10-02 19:59:06 +00:00
|
|
|
serveElm fp = do
|
2012-12-08 20:53:16 +00:00
|
|
|
guard (takeExtension fp == ".elm")
|
2013-08-10 07:51:29 +00:00
|
|
|
let file = tail fp
|
|
|
|
liftIO $ rawSystem "elm" [ "--make" ,"--runtime=" ++ runtime
|
|
|
|
, "--cache-dir=elm-server-cache", file ]
|
|
|
|
liftIO $ removeDirectoryRecursive "elm-server-cache"
|
|
|
|
serveFile (asContentType "text/html") ("build" </> replaceExtension file "html")
|
2012-04-19 06:32:10 +00:00
|
|
|
|
2013-08-10 07:51:29 +00:00
|
|
|
serveLib :: FilePath -> [Char] -> ServerPartT IO Response
|
2012-10-02 19:59:06 +00:00
|
|
|
serveLib libLoc fp = do
|
2012-12-08 20:53:16 +00:00
|
|
|
guard (fp == runtime)
|
2012-10-02 19:59:06 +00:00
|
|
|
serveFile (asContentType "application/javascript") libLoc
|
2012-04-19 06:32:10 +00:00
|
|
|
|
2012-05-30 22:01:54 +00:00
|
|
|
main :: IO ()
|
2012-04-19 06:32:10 +00:00
|
|
|
main = getArgs >>= parse
|
|
|
|
|
2012-05-30 22:01:54 +00:00
|
|
|
parse :: [String] -> IO ()
|
2012-04-19 06:32:10 +00:00
|
|
|
parse ("--help":_) = putStrLn usage
|
2012-10-03 07:17:09 +00:00
|
|
|
parse ("--version":_) = putStrLn ("The Elm Server " ++ showVersion version)
|
2013-08-10 07:51:29 +00:00
|
|
|
parse args =
|
|
|
|
case null remainingArgs of
|
|
|
|
True -> serve portNumber =<< elmRuntime
|
|
|
|
False -> putStrLn usageMini
|
|
|
|
where
|
|
|
|
runtimeArg = filter (isPrefixOf "--runtime-location=") args
|
|
|
|
portArg = filter (isPrefixOf "--port=") args
|
|
|
|
remainingArgs = (args \\ runtimeArg) \\ portArg
|
2013-06-04 09:37:20 +00:00
|
|
|
|
2013-08-10 07:51:29 +00:00
|
|
|
argValue arg = tail $ dropWhile (/= '=') (head arg)
|
|
|
|
portNumber = if null portArg then 8000 else read (argValue portArg) :: Int
|
|
|
|
elmRuntime = if null runtimeArg then Elm.runtime else return $ argValue runtimeArg
|
2012-04-19 06:32:10 +00:00
|
|
|
|
2012-05-30 22:01:54 +00:00
|
|
|
usageMini :: String
|
2012-04-19 06:32:10 +00:00
|
|
|
usageMini =
|
|
|
|
"Usage: elm-server [OPTIONS]\n\
|
|
|
|
\Try `elm-server --help' for more information."
|
|
|
|
|
2012-05-30 22:01:54 +00:00
|
|
|
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\
|
2013-06-04 09:37:20 +00:00
|
|
|
\Server configuration:\n\
|
|
|
|
\ --port set the port to listen on (default: 8000)\n\
|
|
|
|
\\n\
|
2012-04-19 06:32:10 +00:00
|
|
|
\Resource Locations:\n\
|
2012-10-03 06:47:46 +00:00
|
|
|
\ --runtime-location set the location of the Elm runtime\n\
|
2012-04-19 06:32:10 +00:00
|
|
|
\\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>"
|