This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.hs
53 lines (48 loc) · 1.76 KB
/
main.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
50
51
52
53
module Main where
import Control.Monad
import Happstack.Server
import Control.Monad.Trans (MonadIO(liftIO))
import System.Cmd
import System.IO
import System.IO.Temp
import System.FilePath
myPolicy :: BodyPolicy
myPolicy = defaultBodyPolicy "/tmp" 1024000 1024000 1024000
handler :: ServerPart Response
handler =
do
decodeBody myPolicy
method POST
msum [handlerWithAdditions, handlerSimple]
handlerWithAdditions :: ServerPart Response
handlerWithAdditions =
do
addition <- look "addition"
file <- look "file"
(ecode,str) <- liftIO $ withSystemTempDirectory "hinterpw." $ \td -> do
outf <- openFile (combine td "file.hs") WriteMode
outa <- openFile (combine td "add.tar.gz") WriteMode
hPutStr outf file
hPutStr outa addition
hFlush outf
hFlush outa
ecode' <- system $ "cd " ++ td ++ "&& ((base64 -d < add.tar.gz | tar zx) && ghc --make file.hs >/dev/null && ./file) > temporary 2>&1"
str' <- readFile $ combine td "temporary"
return (ecode', str')
ok $ (toResponse $ show ecode ++ "\n" ++ str) {rsHeaders = mkHeaders [("Access-Control-Allow-Origin", "*")]}
handlerSimple :: ServerPart Response
handlerSimple =
do
file <- look "file"
(ecode,str) <- liftIO $ withSystemTempDirectory "hinterpw." $ \td -> do
outf <- openFile (combine td "file.hs") WriteMode
hPutStr outf file
hFlush outf
ecode' <- system $ "cd " ++ td ++ "&& (ghc --make file.hs >/dev/null && ./file) > temporary 2>&1"
str' <- readFile $ combine td "temporary"
return (ecode', str')
ok $ (toResponse $ show ecode ++ "\n" ++ str) {rsHeaders = mkHeaders [("Access-Control-Allow-Origin", "*")]}
main :: IO ()
main =
let config = nullConf in
simpleHTTP nullConf $ handler