-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.gren
More file actions
167 lines (144 loc) · 5.45 KB
/
Copy pathMain.gren
File metadata and controls
167 lines (144 loc) · 5.45 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
module Main exposing (main)
import Node
import Bytes exposing (Bytes)
import Dict
import Stream
import Node exposing (Environment, Program)
import FileSystem
import FileSystem.Path as Path
import HttpServer as Http exposing (ServerError(..), Method(..))
import HttpServer.Response as Response exposing (Response)
import Init
import Json.Decode as Decode
import Task exposing (Task)
main : Program Model Msg
main =
Node.defineProgram
{ init = init
, update = update
, subscriptions = subscriptions
}
type alias Model =
{ stdout : Stream.Writable Bytes
, stderr : Stream.Writable Bytes
, server : Maybe Http.Server
, fsPermission : FileSystem.Permission
}
type Msg
= CreateServerResult (Result Http.ServerError Http.Server)
| GotRequest { request : Http.Request, response : Response }
| GotJpeg { response : Response, result : (Result FileSystem.Error Bytes) }
init : Environment -> Init.Task { model : Model, command : Cmd Msg }
init env =
Init.await FileSystem.initialize <| \fileSystemPermission ->
Init.await Http.initialize <| \serverPermission ->
Node.startProgram
{ model =
{ stdout = env.stdout
, stderr = env.stderr
, server = Nothing
, fsPermission = fileSystemPermission
}
, command =
Task.attempt CreateServerResult <|
Http.createServer serverPermission
{ host = "0.0.0.0"
, port_ = 3000
}
}
update : Msg -> Model -> { model : Model, command : Cmd Msg }
update msg model =
when msg is
CreateServerResult result ->
when result is
Ok server ->
{ model = { model | server = Just server }
, command =
Stream.writeLineAsBytes "Server started" model.stdout
|> Task.map (\_ -> {})
|> Task.onError (\_ -> Task.succeed {})
|> Task.execute
}
Err (ServerError { code, message }) ->
{ model = model
, command =
Stream.writeLineAsBytes ("Server failed to start: " ++ code ++ "\n" ++ message) model.stderr
|> Task.map (\_ -> {})
|> Task.onError (\_ -> Task.succeed {})
|> Task.execute
}
GotRequest { request = req, response = res } ->
{ model = model
, command =
if req.url.path == "/george.jpeg" then
FileSystem.readFile model.fsPermission (Path.fromPosixString "./public/george.jpeg")
|> Task.attempt (\fileOp -> GotJpeg { response = res, result = fileOp })
else
htmlResponse req res
}
GotJpeg { response, result = (Ok bytes) } ->
{ model = model
, command =
response
|> Response.setHeader "Content-Type" "image/jpeg"
|> Response.setStatus 200
|> Response.setBodyAsBytes bytes
|> Response.send
}
GotJpeg { response, result = (Err _) } ->
{ model = model
, command =
response
|> Response.setHeader "Content-Type" "text/plain"
|> Response.setStatus 404
|> Response.setBody "Not found"
|> Response.send
}
htmlResponse : Http.Request -> Response -> Cmd Msg
htmlResponse req res =
let
{ body, status } = when { method = req.method, path = req.url.path } is
{ method = GET, path = "/" } ->
{ body = "Welcome!"
, status = 200
}
{ method = GET, path = "/hello" } ->
{ body = "Hello to you too!"
, status = 200
}
{ method = POST, path = "/name" } ->
{ body = "Hello, " ++
( req
|> Http.bodyFromJson (Decode.dict Decode.string)
|> Result.withDefault Dict.empty
|> Dict.get "name"
|> Maybe.withDefault "Oops! Can't decode body."
)
, status = 200
}
{ method = POST } ->
{ body = "You posted: " ++
( req
|> Http.bodyAsString
|> Maybe.withDefault "Oops! Can't decode body."
)
, status = 200
}
_ ->
{ body = "Not found: " ++ (Http.requestInfo req)
, status = 404
}
in
res
|> Response.setStatus status
|> Response.setHeader "Content-type" "text/html"
|> Response.setHeader "X-Custom-Header" "hey there"
|> Response.setBody body
|> Response.send
subscriptions : Model -> Sub Msg
subscriptions model =
when model.server is
Just server ->
Http.onRequest server <| \req res -> GotRequest { request = req, response = res }
Nothing ->
Sub.none