@@ -19,10 +19,13 @@ module Main (
1919) where
2020
2121import Web.Scotty
22+ import qualified Web.Scotty as Scotty
2223import qualified Language.PureScript as P
2324
2425import Data.Version (showVersion )
2526
27+ import Network.HTTP.Types (status500 )
28+
2629import Data.Monoid
2730import Data.String
2831import Data.Maybe (mapMaybe )
@@ -43,6 +46,7 @@ import qualified Paths_trypurescript as Paths
4346
4447import qualified Data.ByteString as B
4548import qualified Data.ByteString.UTF8 as BU
49+ import qualified Data.ByteString.Lazy.UTF8 as BUL
4650
4751import Data.FileEmbed
4852
@@ -53,7 +57,7 @@ data Compiled = Compiled { js :: String
5357 , externs :: String
5458 }
5559
56- data Response = Response ( Either String Compiled )
60+ newtype Response = Response { runResponse :: Either String Compiled }
5761
5862options :: P. Options P. Compile
5963options = P. defaultCompileOptions { P. optionsAdditional = P. CompileOptions " PS" [" Main" ] [] }
@@ -140,7 +144,7 @@ page ex input res = html $ renderHtml $ do
140144 let (success, text) = responseToJs res
141145
142146 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
144148 H. div ! A. id " code" $ mempty
145149 H. textarea ! A. name " code" ! A. id " textarea" ! A. style " display: none;" $ maybe mempty (H. toHtml . str) input
146150 H. div $ H. button ! A. type_ " submit" $ H. toHtml $ str " Compile"
@@ -168,10 +172,19 @@ server port = do
168172 Just (_, code) -> do
169173 response <- lift $ compile preludeModules code
170174 page (Just name) (Just code) (Just response)
171- post " /compile" $ do
175+ post " /compile/html " $ do
172176 code <- param " code"
173177 response <- lift $ compile preludeModules code
174178 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
175188
176189term :: Term (IO () )
177190term = server <$> port
0 commit comments