-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_server.elm
84 lines (62 loc) · 1.58 KB
/
web_server.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
module WebServer where
--import Behaviour exposing (Action, init, update, input)
import Html exposing (text)
main = text "placeholder" -- hack
------------------------------
------------------------------
-- APP BEHAVIOUR --
type alias State =
{ count : Int}
type Action =
Increment | Decrement
init: State
init = {count = 0}
update: Action -> State -> State
update action state =
case action of
Increment -> {state | count <- state.count + 1 }
Decrement -> {state | count <- state.count - 1 }
input: String -> Action
input action =
case action of
"decrement" -> Decrement
_ -> Increment
view: State -> String
view state =
"The current count is: " ++ toString state.count
-- END APP
------------------------------
------------------------------
------------------------------
-- Web stuff --
------------------------------
type alias Request =
{ id: Int
, url: String
, body: String
, action: String
}
type alias Response =
{ id: Int
, url: String
, body: String
}
------------------------------
interpret: Request -> Action
interpret request =
input request.action
respond: Request -> State -> Response
respond request model =
{ id = request.id
, url= request.url
, body= view model
}
-- all inputs come as requests
port messageIn : Signal Request
-- all outputs leave as responses.
port messageOut : Signal Response
port messageOut =
let inputs = messageIn
actions = Signal.map interpret inputs
in
Signal.map2 respond inputs (Signal.foldp update init actions)