forked from mardefasma/tech-curry-pprof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
223 lines (176 loc) · 4.47 KB
/
main.go
File metadata and controls
223 lines (176 loc) · 4.47 KB
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
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"sync"
"golang.org/x/crypto/bcrypt"
)
type User struct {
Username string `json:"username"`
Password string `json:"password"`
Email string `json:"email"`
}
const (
userFilepath = "users.csv"
)
// hint: need changes to activate pprof
func main() {
http.HandleFunc("/register", registerUser)
http.HandleFunc("/login", login)
http.HandleFunc("/", helloWorld)
fmt.Println("Server started at localhost:8080")
http.ListenAndServe(":8080", nil)
}
func helloWorld(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!"))
}
func login(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
var user User
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusInternalServerError)
return
}
_ = json.Unmarshal(body, &user)
if user.Username == "" || user.Password == "" {
http.Error(w, "Please fill out all form fields", http.StatusBadRequest)
return
}
userData, err := getUserDataWithPassword(user.Username, user.Password)
if err != nil {
http.Error(w, "Invalid username or password", http.StatusUnauthorized)
return
}
w.Write([]byte("Hello " + userData.Username + "!"))
}
func registerUser(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
var user User
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusInternalServerError)
return
}
_ = json.Unmarshal(body, &user)
if user.Username == "" || user.Password == "" {
http.Error(w, "Please fill out all form fields", http.StatusBadRequest)
return
}
if !isValidEmail(user.Email) {
http.Error(w, "Invalid email", http.StatusBadRequest)
return
}
if isUserExists(user.Username) {
http.Error(w, "User already exists", http.StatusBadRequest)
return
}
err = SaveUserData(user, userFilepath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write([]byte("User created successfully"))
}
// this seems sus
func isValidEmail(email string) bool {
re := regexp.MustCompile(`^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$`)
if email == "" {
return false
}
return re.MatchString(email)
}
// this function may need attention
func SaveUserData(user User, filepath string) error {
file, err := os.OpenFile(filepath, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
return err
}
w := csv.NewWriter(file)
defer w.Flush()
hashedPassword, err := hashPassword(user.Password)
if err != nil {
return err
}
header := []string{"Name", "Email", "Password"}
if err := w.Write(header); err != nil {
return err
}
if err := w.Write([]string{user.Username, user.Email, hashedPassword}); err != nil {
return err
}
return nil
}
// what can be done here that can improve performance but still serve the same purpose?
func hashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 15)
return string(bytes), err
}
func checkPassword(hashedPassword string, password string) error {
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
}
// there are 2 points that need change here
func getUserDataWithPassword(username, password string) (User, error) {
f, err := os.Open(userFilepath)
if err != nil {
return User{}, err
}
reader := csv.NewReader(f)
records, err := reader.ReadAll()
if err != nil {
return User{}, err
}
var wg sync.WaitGroup
var user User
for _, record := range records {
wg.Add(1)
go func(record []string) {
if record[0] == username {
err = checkPassword(record[2], password)
if err == nil {
user = parseUserData(record)
}
}
}(record)
}
if err != nil {
return User{}, err
}
wg.Wait()
return user, nil
}
// what can be done here that can improve performance but still serve the same purpose? be creative
func isUserExists(username string) bool {
f, err := os.Open(userFilepath)
if err != nil {
return true
}
defer f.Close()
reader := csv.NewReader(f)
records, err := reader.ReadAll()
if err != nil {
return true
}
for _, record := range records {
if record[0] == username {
return true
}
}
return false
}
func parseUserData(record []string) User {
return User{
Username: record[0],
Email: record[1],
Password: record[2],
}
}