-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
179 lines (142 loc) · 3.99 KB
/
handlers.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
173
174
175
176
177
178
179
package main
import (
"database/sql"
"fmt"
"net/http"
"golang.org/x/crypto/bcrypt"
)
var err error
func Signup(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.ServeFile(w, r, "signup.html")
return
}
username := r.FormValue("username")
password := r.FormValue("password")
var user string
rows, err := db.Query("SELECT username FROM users WHERE username=?", username)
if err != nil {
panic(err.Error())
}
count := 0
for rows.Next() {
count++
}
if count > 0 {
w.Write([]byte("USER already existed"))
return
}
err = db.QueryRow("SELECT username FROM users WHERE username=?", username).Scan(&user)
switch {
case err == sql.ErrNoRows:
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
http.Error(w, "Unable to crate accout right now (ONE)", http.StatusInternalServerError)
return
}
_, err = db.Exec("INSERT INTO users(username,password) VALUES(?,?)", username, hashedPassword)
if err != nil {
http.Error(w, "Unable to create your account (TWO)", http.StatusInternalServerError)
return
}
w.Write([]byte("User Accout Created"))
case err != nil:
http.Error(w, "Unable to create your account (THREE)", http.StatusInternalServerError)
return
default:
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}
http.Redirect(w, r, "/", http.StatusOK)
}
func Login(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.ServeFile(w, r, "login.html")
return
}
username := r.FormValue("username")
password := r.FormValue("password")
var dbusername string
var dbpassword string
err = db.QueryRow("SELECT username, password FROM users WHERE username=?", username).Scan(&dbusername, &dbpassword)
if err != nil {
w.Write([]byte("User Does Not Exist"))
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return
}
err = bcrypt.CompareHashAndPassword([]byte(dbpassword), []byte(password))
if err != nil {
w.Write([]byte("Credentials Did not Match!"))
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return
}
http.Redirect(w, r, "/register", 301)
}
func Homepage(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
}
func Register(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.ServeFile(w, r, "register.html")
}
http.Redirect(w, r, "/stored", 301)
}
func Storedb(w http.ResponseWriter, r *http.Request) {
var VoterID string
var LastName string
var FirstName string
var Age string
var Sex string
var State string
var City string
switch r.Method {
case "GET":
http.ServeFile(w, r, "db.html")
case "POST":
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
VoterID = r.FormValue("VoterID")
LastName = r.FormValue("LastName")
FirstName = r.FormValue("FirstName")
State = r.FormValue("State")
City = r.FormValue("City")
Age = r.FormValue("Age")
Sex = r.FormValue("Sex")
default:
fmt.Fprintf(w, "Service only Supports GET and POST")
}
_, err = db.Exec("INSERT INTO Voters(VoterID,LastName,FirstName,Age,Sex,State,City)VALUES(?,?,?,?,?,?,?)", VoterID, LastName, FirstName, Age, Sex, State, City)
if err != nil {
panic(err.Error())
}
w.Write([]byte(" Registered Successully!"))
}
func TermsandConditions(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
http.ServeFile(w, r, "terms.html")
case "POST":
http.Redirect(w, r, "/", 301)
default:
fmt.Fprintf(w, "Service only Supports GET and POST")
}
}
func Vote(w http.ResponseWriter, r *http.Request) {
fmt.Println("Vote Handler has been called!")
switch r.Method {
case "GET":
http.ServeFile(w, r, "vote.html")
case "POST":
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
res := r.Form.Get("Party_Name")
fmt.Println("party values is ", res)
default:
fmt.Fprintf(w, "Service only Supports GET and POST")
}
fmt.Println("Vote Handler is about to end ")
w.Write([]byte("Vote !"))
}