-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.scm
136 lines (123 loc) · 4.67 KB
/
server.scm
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
(use-modules (artanis artanis))
(use-modules (artanis utils))
(load "db.scm")
(load "session.scm")
(init-server)
(define require-login
(lambda (rc if-logged-in)
(let ((pid (session-get rc "pid")))
(format #t "Pid: ~a" pid)
(cond
((or (not (string? pid)) (string=? pid ""))
(let* (
(cor #t)
(page (tpl->html "login.tpl" (the-environment))))
(tpl->response "layout.tpl" (the-environment))))
(else (if-logged-in rc))))))
(define get-fname
(lambda (pid)
(let ((res (db-query
(format #f "SELECT fname FROM pdata WHERE id='~a'" pid))))
(cond
((null? res) "")
(else (cdr (car (car res))))))))
(define get-lname
(lambda (pid)
(let ((res (db-query
(format #f "SELECT lname FROM pdata WHERE id='~a'" pid))))
(cond
((null? res) "")
(else (cdr (car (car res))))))))
(get "/session" #:session #t
(lambda (rc)
(format #f "Session: ~a" (session-sid rc))))
(get "/setpid/:pid" #:session #t
(lambda (rc)
(session-set rc "pid" (params rc "pid"))
(format #f "PID ~a" (params rc "pid"))))
(get "/getpid" #:session #t
(lambda (rc)
(require-login rc (lambda (rc)
(format #f "PID: ~a" (session-get rc "pid"))))))
(get "/login" #:session #t
(lambda (rc)
(let* ((cor (not (params rc "incorrect")))
(page (tpl->html "login.tpl" (the-environment))))
(tpl->response "layout.tpl" (the-environment)))))
(get "/" #:session #t
(lambda (rc)
(let* ((lname (get-lname (session-get rc "pid")))
(fname (get-fname (session-get rc "pid")))
(page (tpl->html "index.tpl" (the-environment))))
(tpl->response "layout.tpl" (the-environment)))))
(get "/logout" #:session #t
(lambda (rc)
(session-set rc "pid" "")
(let* (
(lname "")
(fname "")
(page (tpl->html "index.tpl" (the-environment))))
(tpl->response "layout.tpl" (the-environment)))))
(get "/do_login" #:session #t
(lambda (rc)
(let* ((uname (params rc "uname"))
(pid (let ((res (db-query
(format #f
"SELECT id FROM pdata WHERE uname='~a'"
uname))))
(cond
((null? res) "")
(else (cdr (car (car res)))))))
(pin_cor (let ((res (db-query
(format #f
"SELECT pin FROM pdata WHERE id='~a'"
pid))))
(cond
((null? res) "")
(else (cdr (car (car res)))))))
(cor (string=?
(params rc "pin")
pin_cor))
(lname (get-lname pid))
(fname (get-fname pid))
(page (tpl->html
(cond (cor "index.tpl") (else "login.tpl"))
(the-environment))))
(db-query "SELECT * FROM pdata")
(format
#t
"uname: ~a pid: ~a pcor: ~a - pin: ~a - cor: ~a~%"
uname pid pin_cor (params rc "pin") cor)
(cond
(cor
(session-set rc "pid" pid)
(let ((page (tpl->html "index.tpl" (the-environment))))
(tpl->response "layout.tpl" (the-environment))))
(else
(let ((page (tpl->html "login.tpl" (the-environment))))
(response-emit
(tpl->html "layout.tpl" (the-environment))
#:status 401)))))))
(get "/showdata/:data" #:session #t
(lambda (rc)
(require-login
rc (lambda (rc)
(let* ((dtype (params rc "data"))
(pid (session-get rc "pid"))
(data (db-query
(format #f "SELECT * FROM ~a WHERE pid='~a'"
dtype pid))))
(cond
((not (or
(string=? dtype "appointments")
(string=? dtype "medical_issues")
(string=? dtype "medications")
(string=? dtype "allergies")))
"400 yo")
(else
(let
((page (tpl->html
(format #f "show-~a.tpl" dtype)
(the-environment))))
(tpl->response "layout.tpl" (the-environment))))))))))
(run #:port 8080)