Skip to content

Commit b245958

Browse files
committed
update sql
1 parent 3c7702e commit b245958

File tree

7 files changed

+159
-2
lines changed

7 files changed

+159
-2
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/go-playground/locales v0.14.0 // indirect
99
github.com/go-playground/universal-translator v0.18.0 // indirect
1010
github.com/go-playground/validator/v10 v10.11.0 // indirect
11+
github.com/go-sql-driver/mysql v1.6.0 // indirect
1112
github.com/goccy/go-json v0.9.11 // indirect
1213
github.com/json-iterator/go v1.1.12 // indirect
1314
github.com/leodido/go-urn v1.2.1 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/j
1212
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
1313
github.com/go-playground/validator/v10 v10.11.0 h1:0W+xRM511GY47Yy3bZUbJVitCNg2BOGlCyvTqsp/xIw=
1414
github.com/go-playground/validator/v10 v10.11.0/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
15+
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
16+
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
1517
github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk=
1618
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
1719
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=

main.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

3-
import ginlearn "learning2/gin"
3+
import (
4+
"learning2/sql"
5+
)
46

57
func main() {
6-
ginlearn.Run()
8+
sql.Run()
79
}

sql/db.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package sql
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
"log"
7+
)
8+
9+
var dB *sql.DB
10+
11+
func InitializeDB(dbName, root, pwd, ipAndPort, charset string) {
12+
daraSourceName := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=%s&parseTime=True", root, pwd, ipAndPort, dbName, charset)
13+
db, err := sql.Open("mysql", daraSourceName)
14+
if err != nil {
15+
log.Fatal(err)
16+
}
17+
dB = db
18+
}

sql/main.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package sql
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
_ "github.com/go-sql-driver/mysql"
6+
)
7+
8+
func Run() {
9+
r := gin.Default()
10+
// 链接数据库
11+
InitializeDB("relearn_mysql", "root", "", "", "utf8")
12+
InitRouter(r)
13+
err := r.Run()
14+
if err != nil {
15+
return
16+
}
17+
}

sql/middleware.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package sql
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
)
6+
7+
var auth gin.HandlerFunc = func(context *gin.Context) {
8+
value, err := context.Cookie("gin_cookie")
9+
if err != nil {
10+
context.JSON(403, gin.H{
11+
"message": "认证失败,没有cookie",
12+
})
13+
context.Abort()
14+
} else {
15+
context.Set("cookie", value)
16+
context.Next()
17+
}
18+
}

sql/router.go

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package sql
2+
3+
import (
4+
"crypto/md5"
5+
"encoding/hex"
6+
"fmt"
7+
"github.com/gin-gonic/gin"
8+
"log"
9+
"math/rand"
10+
"time"
11+
)
12+
13+
var r *gin.Engine
14+
15+
func InitRouter(e *gin.Engine) {
16+
r = e
17+
InitAuth()
18+
}
19+
20+
func InitAuth() {
21+
r.POST("/register", func(context *gin.Context) {
22+
name := context.PostForm("name")
23+
password := context.PostForm("password")
24+
spQuestion := context.PostForm("sp_question")
25+
spAnswer := context.PostForm("sp_answer")
26+
stmt, err := dB.Prepare("insert into login_plus(id, name, password_md5, password_salt, sp_question, sp_answer) values (?,?,?,?,?,?)")
27+
if err != nil {
28+
log.Fatal(err)
29+
return
30+
}
31+
salt := GenerateUUIDStr()
32+
passwordMd5 := MD5(password + salt)
33+
_, err = stmt.Exec(0, name, passwordMd5, salt, spQuestion, spAnswer)
34+
if err != nil {
35+
log.Fatal(err)
36+
return
37+
}
38+
context.JSON(200, map[string]string{
39+
"code": "0",
40+
"message": "注册成功",
41+
})
42+
})
43+
r.POST("/login", func(context *gin.Context) {
44+
name := context.PostForm("name")
45+
password := context.PostForm("password")
46+
var passwordMd5 string
47+
var passwordSalt string
48+
stmt, err := dB.Prepare("select password_md5, password_salt from login_plus where name = ?")
49+
if err != nil {
50+
log.Fatal(err)
51+
return
52+
}
53+
rows, err := stmt.Query(name)
54+
if err != nil {
55+
log.Fatal(err)
56+
return
57+
}
58+
defer rows.Close()
59+
rows.Next()
60+
err = rows.Scan(&passwordMd5, &passwordSalt)
61+
if err != nil {
62+
log.Fatal(err)
63+
return
64+
}
65+
if passwordMd5 == MD5(password+passwordSalt) {
66+
context.SetCookie("gin_cookie", "", 3600, "/", "", false, true)
67+
context.JSON(200, map[string]string{
68+
"code": "0",
69+
"message": "登录成功",
70+
})
71+
} else {
72+
context.JSON(200, map[string]string{
73+
"code": "-1",
74+
"message": "账号或密码错误",
75+
})
76+
}
77+
})
78+
}
79+
80+
func GenerateUUIDStr() string {
81+
rand.Seed(time.Now().Unix())
82+
var randomBytes = make([]byte, 16)
83+
for i := 0; i < 16; i++ {
84+
randomBytes[i] = byte(rand.Intn(128))
85+
}
86+
// 摘自 java uuid 的生成
87+
randomBytes[6] &= 0x0f /* clear version */
88+
randomBytes[6] |= 0x40 /* set to version 4 */
89+
randomBytes[8] &= 0x3f /* clear variant */
90+
randomBytes[8] |= 0x80 /* set to IETF variant */
91+
return hex.EncodeToString(randomBytes)
92+
}
93+
94+
func MD5(str string) string {
95+
data := []byte(str) //切片
96+
has := md5.Sum(data)
97+
md5str := fmt.Sprintf("%x", has) //将[]byte转成16进制
98+
return md5str
99+
}

0 commit comments

Comments
 (0)