forked from elm/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
79 lines (59 loc) · 2.02 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE OverloadedStrings #-}
module Main
( main
)
where
import Control.Monad (msum)
import qualified Data.ByteString as BS
import Network.HTTP.Client.TLS (newTlsManager)
import Snap.Core
import Snap.Http.Server
import qualified System.Environment as Env
import qualified Artifacts
import qualified Cors
import qualified Endpoint.Compile as Compile
import qualified Endpoint.Quotes as Quotes
import qualified Endpoint.Repl as Repl
import qualified Endpoint.Slack as Slack
-- RUN THE DEV SERVER
main :: IO ()
main =
do manager <- newTlsManager
slackToken <- Env.getEnv "SLACK_TOKEN"
rArtifacts <- Artifacts.loadRepl
cArtifacts <- Artifacts.loadCompile
errorJS <- Compile.loadErrorJS
let depsInfo = Artifacts.toDepsInfo cArtifacts
httpServe config $ msum $
[ ifTop $ status
, path "repl" $ Repl.endpoint rArtifacts
, path "compile" $ Compile.endpoint_V1 cArtifacts
, path "compile/v2" $ Compile.endpoint_V2 cArtifacts
, path "compile/errors.js" $ serveJavaScript errorJS
, path "compile/deps-info.json" $ serveDepsInfo depsInfo
, path "quotes" $ Quotes.endpoint
, path "slack-invite" $ Slack.endpoint slackToken manager
, notFound
]
config :: Config Snap a
config =
setPort 8000 $ setAccessLog ConfigNoLog $ setErrorLog ConfigNoLog $ defaultConfig
status :: Snap ()
status =
do modifyResponse $ setContentType "text/plain"
writeBuilder "Status: OK"
notFound :: Snap ()
notFound =
do modifyResponse $ setResponseStatus 404 "Not Found"
modifyResponse $ setContentType "text/html; charset=utf-8"
writeBuilder "Not Found"
serveJavaScript :: BS.ByteString -> Snap ()
serveJavaScript javascript =
do modifyResponse $ setContentType "application/javascript"
writeBS javascript
serveDepsInfo :: BS.ByteString -> Snap ()
serveDepsInfo json =
Cors.allow GET ["https://elm-lang.org"] $
do modifyResponse $ setContentType "application/json"
writeBS json