-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
69 lines (58 loc) · 1.39 KB
/
database.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
package main
import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"log"
"time"
)
// 数据库初始化
func DBInit() *gorm.DB {
db, err := gorm.Open(sqlite.Open("default.db"), &gorm.Config{})
if err != nil {
log.Fatal("数据库打开失败")
}
err = db.AutoMigrate(&User{})
if err != nil {
return nil
}
err = db.AutoMigrate(&Post{})
if err != nil {
return nil
}
err = db.AutoMigrate(&Session{})
if err != nil {
return nil
}
return db
}
// 数据结构定义
type User struct {
UserID uint64 `gorm:"primary_key" json:"user_id"`
Username string `json:"username"`
Password string `json:"password"`
Name string `json:"name"`
UserType uint8 `json:"user_type"`
Subscribed string `json:"subscribed"`
Reported string `json:"reported"`
Written string `json:"written"`
}
type Response struct {
Code uint16 `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
type Session struct {
SessionID string `gorm:"primary_key" json:"session_id"`
UserID uint64 `json:"user_id"`
UserType uint8 `json:"user_type"`
Time time.Time
}
type Post struct {
PostID uint64 `gorm:"primary_key" json:"post_id"`
UserID uint64 `json:"user_id"`
Content string `json:"content"`
Time time.Time `json:"time"`
Counter int16 `json:"counter"`
Subscribed string `json:"subscribed"`
Reported string `json:"reported"`
}