Skip to content

Commit 592de76

Browse files
committed
Add compile/text service
1 parent 14176f0 commit 592de76

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

Main.hs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ module Main (
1919
) where
2020

2121
import Web.Scotty
22+
import qualified Web.Scotty as Scotty
2223
import qualified Language.PureScript as P
2324

2425
import Data.Version (showVersion)
2526

27+
import Network.HTTP.Types (status500)
28+
2629
import Data.Monoid
2730
import Data.String
2831
import Data.Maybe (mapMaybe)
@@ -43,6 +46,7 @@ import qualified Paths_trypurescript as Paths
4346

4447
import qualified Data.ByteString as B
4548
import qualified Data.ByteString.UTF8 as BU
49+
import qualified Data.ByteString.Lazy.UTF8 as BUL
4650

4751
import 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

5862
options :: P.Options P.Compile
5963
options = 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

176189
term :: Term (IO ())
177190
term = server <$> port

trypurescript.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ executable trypurescript
1717
bytestring >=0.10.0.2 && <0.11, base ==4.*, scotty -any,
1818
purescript ==0.5.7.1, containers -any, mtl -any, blaze-html -any,
1919
cmdtheline -any, monad-unify >=0.2.1 && <0.3, utf8-string -any,
20-
file-embed >=0.0.6
20+
http-types >= 0.8.5, file-embed >=0.0.6
2121
main-is: Main.hs
2222
buildable: True
2323
other-modules: Main

0 commit comments

Comments
 (0)