-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.hs
49 lines (43 loc) · 1.52 KB
/
index.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.HTTP.Types
import Network.Wai.Application.Static (staticApp, defaultWebAppSettings)
import Network.Wai.Handler.Warp (run)
import Data.Conduit (($$))
import Data.Conduit.Binary (sinkHandle)
import System.IO (withFile, IOMode(WriteMode))
import System.Environment (getArgs)
import System.Process (runProcess, getProcessExitCode)
import Control.Applicative ((<$>))
import Data.Foldable (traverse_)
import Data.Text (unpack)
import Data.ByteString.Lazy.Char8 (pack)
import IdrisClient (runClient)
static :: Application
static = staticApp $ defaultWebAppSettings "."
application :: Application
application req = case ((parseMethod . requestMethod) req, pathInfo req) of
(Right GET, []) -> static req { pathInfo = ["index.html"] }
(Right POST, ["sources", name]) -> do
withFile ("sources/" ++ unpack name) WriteMode $ \h->
requestBody req $$ sinkHandle h
let report = runClient (":l " ++ unpack name)
responseLBS
status200
[("Content-Type", "text/plain")] .
pack <$> report
_ -> static req
runServer :: Int -> String -> IO ()
runServer port sourcePath = do
repl <- runProcess "idris" [] (Just sourcePath) Nothing Nothing Nothing Nothing
exitCode <- getProcessExitCode repl
--maybe (return ()) print exitCode
traverse_ print exitCode
run port application
main' :: [String] -> IO ()
main' [] = runServer 3000 "sources"
main' [port, sourcePath] = runServer (read port) sourcePath
main :: IO ()
main = do
args <- getArgs
main' args