-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.back
307 lines (253 loc) · 7.55 KB
/
backup.back
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package main
import (
"database/sql"
"fmt"
"html/template"
"log"
"math/rand"
"net/http"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/sessions"
)
var tpl *template.Template
var static_name string
var (
key = []byte("secret-key")
store = sessions.NewCookieStore(key)
)
func init() {
tpl = template.Must(template.ParseGlob("templates/*.html"))
}
func index(w http.ResponseWriter, r *http.Request) {
//http.ServeFile(w, r, "./templates/index.html")
// tpl.ExecuteTemplate(w, "index.html", nil)
session, _ := store.Get(r, "login")
session.Values["authenticated"] = true
session.Save(r, w)
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
var husr, hpass, email, pass string
//fmt.Println(r.PostFormValue("button"))
// fmt.Fprintf(w, "POST request successful\n")
if r.FormValue("button") == "hospital" {
husr = r.PostFormValue("huser")
static_name = husr
hpass = r.PostFormValue("hpass")
check := hcheck(husr, hpass)
//fmt.Println("Reached checkpoint")
//fmt.Println(check)
if check {
fmt.Println(check)
http.Redirect(w, r, "/hospital", http.StatusFound)
//http.ServeFile(w, r, "./templates/main.html")
} else {
fmt.Println(check)
fmt.Fprintf(w, "USER NOT FOUND\n")
http.Redirect(w, r, "/", http.StatusFound)
}
}
if r.FormValue("buttons") == "root" {
email = r.PostFormValue("auser")
pass = r.PostFormValue("apass")
chck := check(string(email), string(pass))
if chck {
//fmt.Println(chck)
http.Redirect(w, r, "/main", http.StatusFound)
//http.ServeFile(w, r, "./templates/main.html")
} else {
fmt.Println(chck)
fmt.Fprintf(w, "USER NOT FOUND\n")
http.Redirect(w, r, "/", http.StatusFound)
}
}
}
// MAIN FUNCTION
func main() {
fmt.Println("Serving at localhost:8080...")
fileServer := http.FileServer(http.Dir("./templates"))
http.Handle("/", fileServer)
//http.HandleFunc("/", index)
http.HandleFunc("/login", index)
http.HandleFunc("/main", newentry)
http.HandleFunc("/done", temp)
http.HandleFunc("/hospital", hospital)
http.HandleFunc("/hospitaldone", hospitaldone)
http.HandleFunc("/view", viewhandler)
http.HandleFunc("/error", errored)
http.ListenAndServe(":8080", nil)
}
func errored(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Error")
}
// Check in database for email and it's corresponding password
func check(mail string, pass string) bool {
status := false
db, err := sql.Open("mysql", "root:yes@tcp(localhost:3306)/test_schema")
if err != nil {
fmt.Printf("not connected")
}
defer db.Close()
query := "SELECT Passwords FROM test_schema.database WHERE Email ='" + mail + "'"
res, err := db.Query(query)
if err != nil {
panic(err.Error())
}
for res.Next() {
var passe string
if err := res.Scan(&passe); err != nil {
log.Fatal(err)
}
if passe == pass {
status = true
//fmt.Println(passe)
}
}
if err := res.Err(); err != nil {
log.Fatal(err)
}
return status
}
// New Entry
func newentry(w http.ResponseWriter, r *http.Request) {
//http.ServeFile(w, r, "./templates/main.html")
session, _ := store.Get(r, "login")
if auth, err := session.Values["authenticated"].(bool); !err || !auth {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
tpl.ExecuteTemplate(w, "main.html", nil)
}
func temp(w http.ResponseWriter, r *http.Request) {
district := r.FormValue("district")
pin := r.FormValue("pin")
hospital_name := r.FormValue("name")
//fmt.Printf("%v %v %v\n", district, pin, hospital_name)
chck := newentity(district, pin, hospital_name)
if chck {
//fmt.Printf("working...\n")
http.Redirect(w, r, "/main", http.StatusFound)
} else {
http.Redirect(w, r, "/", http.StatusFound)
}
}
// Connection to New database on same server
func newentity(district string, pin string, hospital_name string) bool {
stat := true
db, err := sql.Open("mysql", "root:yes@tcp(localhost:3306)/test_schema")
if err != nil {
fmt.Printf("not connected")
}
defer db.Close()
query := "INSERT INTO test_schema.data (namess, pincode, district) SELECT * FROM (SELECT '" + hospital_name + "' AS namess, '" + pin + "' AS pincode, '" + district + "' AS district) AS temp WHERE NOT EXISTS( SELECT namess FROM test_schema.data WHERE district='" + district + "' AND pincode='" + pin + "');"
_, errs := db.Query(" INSERT INTO test_schema.hospital (namess, passwords, oxygen_beds, ventilator_beds, normal_bed) SELECT * FROM (SELECT '" + hospital_name + "' AS namess, '" + RandStringRunes(4) + "' AS passwords, '0' AS oxygen_beds,'0' AS ventilator_beds,'0' AS normal_bed) AS tmp WHERE NOT EXISTS ( SELECT namess FROM test_schema.hospital WHERE namess = '" + hospital_name + "' );")
_, err = db.Query(query)
if err != nil {
fmt.Println(err.Error())
stat = false
}
if err != nil {
fmt.Println(errs)
}
return stat
}
//view table
func viewhandler(w http.ResponseWriter, r *http.Request) {
res := view(r.FormValue("pin"))
for res.Next() {
var table string
if err := res.Scan(&table); err != nil {
log.Fatal(err)
}
fmt.Fprintf(w, table+"\n")
}
}
func view(pin string) *sql.Rows {
db, err := sql.Open("mysql", "root:yes@tcp(localhost:3306)/test_schema")
if err != nil {
fmt.Printf("not connected")
}
defer db.Close()
query := "SELECT name FROM test_schema.data WHERE pincode ='" + pin + "';"
res, _ := db.Query(query)
return res
}
//Hospital handler
func hospital(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "login")
if auth, err := session.Values["authenticated"].(bool); !err || !auth {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
tpl.ExecuteTemplate(w, "hospital.html", nil)
}
func hospitaldone(w http.ResponseWriter, r *http.Request) {
oxygen := r.FormValue("oxy")
vent := r.FormValue("vent")
norm := r.FormValue("norm")
//fmt.Printf("%T", oxygen)
chck := addbeds(oxygen, vent, norm, static_name)
if chck {
http.Redirect(w, r, "/hospital", http.StatusFound)
} else {
http.Redirect(w, r, "/", http.StatusForbidden)
}
}
func addbeds(oxy string, vent string, norm string, name string) bool {
stat := true
//fmt.Printf(" %v, %v, %v\n", oxy, vent, norm)
query := "UPDATE test_schema.hospital SET oxygen_beds='" + oxy + "', ventilator_beds='" + vent + "', normal_bed='" + norm + "' WHERE namess='" + name + "';"
//fmt.Println(query)
//query := "UPDATE test_schema.hospital SET oxygen_beds='0'"
db, err := sql.Open("mysql", "root:yes@tcp(localhost:3306)/test_schema")
if err != nil {
fmt.Printf("not connected")
}
defer db.Close()
_, err = db.Query(query)
if err != nil {
fmt.Println(err.Error())
stat = false
}
return stat
}
func hcheck(usr string, hpass string) bool {
stat := false
db, err := sql.Open("mysql", "root:yes@tcp(localhost:3306)/test_schema")
if err != nil {
fmt.Printf("not connected")
}
defer db.Close()
query := "SELECT passwords FROM test_schema.hospital WHERE namess ='" + usr + "'"
res, _ := db.Query(query)
//fmt.Println("Reached checkpoint")
for res.Next() {
var passe string
if err := res.Scan(&passe); err != nil {
log.Fatal(err)
}
fmt.Println(passe)
if passe == hpass {
stat = true
//fmt.Println(passe)
}
}
if err := res.Err(); err != nil {
log.Fatal(err)
}
return stat
}
//-------------------------------------------------------------------------------------------------------------------------------------
func init() {
rand.Seed(time.Now().UnixNano())
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func RandStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}