git.iko.soy/lynxjs-elm/compiler
on master help sign in
Add compile/v2 returning errors in JSON
922073aTereza Sokol
worker/src/Endpoint/Compile.hs+48−16
@@ -1,7 +1,8 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}
2{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}
3module Endpoint.Compile
3module Endpoint.Compile
4− ( endpoint
4+ ( endpointV1
5+ , endpointV2
5 , loadErrorJS
6 , loadErrorJS
6 )
7 )
7 where
8 where
@@ -66,23 +67,49 @@ allowedOrigins =
66-- ENDPOINT
67-- ENDPOINT
67
68
68
69
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+
69−endpoint :: A.Artifacts -> Snap ()
86+endpointV2 :: A.Artifacts -> Snap ()
87+endpointV2 artifacts =
70−endpoint 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 =
71 Cors.allow POST allowedOrigins $
108 Cors.allow POST allowedOrigins $
72 do result <- foldMultipart defaultUploadPolicy ignoreFile 0
109 do result <- foldMultipart defaultUploadPolicy ignoreFile 0
73 case result of
110 case result of
74 ([("code",source)], 0) ->
111 ([("code",source)], 0) ->
75− do modifyResponse $ setContentType "text/html; charset=utf-8"
76− case compile artifacts source of
112+ respondToOutcome source (compile artifacts source)
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) []
86
113
87 _ ->
114 _ ->
88 do modifyResponse $ setResponseStatus 400 "Bad Request"
115 do modifyResponse $ setResponseStatus 400 "Bad Request"
@@ -166,8 +193,13 @@ checkImports interfaces imports =
166-- RENDER REPORT
193-- RENDER REPORT
167
194
168
195
169−renderReport :: Help.Report -> B.Builder
196+renderJsonReport :: Help.Report -> B.Builder
170−renderReport report =
197+renderJsonReport report =
198+ Encode.encodeUgly (Exit.toJson report)
199+
200+
201+renderHtmlReport :: Help.Report -> B.Builder
202+renderHtmlReport report =
171 [r|<!DOCTYPE HTML>
203 [r|<!DOCTYPE HTML>
172<html>
204<html>
173<head>
205<head>
worker/src/Main.hs+4−3
@@ -30,16 +30,17 @@ main =
30 do rArtifacts <- Artifacts.loadRepl
30 do rArtifacts <- Artifacts.loadRepl
31 cArtifacts <- Artifacts.loadCompile
31 cArtifacts <- Artifacts.loadCompile
32 errorJS <- Compile.loadErrorJS
32 errorJS <- Compile.loadErrorJS
33− manager <- Donate.getManager =<< getSecret
33+ -- manager <- Donate.getManager =<< getSecret
34 let depsInfo = Artifacts.toDepsInfo cArtifacts
34 let depsInfo = Artifacts.toDepsInfo cArtifacts
35
35
36 httpServe config $ msum $
36 httpServe config $ msum $
37 [ ifTop $ status
37 [ ifTop $ status
38 , path "repl" $ Repl.endpoint rArtifacts
38 , path "repl" $ Repl.endpoint rArtifacts
39− , path "compile" $ Compile.endpoint cArtifacts
39+ , path "compile" $ Compile.endpointV1 cArtifacts
40+ , path "compile/v2" $ Compile.endpointV2 cArtifacts
40 , path "compile/errors.js" $ serveJavaScript errorJS
41 , path "compile/errors.js" $ serveJavaScript errorJS
41 , path "compile/deps-info.json" $ serveDepsInfo depsInfo
42 , path "compile/deps-info.json" $ serveDepsInfo depsInfo
42− , path "donate" $ Donate.endpoint manager
43+ -- , path "donate" $ Donate.endpoint manager
43 , notFound
44 , notFound
44 ]
45 ]
45
46