/
db.go
68 lines (59 loc) · 1.41 KB
/
db.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
package main
import (
"fmt"
"os"
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
var db *gorm.DB
// Room 需要监控的直播间 ID
type Room struct {
ID uint
Name string
}
// User 用户
type User struct {
ID uint
Name string `gorm:"size=16"`
}
// Rqz 人气
type Rqz struct {
ID uint
UP uint `gorm:"index:up"`
Value uint
CreatedAt time.Time `gorm:"index:time"`
}
// Danmaku 弹幕
type Danmaku struct {
ID uint
UP uint `gorm:"index:up"`
User User `gorm:"foreignkey:UserRefer"`
UserRefer uint `gorm:"index:user"`
Kimo uint `gorm:"type:tinyint(1)"`
Comment string
CreatedAt time.Time `gorm:"index:time"`
}
// Gift 礼物
type Gift struct {
ID uint
UP uint `gorm:"index:up"`
User User `gorm:"foreignkey:UserRefer"`
UserRefer uint `gorm:"index:user"`
Type bool // true 花钱买的 - false 免费的和白嫖的
Name string
Number uint
Price uint
Remain uint
CreatedAt time.Time `gorm:"index:time"`
}
func init() {
var err error
db, err = gorm.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Asia%%2fShanghai", os.Getenv("DBUSER"), os.Getenv("DBPASS"), os.Getenv("DBHOST"), os.Getenv("DBNAME")))
if err != nil {
panic(err)
}
db.AutoMigrate(&Room{}, &User{}, &Rqz{}, &Danmaku{}, &Gift{})
db.DB().SetMaxIdleConns(10)
db.DB().SetMaxOpenConns(100)
}