-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
179 lines (148 loc) · 3.61 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
173
174
175
176
177
178
179
package main
import (
"html/template"
"io/ioutil"
"log"
"net/http"
"strings"
_ "github.com/mattn/go-sqlite3"
"github.com/jerryshell/golang-web-login/dao"
"github.com/jerryshell/golang-web-login/domain"
"github.com/jerryshell/golang-web-login/session"
)
func init() {
http.HandleFunc("/", index)
http.HandleFunc("/login", login)
http.HandleFunc("/userinfo", userinfo)
http.HandleFunc("/logout", logout)
http.HandleFunc("/register", register)
}
func main() {
log.Println("Server is running at http://localhost:8080/. Press Ctrl+C to stop.")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func index(w http.ResponseWriter, r *http.Request) {
user, _ := session.GetSession(w, r).GetAttr("user")
t, err := template.ParseFiles("html/index.html")
checkError(err)
err = t.Execute(w, user)
checkError(err)
}
func login(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
loginHTML, err := ioutil.ReadFile("html/login.html")
checkError(err)
_, err = w.Write(loginHTML)
if err != nil {
log.Println(err)
}
return
}
username := r.FormValue("username")
password := r.FormValue("password")
log.Println("login", username, password)
if isEmpty(username, password) {
message(w, r, "字段不能为空")
return
}
user := dao.FindUserByUsernameAndPassword(username, password)
if user == nil {
message(w, r, "登录失败!")
return
}
// 登陆成功
sess := session.GetSession(w, r)
sess.SetAttr("user", user)
http.Redirect(w, r, "/", 302)
}
func message(w http.ResponseWriter, r *http.Request, message string) {
t, err := template.ParseFiles("html/message.html")
checkError(err)
err = t.Execute(w, map[string]string{"Message": message})
checkError(err)
}
func userinfo(w http.ResponseWriter, r *http.Request) {
sess := session.GetSession(w, r)
user, exist := sess.GetAttr("user")
if !exist {
http.Redirect(w, r, "/", 302)
return
}
if r.Method == "GET" {
t, err := template.ParseFiles("html/userinfo.html")
checkError(err)
err = t.Execute(w, user)
checkError(err)
return
}
// POST 更新用户信息
username := r.FormValue("username")
password := r.FormValue("password")
email := r.FormValue("email")
if isEmpty(username, password, email) {
message(w, r, "字段不能为空")
return
}
switch user := user.(type) {
case *domain.User:
user.Username = username
user.Password = password
user.Email = email
dao.UpdateUser(user)
default:
log.Println(":userinfo: user.(type)", user)
}
http.Redirect(w, r, "/userinfo", 302)
}
func register(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
registerHTML, err := ioutil.ReadFile("html/register.html")
checkError(err)
_, err = w.Write(registerHTML)
if err != nil {
log.Println(err)
return
}
return
}
username := r.FormValue("username")
password := r.FormValue("password")
password2 := r.FormValue("password2")
email := r.FormValue("email")
if isEmpty(username, password, password2, email) {
message(w, r, "字段不能为空")
return
}
if password != password2 {
message(w, r, "两次密码不相符")
return
}
user := &domain.User{
Username: username,
Password: password,
Email: email,
}
dao.AddUser(user)
message(w, r, "注册成功!")
}
func logout(w http.ResponseWriter, r *http.Request) {
sess := session.GetSession(w, r)
sess.DelAttr("user")
http.Redirect(w, r, "/", 302)
}
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
func isEmpty(strSlice ...string) (isEmpty bool) {
for _, str := range strSlice {
str = strings.TrimSpace(str)
if str == "" || len(str) == 0 {
isEmpty = true
return
}
}
isEmpty = false
return
}