-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
105 lines (90 loc) · 2.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
package main
import (
"database/sql"
"encoding/json"
_ "github.com/go-sql-driver/mysql"
"golang.org/x/crypto/bcrypt"
"net/http"
)
func Dbconnect() *sql.DB {
db, err := sql.Open("mysql", "root:password@tcp(127.0.0.1)/userdb")
if err != nil {
panic(err.Error())
}
return db
}
func main() {
http.HandleFunc("/", Homepage)
http.HandleFunc("/signup", Signup)
http.HandleFunc("/signin", Signin)
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(err.Error())
}
}
func Homepage(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("welcome"))
}
type User struct {
Username string `json:"username"`
Password string `json:"password"`
}
func Signup(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "method not allowed", http.StatusBadRequest)
}
var user User
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
http.Error(w, "bad request decoding error", http.StatusBadRequest)
return
}
dbs := Dbconnect()
var newuser string
err = dbs.QueryRow("select username from users where username=?", user.Username).Scan(&newuser)
switch {
case err == sql.ErrNoRows:
hashedpassword, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
if err != nil {
http.Error(w, "unable create account", http.StatusInternalServerError)
return
}
_, err = dbs.Query("insert into users(username,password) values(?,?)", user.Username, hashedpassword)
if err != nil {
http.Error(w, "server unable to create user", http.StatusInternalServerError)
return
}
w.Write([]byte("user created"))
case err != nil:
http.Error(w, " server error, unbale to create account", http.StatusInternalServerError)
return
default:
http.Error(w, "user already existed", http.StatusBadRequest)
}
}
func Signin(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "method not allowed", http.StatusBadRequest)
return
}
var user User
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
}
dbs := Dbconnect()
var newpassword string
err = dbs.QueryRow("select password from users where username=?", user.Username).Scan(&newpassword)
switch {
case err != nil:
http.Error(w, "unauthorized no user with this username", http.StatusUnauthorized)
return
default:
err := bcrypt.CompareHashAndPassword([]byte(newpassword), []byte(user.Password))
if err != nil {
http.Error(w, "unathorized password wrong", http.StatusUnauthorized)
break
}
w.Write([]byte("welcome" + user.Username))
}
}