git.iko.soy/lynxjs-elm/compiler
on master help sign in
Add compile/v2 returning errors in JSON

`/compile` still returns the old format. `/compile/v2` returns the same for successful compiles, but returns compiling errors in a JSON format.

commit922073a798e8c7dd24360d83e935521a00b6a57e authorTereza Sokol <terezasokol@gmail.com> dateMon 2 Aug 2021, 17:54 (5 years ago) parent94715a5
2 files changed+52−19
worker/src/Endpoint/Compile.hs+48−16 worker/src/Main.hs+4−3
worker/src/Endpoint/Compile.hs+48−16
@@ -1,7 +1,8 @@
11{-# OPTIONS_GHC -Wall #-}
22{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}
33module Endpoint.Compile
4− ( endpoint
4+ ( endpointV1
5+ , endpointV2
56 , loadErrorJS
67 )
78 where
@@ -66,23 +67,49 @@ allowedOrigins =
6667-- ENDPOINT
6768
6869
69−endpoint :: A.Artifacts -> Snap ()
70−endpoint artifacts =
70+endpointV1 :: A.Artifacts -> Snap ()
71+endpointV1 artifacts =
72+ endpoint artifacts $ \source outcome ->
73+ do modifyResponse $ setContentType "text/html; charset=utf-8"
74+ case outcome of
75+ Success builder ->
76+ writeBuilder builder
77+
78+ NoMain ->
79+ writeBuilder $ renderHtmlReport noMain
80+
81+ BadInput name err ->
82+ writeBuilder $ renderHtmlReport $
83+ Help.compilerReport "/" (Error.Module name "/try" File.zeroTime source err) []
84+
85+
86+endpointV2 :: A.Artifacts -> Snap ()
87+endpointV2 artifacts =
88+ endpoint artifacts $ \source outcome ->
89+ case outcome of
90+ Success builder ->
91+ do modifyResponse $ setContentType "text/html; charset=utf-8"
92+ writeBuilder builder
93+
94+ NoMain ->
95+ do modifyResponse $ setResponseStatus 400 "Bad Request"
96+ modifyResponse $ setContentType "text/json; charset=utf-8"
97+ writeBuilder $ renderJsonReport noMain
98+
99+ BadInput name err ->
100+ do modifyResponse $ setResponseStatus 400 "Bad Request"
101+ modifyResponse $ setContentType "text/json; charset=utf-8"
102+ writeBuilder $ renderJsonReport $
103+ Help.compilerReport "/" (Error.Module name "/try" File.zeroTime source err) []
104+
105+
106+endpoint :: A.Artifacts -> (B.ByteString -> Outcome -> Snap ()) -> Snap ()
107+endpoint artifacts respondToOutcome =
71108 Cors.allow POST allowedOrigins $
72109 do result <- foldMultipart defaultUploadPolicy ignoreFile 0
73110 case result of
74111 ([("code",source)], 0) ->
75− do modifyResponse $ setContentType "text/html; charset=utf-8"
76− case compile artifacts source of
77− Success builder ->
78− writeBuilder builder
79−
80− NoMain ->
81− writeBuilder $ renderReport noMain
82−
83− BadInput name err ->
84− writeBuilder $ renderReport $
85− Help.compilerReport "/" (Error.Module name "/try" File.zeroTime source err) []
112+ respondToOutcome source (compile artifacts source)
86113
87114 _ ->
88115 do modifyResponse $ setResponseStatus 400 "Bad Request"
@@ -166,8 +193,13 @@ checkImports interfaces imports =
166193-- RENDER REPORT
167194
168195
169−renderReport :: Help.Report -> B.Builder
170−renderReport report =
196+renderJsonReport :: Help.Report -> B.Builder
197+renderJsonReport report =
198+ Encode.encodeUgly (Exit.toJson report)
199+
200+
201+renderHtmlReport :: Help.Report -> B.Builder
202+renderHtmlReport report =
171203 [r|<!DOCTYPE HTML>
172204<html>
173205<head>
worker/src/Main.hs+4−3
@@ -30,16 +30,17 @@ main =
3030 do rArtifacts <- Artifacts.loadRepl
3131 cArtifacts <- Artifacts.loadCompile
3232 errorJS <- Compile.loadErrorJS
33− manager <- Donate.getManager =<< getSecret
33+ -- manager <- Donate.getManager =<< getSecret
3434 let depsInfo = Artifacts.toDepsInfo cArtifacts
3535
3636 httpServe config $ msum $
3737 [ ifTop $ status
3838 , path "repl" $ Repl.endpoint rArtifacts
39− , path "compile" $ Compile.endpoint cArtifacts
39+ , path "compile" $ Compile.endpointV1 cArtifacts
40+ , path "compile/v2" $ Compile.endpointV2 cArtifacts
4041 , path "compile/errors.js" $ serveJavaScript errorJS
4142 , path "compile/deps-info.json" $ serveDepsInfo depsInfo
42− , path "donate" $ Donate.endpoint manager
43+ -- , path "donate" $ Donate.endpoint manager
4344 , notFound
4445 ]
4546