-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.elm
313 lines (235 loc) · 7.97 KB
/
Main.elm
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
port module Main exposing (..)
import Task exposing (Task, andThen, map, onError)
import Json.Encode as Json
import Json.Decode exposing (Decoder, decodeString, object2, list, int, string, (:=))
import String exposing (isEmpty, join, split)
import Html exposing (..)
import Html.App as Html
import Node.File as File
import Node.Console as Console
import Node.Http as Http
import Node.Url as Url
import Node.Process as Process
import Node.Path as Path
import Node.Stream as Stream
import Node.Encoding as Encoding
import List
import Dict
type alias Artist =
{ id : Int
, name : String
}
mimeTypes : Dict.Dict String String
mimeTypes =
Dict.fromList
[ ( ".txt", "text/plain" )
, ( ".html", "text/html" )
, ( ".jpeg", "image/jpeg" )
, ( ".png", "image/png" )
, ( ".svg", "image/svg+xml" )
, ( ".js", "text/javascript" )
, ( ".css", "text/css" )
, ( ".json", "application/json" )
]
mimetype : Path.FilePath -> String
mimetype filename =
let
ext =
Path.extname filename
in
Maybe.withDefault "text/plain" (Dict.get ext mimeTypes)
artistValue : Artist -> Json.Value
artistValue artist =
Json.object
[ ( "artistId", Json.int artist.id )
, ( "name", Json.string artist.name )
]
artistsValue : List Artist -> Json.Value
artistsValue artists =
artists
|> List.map artistValue
|> Json.list
encodeArtists : String
encodeArtists =
Json.encode 4
<| artistsValue
[ { id = 1
, name = "Radiohead"
}
, { id = 2
, name = "Massive Attack"
}
]
decodeArtists : String -> Result String (List Artist)
decodeArtists json =
decodeString artistsDecoder json
artistsDecoder : Decoder (List Artist)
artistsDecoder =
list
<| object2 Artist
("id" := int)
("name" := string)
readArtists : Task String String
readArtists =
File.read "data.json"
`withError` "Could not find the given data file: data.json"
staticPath : String -> String
staticPath filename =
Process.cwd ++ "/static/" ++ filename
readAndSendStatic : String -> Http.Response -> Task a ()
readAndSendStatic filename res =
Stream.createReadStream (staticPath filename)
`andThen` \readable ->
res
|> Http.setStatusCode 200
|> Http.setHeader "Content-Type" (mimetype filename)
|> Http.responseAsStream
|> Stream.pipe readable
redirectTo : String -> Http.Response -> Task a ()
redirectTo path res =
res
|> Http.setStatusCode 302
|> Http.setHeader "Location" path
|> Http.end
send : String -> String -> Http.Response -> Task a ()
send content contentType res =
res
|> Http.setHeader "Content-Type" contentType
|> Http.sendResponse content
sendJson : String -> Http.Response -> Task a ()
sendJson json res =
send json "application/json" res
sendHtml : String -> Http.Response -> Task a ()
sendHtml html res =
send html "text/html" res
handle : Http.Request -> Http.Response -> Task a ()
handle req res =
let
url =
req.url
search =
Maybe.withDefault "" url.search
query =
Url.getQuery url
emptyUrl =
Url.emptyUrl
urlStr =
Url.format
{ emptyUrl
| protocol = Just "http"
, hostname = Just "localhost"
, search = Just "?query=1234"
, port' = Just 100
}
pathList =
List.filter (not << isEmpty) (split "/" (url.pathname ? ""))
in
case pathList of
[] ->
tryCatch (emitError res)
<| readAndSendStatic "index.html" res
"static" :: file ->
tryCatch (emitError res)
<| readAndSendStatic (join "/" file) res
[ "send" ] ->
case req.method of
"POST" ->
(Http.requestAsStream req
|> Stream.setEncoding Encoding.UTF8
|> Stream.onData
)
`andThen` \_ -> sendHtml "<h1>send json</h1>" res
_ ->
emitErrorAsJson res ""
[ "load" ] ->
tryCatch (emitErrorAsJson res)
<| readArtists
`andThen` (\json ->
case (decodeArtists json) of
Ok val ->
Task.succeed (toString val)
Err err ->
Task.fail err
)
`andThen` \str -> sendHtml str res
"query" :: [] ->
--Console.log (Url.parseQueryString search)
-- `andThen` \_ ->
Http.sendResponse (toString <| Dict.get "x" query) res
[ "format" ] ->
Console.log urlStr
`andThen` \_ -> Http.sendResponse ("Format Url:" ++ urlStr) res
[ "hello" ] ->
-- (Maybe.withDefault "" (Dict.get "x" url.query))
Http.sendResponse "Hello World!" res
_ ->
Console.log
("Pathname:"
++ (Maybe.withDefault "" url.pathname)
++ "Host:"
++ (Maybe.withDefault "" url.host)
++ "Port:"
++ (toString (Maybe.withDefault -1 url.port'))
++ "Protocol:"
++ (Maybe.withDefault "" url.protocol)
)
`andThen` \_ -> sendJson encodeArtists res
emitError : Http.Response -> String -> Task x ()
emitError res message =
res
|> Http.setStatusCode 404
|> Http.setHeader "Content-Type" "text/plain"
|> Http.sendResponse message
emitErrorAsJson : Http.Response -> String -> Task x ()
emitErrorAsJson res message =
let
json =
[ Json.object [ ( "error", Json.string (message) ) ] ]
|> Json.list
|> Json.encode 0
in
Http.setStatusCode 404 res
|> sendJson json
withError : Task x a -> y -> Task y a
withError task error =
Task.mapError (\_ -> error) task
tryCatch : (x -> Task y a) -> Task x a -> Task y a
tryCatch =
flip Task.onError
(?) : Maybe a -> a -> a
(?) maybe default =
Maybe.withDefault default maybe
type alias Model =
{ httpPort : Int }
init : ( Model, Cmd Msg )
init =
( { httpPort = 8081 }, Cmd.none )
type Msg
= Started Bool
| Serve
| None
port dbg : String -> Cmd msg
update : Msg -> Model -> ( Model, Cmd Msg )
update action model =
case action of
Started val ->
( model
, Cmd.batch [ Task.perform (always None) (always (Serve)) (Http.serve model.httpPort handle) ]
)
Serve ->
( model, Task.perform (always None) (always None) (Console.log ("Serve at http://localhost:" ++ (toString model.httpPort))) )
None ->
( model, Cmd.none )
main : Program Never
main =
Html.program
{ init = init
, view = always <| div [] []
, update = update
, subscriptions = subscriptions
}
-- SUBSCRIPTIONS
port started : (Bool -> msg) -> Sub msg
subscriptions : Model -> Sub Msg
subscriptions model =
started Started