@@ -19,10 +19,13 @@ module Main (
19
19
) where
20
20
21
21
import Web.Scotty
22
+ import qualified Web.Scotty as Scotty
22
23
import qualified Language.PureScript as P
23
24
24
25
import Data.Version (showVersion )
25
26
27
+ import Network.HTTP.Types (status500 )
28
+
26
29
import Data.Monoid
27
30
import Data.String
28
31
import Data.Maybe (mapMaybe )
@@ -43,6 +46,7 @@ import qualified Paths_trypurescript as Paths
43
46
44
47
import qualified Data.ByteString as B
45
48
import qualified Data.ByteString.UTF8 as BU
49
+ import qualified Data.ByteString.Lazy.UTF8 as BUL
46
50
47
51
import Data.FileEmbed
48
52
@@ -53,7 +57,7 @@ data Compiled = Compiled { js :: String
53
57
, externs :: String
54
58
}
55
59
56
- data Response = Response ( Either String Compiled )
60
+ newtype Response = Response { runResponse :: Either String Compiled }
57
61
58
62
options :: P. Options P. Compile
59
63
options = P. defaultCompileOptions { P. optionsAdditional = P. CompileOptions " PS" [" Main" ] [] }
@@ -140,7 +144,7 @@ page ex input res = html $ renderHtml $ do
140
144
let (success, text) = responseToJs res
141
145
142
146
H. h2 $ H. toHtml $ str " PureScript Code"
143
- H. form ! A. action " /compile" ! A. method " POST" $ do
147
+ H. form ! A. action " /compile/html " ! A. method " POST" $ do
144
148
H. div ! A. id " code" $ mempty
145
149
H. textarea ! A. name " code" ! A. id " textarea" ! A. style " display: none;" $ maybe mempty (H. toHtml . str) input
146
150
H. div $ H. button ! A. type_ " submit" $ H. toHtml $ str " Compile"
@@ -168,10 +172,19 @@ server port = do
168
172
Just (_, code) -> do
169
173
response <- lift $ compile preludeModules code
170
174
page (Just name) (Just code) (Just response)
171
- post " /compile" $ do
175
+ post " /compile/html " $ do
172
176
code <- param " code"
173
177
response <- lift $ compile preludeModules code
174
178
page Nothing (Just code) (Just response)
179
+ post " /compile/text" $ do
180
+ code <- BUL. toString <$> body
181
+ response <- lift $ compile preludeModules code
182
+ case runResponse response of
183
+ Left err -> do
184
+ status status500
185
+ Scotty. text . fromString $ err
186
+ Right comp ->
187
+ Scotty. text . fromString $ js comp
175
188
176
189
term :: Term (IO () )
177
190
term = server <$> port
0 commit comments