-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
172 lines (159 loc) · 4.54 KB
/
main.go
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
// This is a prototype application created to illustrate what an online
// court experience could be like. The app currently does little more than navigate between
// mocked pages representing different states in the experience for different users.
//
// In order to function as envisioned, we would need to implement many of the
// vital features, a much more complex data model, persistent storage, asynchronous messaging,
// a library of default questions, requirements, and timelines targeted at specific
// types of claims, user authentication, ...
package main
import (
"html/template"
"net/http"
)
var dcases, jcases d
// create our mock data
func init() {
dcases = d{
"1": d{
"id": "HQ17XO1372",
"c": "Mrs Connie Wilkinson",
"d": "Diamond Care Homes Ltd",
"s": "Awaiting Review",
"dead": "30 Jun 2017",
},
"2": d{
"id": "FM16P00127",
"c": "Mrs Connie Wilkinson",
"d": "Mr Fred Bloggs",
"s": "Closed",
"dead": "24 Apr 2015",
},
}
jcases = d{
"1": d{
"id": "HQ17XO1372",
"c": "Mrs Connie Wilkinson",
"d": "Diamond Care Homes Ltd",
"s": "Awaiting Review",
"dead": "30 Jun 2017",
},
"2": d{
"id": "452345",
"c": "Minerva Hooper",
"d": "Cyril Figgis",
"s": "Awaiting Defendent Response",
"dead": "7 Jul 2017",
},
"3": d{
"id": "897980",
"c": "Sage Rodriguez",
"d": "Saul Goodman",
"s": "Pending Claimant Response",
"dead": "8 Jul 2017",
},
"4": d{
"id": "345985",
"c": "Philip Chaney",
"d": "Sterling Archer",
"s": "Pending Claimant Response",
"dead": "8 Jul 2017",
},
"5": d{
"id": "767806",
"c": "Doris Greene",
"d": "Michael Bluth",
"s": "Awaiting your Decision",
"dead": "9 Jul 2017",
},
}
}
// data for templates
type d map[string]interface{}
// Handler for home page
// /user/:id, i.e. /user/ABC1234567
func mainHandler(w http.ResponseWriter, r *http.Request) {
// TODO maintain user authentication/id with token/cookie
id := r.URL.Path[len("/user/"):]
// in porduction, here you would get user from db based on id
// including checking authrntication
usertype := "party"
if id == "1" {
usertype = "judge"
}
data := d{
"jcases": jcases,
"usertype": usertype,
}
t, err := template.ParseFiles("static/dashboard.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
t.Execute(w, data)
}
// Handler for html pages
func pageHandler(w http.ResponseWriter, r *http.Request) {
// TODO maintain user authentication/id with token/cookie
t, err := template.ParseFiles("static/homepage.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
t.Execute(w, d{})
}
// handler for new case, i.e. /case/new
func newCaseHandler(w http.ResponseWriter, r *http.Request) {
// TODO maintain user authentication/id with token/cookie
if r.Method == http.MethodGet {
// get form to input new case
t, err := template.ParseFiles("static/newClaim.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
t.Execute(w, d{})
} else if r.Method == http.MethodPost {
// TODO create/save case
http.Redirect(w, r, "/case/id#", http.StatusOK)
} else {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
}
// handler for exisitng cases
func caseHandler(w http.ResponseWriter, r *http.Request) {
// TODO maintain user authentication/id with token/cookie
id := r.URL.Path[len("/case/"):]
if len(id) == 0 {
// TODO: get all cases from db with this user ID as a party
data := d{
"dcases": dcases,
}
t, err := template.ParseFiles("static/caseList.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
t.Execute(w, data)
} else {
t, err := template.ParseFiles("static/case.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
t.Execute(w, d{})
}
}
// handler for evidence
func evidenceHandler(w http.ResponseWriter, r *http.Request) {
// TODO maintain user authentication/id with token/cookie
t, err := template.ParseFiles("static/evidence.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
t.Execute(w, d{})
}
func main() {
http.HandleFunc("/", pageHandler)
http.HandleFunc("/user/", mainHandler)
http.HandleFunc("/case/new", newCaseHandler)
http.HandleFunc("/case/", caseHandler)
http.HandleFunc("/evidence/", evidenceHandler)
http.Handle("/assets/", http.FileServer(http.Dir("./static")))
http.ListenAndServe(":8080", nil)
}