-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathMain.hs
More file actions
292 lines (215 loc) · 7.4 KB
/
Main.hs
File metadata and controls
292 lines (215 loc) · 7.4 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import qualified Data.ByteString as BS
import Data.Foldable (asum)
import qualified Data.Map as Map
import Data.Word (Word8)
import GHC.Conc
import qualified Snap.Core as S
import qualified Snap.Http.Server as S
import Snap.Util.FileServe (serveFile)
import System.Console.CmdArgs
import qualified Elm.ModuleName as ModuleName
import qualified Elm.Package as Pkg
import qualified Elm.Version as V
import qualified Json.Encode as E
import qualified Parse.Primitives as P
import qualified Artifacts
import qualified GitHub
import qualified Gzip
import qualified Legacy
import qualified Memory
import qualified Memory.History as History
import qualified Package.Path as Path
import qualified Package.Register as Register
import qualified Server.Router as Router
import Server.Router (Route, top, s, int, bytes, (</>), (==>))
import qualified ServeFile
-- FLAGS
data Flags =
Flags
{ port :: Int
, github :: String
}
deriving (Data,Typeable,Show,Eq)
flags :: Flags
flags =
Flags
{ port = 8000
&= help "set the port of the server"
, github = ""
&= help "OAuth token for talking to GitHub"
}
-- MAIN
main :: IO ()
main =
do setNumCapabilities =<< getNumProcessors
cargs <- cmdArgs flags
artifacts <- Artifacts.init
memory <- Memory.init
token <- GitHub.init (github cargs)
putStrLn $ "Serving at http://localhost:" ++ show (port cargs)
S.httpServe (config (port cargs)) (serve artifacts token memory)
config :: Int -> S.Config S.Snap a
config port =
S.defaultConfig
# S.setVerbose False
# S.setPort port
# S.setAccessLog S.ConfigNoLog
# S.setErrorLog S.ConfigNoLog
(#) :: a -> (a -> b) -> b
(#) value func =
func value
-- SERVE
serve :: Artifacts.Artifacts -> GitHub.Token -> Memory.Memory -> S.Snap ()
serve artifacts token memory =
asum
[
-- LEGACY USERS (<0.19)
Legacy.getElmVersion >>= Legacy.serve
,
-- NORMAL ROUTES
Router.serve $ Router.oneOf $
[ top ==> ServeFile.misc artifacts "Elm Packages"
, s "packages" ==> S.redirect' "/" 301
, s "packages" </> bytes </> bytes </> packageRoute ==> servePackageInfo artifacts memory
, s "search.json" ==> Gzip.serveFile "application/json" "search.json.gz"
, s "all-packages" ==> serveFile "all-packages.json"
, s "all-packages" </> s "since" </> int ==> serveNewPackages memory
, s "register" ==> Register.register token memory
, s "artifacts" </> bytes ==> Artifacts.serve artifacts
, s "assets" </> s "fonts.css" ==> serveFonts
, s "help" </>
Router.oneOf
[ s "design-guidelines" ==> ServeFile.misc artifacts "Design Guidelines"
, s "documentation-format" ==> ServeFile.misc artifacts "Documentation Format"
]
]
,
-- NOT FOUND
do S.modifyResponse $ S.setResponseStatus 404 "Not Found"
method <- S.getsRequest S.rqMethod
if method == S.GET
then ServeFile.misc artifacts "Not Found"
else S.writeBuilder "Not Found"
]
-- NEW PACKAGES
serveNewPackages :: Memory.Memory -> Int -> S.Snap ()
serveNewPackages memory index =
do history <- Memory.getHistory memory
S.writeBuilder $ E.encodeUgly $ E.list History.encodeEvent $
History.since index history
-- PACKAGE ROUTE
data PackageRoute
= PkgOverview
| Pkg__releases_json
| PkgVersion (Maybe V.Version) VsnRoute
packageRoute :: Route (PackageRoute -> a) a
packageRoute =
Router.oneOf
[ top ==> PkgOverview
, s "releases.json" ==> Pkg__releases_json
, version </> vsnRoute ==> PkgVersion
]
version :: Route (Maybe V.Version -> a) a
version =
Router.oneOf
[ s "latest" ==> Nothing
, Router.custom toVersion ==> Just
]
toVersion :: BS.ByteString -> Maybe V.Version
toVersion bytes =
case P.fromByteString V.parser (,) bytes of
Right vsn -> Just vsn
Left _ -> Nothing
-- VERSION ROUTE
data VsnRoute
= VsnOverview
| VsnAbout
| VsnModule ModuleName.Raw
| Vsn__elm_json
| Vsn__docs_json
| Vsn__README_md
| Vsn__endpoint_json
vsnRoute :: Route (VsnRoute -> a) a
vsnRoute =
Router.oneOf
[ top ==> VsnOverview
, s "about" ==> VsnAbout
, s "elm.json" ==> Vsn__elm_json
, s "docs.json" ==> Vsn__docs_json
, s "README.md" ==> Vsn__README_md
, s "endpoint.json" ==> Vsn__endpoint_json
, Router.custom toModuleName ==> VsnModule
]
toModuleName :: BS.ByteString -> Maybe ModuleName.Raw
toModuleName bytes =
case P.fromByteString ModuleName.parser (,) (BS.map toDot bytes) of
Right name -> Just name
Left _ -> Nothing
toDot :: Word8 -> Word8
toDot word =
if word == 0x2D {---}
then 0x2E {-.-}
else word
-- SERVE PACKAGE INFO
servePackageInfo :: Artifacts.Artifacts -> Memory.Memory -> BS.ByteString -> BS.ByteString -> PackageRoute -> S.Snap ()
servePackageInfo artifacts memory author project pkgRoute =
case P.fromByteString Pkg.parser (,) (BS.concat [author,"/",project]) of
Left _ ->
S.pass
Right pkg ->
case pkgRoute of
PkgOverview ->
do pkgs <- Memory.getPackages memory
if Map.member pkg pkgs
then ServeFile.project artifacts pkg
else S.pass
Pkg__releases_json ->
serveFile (Path.releases pkg)
PkgVersion maybeVersion vsnRoute ->
serveVersion artifacts memory pkg maybeVersion vsnRoute
serveVersion :: Artifacts.Artifacts -> Memory.Memory -> Pkg.Name -> Maybe V.Version -> VsnRoute -> S.Snap ()
serveVersion artifacts memory pkg maybeVersion vsnRoute =
do pkgs <- Memory.getPackages memory
case Map.lookup pkg pkgs of
Nothing ->
S.pass
Just (Memory.Summary versions _ _) ->
case verifyVersion maybeVersion versions of
Nothing ->
S.pass
Just vsn ->
case vsnRoute of
VsnOverview -> ServeFile.version artifacts pkg vsn Nothing
VsnAbout -> ServeFile.version artifacts pkg vsn Nothing
Vsn__elm_json -> Gzip.serveFile "application/json" (Path.directory pkg vsn ++ "/elm.json.gz")
Vsn__docs_json -> Gzip.serveFile "application/json" (Path.directory pkg vsn ++ "/docs.json.gz")
Vsn__README_md -> Gzip.serveFile "text/markdown; charset=UTF-8" (Path.directory pkg vsn ++ "/README.md.gz")
Vsn__endpoint_json -> serveFile (Path.directory pkg vsn ++ "/endpoint.json")
VsnModule name -> ServeFile.version artifacts pkg vsn (Just name)
verifyVersion :: Maybe V.Version -> [V.Version] -> Maybe V.Version
verifyVersion maybeVersion versions =
case versions of
[] ->
Nothing
_:_ ->
case maybeVersion of
Nothing ->
Just (maximum versions)
Just version ->
if elem version versions
then Just version
else Nothing
-- SERVE FONTS
serveFonts :: S.Snap ()
serveFonts =
do maybeHeader <- S.getsRequest (S.getHeader "User-Agent")
case maybeHeader of
Nothing ->
return ()
Just userAgent ->
if BS.isInfixOf "Macintosh" userAgent
then Gzip.serveFile "text/css" "assets/fonts/_hints_off.css.gz"
else Gzip.serveFile "text/css" "assets/fonts/_hints_on.css.gz"