git.iko.soy/rowtype-yoga/psfp
on cf0f24c help sign in
39 lines 1.3 kB history raw
1module Playground.Handler where
3import Prelude
5import Data.Maybe (Maybe)
6import Effect.Aff.Class (liftAff)
7import Effect.Class (class MonadEffect, liftEffect)
8import Node.Buffer (Buffer)
9import Node.Buffer as Buffer
10import Node.Encoding (Encoding(..))
11import Node.Express.Handler (HandlerM)
12import Node.Express.Request (getBody')
13import Node.Express.Response as Response
14import Playground.Playground (asErrorWithCode, compileCode, runCode)
15import Shared.Json (readAff)
16import Shared.Models.Body (RunResult)
17import Shared.Models.Body as Body
18import Simple.JSON (write)
20toBody ∷ ∀ r m. MonadEffect m => { stdout ∷ Buffer, stderr ∷ Buffer | r } -> m RunResult
21toBody result = liftEffect $ ado
22 stdout <- Buffer.toString UTF8 result.stdout
23 stderr <- Buffer.toString UTF8 result.stderr
24 let (code ∷ Maybe Int) = asErrorWithCode result >>= _.code
25 in { code, stdout, stderr } ∷ RunResult
27compileHandler ∷ HandlerM Unit
28compileHandler = do
29 body <- getBody'
30 json <- readAff body # liftAff
31 result <- compileCode (json ∷ Body.CompileRequest).code # liftAff
32 Response.send $ write ({ result } ∷ Body.CompileResult)
34runHandler ∷ HandlerM Unit
35runHandler = do
36 result <- liftAff do
37 result <- runCode
38 toBody result
39 Response.send $ write (result ∷ Body.RunResult)