-
Notifications
You must be signed in to change notification settings - Fork 0
/
shop.fs
75 lines (61 loc) · 2.45 KB
/
shop.fs
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
(* WebOnCont continuation passing style web framework *)
(* Web Shop example *)
(* @author Timur Abishev ([email protected]) *)
(* GPL license http://www.gnu.org/licenses/gpl.html *)
module mw.example.shop
open mw.httpcontinuation
open mw.server
(* templates *)
let hrefToCont key text =
"<a href='?key="+key+"'>"+text+"</a>"
let htmlCodeFromText text =
"<html><body>"+text+"</body></html>"
let authTemplate (key : string) =
htmlCodeFromText ("Hello World Auth! What is your name? <form><input type=\"hidden\" name=\"key\" value=\""+key+"\"/><input type = \"text\" name=\"name\" value=\"Name Here!\"/><input type=\"submit\"/></form>")
let mainTemplate (key : string) =
htmlCodeFromText ("It's online show! We are has: <a href='?id=1&key="+key+"'>book 1</a> and <a href='?id=2&key="+key+"'>book 2</a>")
let buyTemplate (id : string) (key : string) =
htmlCodeFromText ("Book "+id+" added to basket. Go to <a href='?action=basket&key="+key+"'>basket</a> or to <a href='?action=main&key="+key+"'>main</a>")
let basketTemplate (ids : string list) (key : string) =
htmlCodeFromText ("Your books: "+(sprintf "%A" ids)+". Go to <a href='?key="+key+"'>main</a>")
(* handlers *)
let mutable authNames = Map [("Session", "Name")]
let mutable books = Map [("Session", ["1";"2"])]
let auth = httpContinuation {
let! session = getSession
match authNames.ContainsKey session with
| true -> return authNames.[session]
| false ->
let! result = request authTemplate
let name = result.QueryString.["name"]
authNames <- authNames.Add (session, name);
books <- books.Add(session, [])
return name
}
let getIdsInBasket = httpContinuation {
let! session = getSession
return books.[session]
}
let addIdToBasket id = httpContinuation {
let! session = getSession
books <- books.Add(session, (id :: books.[session]))
return ()
}
(* handlers *)
registerHandler "printBasket" (httpContinuation {
let! idsInBasket = getIdsInBasket
let! result = request (basketTemplate idsInBasket)
do! processHandler "main"
})
registerHandler "main" (httpContinuation {
let! result = request mainTemplate
let idToBuy = result.QueryString.["id"]
let! name = auth
do! (addIdToBasket idToBuy)
let! nextGoResult = request (buyTemplate idToBuy)
let nextAction = nextGoResult.QueryString.["action"]
if nextAction = "basket"
then do! processHandler "printBasket"
else do! processHandler "main"
})
mw.server.startServer "http://localhost:7777/mw/" "main"