Skip to content

Commit 836b3e5

Browse files
committed
✨ feat(database): support mysql
1 parent cdab114 commit 836b3e5

File tree

9 files changed

+86
-52
lines changed

9 files changed

+86
-52
lines changed

appconfig/global.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ var (
1313
)
1414

1515
type AppConfig struct {
16-
Port int `json:"port"`
16+
Port int `json:"port"`
17+
DatabaseType string `json:"database"`
18+
Source string `json:"source"`
1719
}
1820

1921
type BotConfig struct {

database/init.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
package database
22

33
import (
4-
"os"
5-
"path/filepath"
6-
4+
"github.com/dingdinglz/dingbot/appconfig"
75
"github.com/dingdinglz/dingbot/tool"
6+
"gorm.io/driver/mysql"
87
"gorm.io/driver/sqlite"
98
"gorm.io/gorm"
109
)
1110

1211
func DatabaseInit() {
13-
rootPath, _ := os.Getwd()
1412
var err error
15-
DB, err = gorm.Open(sqlite.Open(filepath.Join(rootPath, "data", "data.db")))
13+
switch appconfig.AppConfigVar.DatabaseType {
14+
case "sqlite":
15+
DB, err = gorm.Open(sqlite.Open(appconfig.AppConfigVar.Source))
16+
case "mysql":
17+
DB, err = gorm.Open(mysql.Open(appconfig.AppConfigVar.Source))
18+
default:
19+
panic("database type error")
20+
}
1621
if err != nil {
1722
tool.ErrorOut("database", "open error", err)
23+
panic(err.Error())
1824
}
1925
DB.AutoMigrate(&KeyWordTable{}, &OpenTable{}, &StopTable{}, &MessageTable{}, &PluginData{}, &StarTable{}, &GithubHookTable{})
2026

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/gofiber/fiber/v2 v2.52.5
88
github.com/gofiber/template/html/v2 v2.1.2
99
github.com/robertkrimen/otto v0.4.1-0.20241103155535-e3739096c29e
10+
gorm.io/driver/mysql v1.5.7
1011
gorm.io/driver/sqlite v1.5.6
1112
gorm.io/gorm v1.25.12
1213
)
@@ -17,6 +18,7 @@ require (
1718
github.com/andybalholm/brotli v1.0.5 // indirect
1819
github.com/fumiama/gofastTEA v0.0.10 // indirect
1920
github.com/fumiama/imgsz v0.0.4 // indirect
21+
github.com/go-sql-driver/mysql v1.7.0 // indirect
2022
github.com/gofiber/template v1.8.3 // indirect
2123
github.com/gofiber/utils v1.1.0 // indirect
2224
github.com/google/uuid v1.5.0 // indirect

go.sum

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ github.com/fumiama/gofastTEA v0.0.10 h1:JJJ+brWD4kie+mmK2TkspDXKzqq0IjXm89aGYfoG
1212
github.com/fumiama/gofastTEA v0.0.10/go.mod h1:RIdbYZyB4MbH6ZBlPymRaXn3cD6SedlCu5W/HHfMPBk=
1313
github.com/fumiama/imgsz v0.0.4 h1:Lsasu2hdSSFS+vnD+nvR1UkiRMK7hcpyYCC0FzgSMFI=
1414
github.com/fumiama/imgsz v0.0.4/go.mod h1:bISOQVTlw9sRytPwe8ir7tAaEmyz9hSNj9n8mXMBG0E=
15+
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
16+
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
1517
github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo=
1618
github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
1719
github.com/gofiber/template v1.8.3 h1:hzHdvMwMo/T2kouz2pPCA0zGiLCeMnoGsQZBTSYgZxc=
@@ -76,7 +78,10 @@ gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
7678
gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78=
7779
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
7880
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
81+
gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo=
82+
gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
7983
gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE=
8084
gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
85+
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
8186
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
8287
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=

main.go

-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import (
44
"fmt"
55

66
"github.com/dingdinglz/dingbot/appconfig"
7-
"github.com/dingdinglz/dingbot/database"
87
)
98

109
func main() {
1110
fmt.Println("dingbot " + appconfig.Version + " Copyright © dinglz 2024")
1211
appconfig.ConfigInit()
13-
database.DatabaseInit()
1412
ServerInit()
1513
ServerRun()
1614
}

route/github.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package route
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"strings"
76

87
"github.com/LagrangeDev/LagrangeGo/message"
@@ -13,14 +12,13 @@ import (
1312
)
1413

1514
func GithubWebhookRoute(ctx *fiber.Ctx) error {
16-
eventName := ctx.Get("X-GitHub-Event")
17-
fmt.Println(eventName)
1815
if bot.DingQQBot == nil {
1916
return JsonMessage(ctx, 0, "ok")
2017
}
2118
if !bot.DingQQBot.Online.Load() {
2219
return JsonMessage(ctx, 0, "ok")
2320
}
21+
eventName := ctx.Get("X-GitHub-Event")
2422
var webhookInfos map[string]interface{}
2523
json.Unmarshal(ctx.Body(), &webhookInfos)
2624
repositoryName := webhookInfos["repository"].(map[string]interface{})["full_name"].(string)
@@ -29,13 +27,12 @@ func GithubWebhookRoute(ctx *fiber.Ctx) error {
2927
}
3028
sendGroupsString := database.GithubWebhookGet(repositoryName)
3129
sendGroups := strings.Split(sendGroupsString, ",")
32-
fmt.Println(webhookInfos["action"].(string))
3330
switch eventName {
3431
case "push":
3532
bot.DingQQBot.SendGroupMessage(11, []message.IMessageElement{message.NewText("11")})
3633
case "star":
3734
if webhookInfos["action"].(string) == "created" {
38-
if database.StarExist(repositoryName, webhookInfos["sender"].(map[string]interface{})["login"].(string)) {
35+
if !database.StarExist(repositoryName, webhookInfos["sender"].(map[string]interface{})["login"].(string)) {
3936
database.StarAdd(repositoryName, webhookInfos["sender"].(map[string]interface{})["login"].(string))
4037
messageSend := "项目" + repositoryName + "得到" + webhookInfos["sender"].(map[string]interface{})["login"].(string) + "のstar\n" + webhookInfos["repository"].(map[string]interface{})["html_url"].(string)
4138
for _, i := range sendGroups {

route/init.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@ import (
1111

1212
func InitRoute(c *fiber.Ctx) error {
1313
port := c.FormValue("port", "")
14+
databaseType := c.FormValue("database", "")
15+
source := c.FormValue("source", "")
1416
if port == "" {
1517
return JsonMessage(c, 1, "port不能为空!")
1618
}
17-
configText, _ := json.Marshal(fiber.Map{"port": tool.StringToInt(port)})
19+
if databaseType == "" {
20+
return JsonMessage(c, 1, "database不能为空!")
21+
}
22+
if source == "" {
23+
return JsonMessage(c, 1, "source不能为空!")
24+
}
25+
configText, _ := json.Marshal(fiber.Map{"port": tool.StringToInt(port), "database": databaseType, "source": source})
1826
rootPath, _ := os.Getwd()
1927
os.WriteFile(filepath.Join(rootPath, "data", "config.json"), configText, os.ModePerm)
2028
return JsonMessage(c, 0, "ok")

server.go

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func ServerRun() {
3232
if appconfig.Init {
3333
ServerInitRun()
3434
} else {
35+
database.DatabaseInit()
3536
ServerCommonRun()
3637
}
3738
}

web/init/index.html

+53-38
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,60 @@
11
<head>
2-
<title>dingbot初始化</title>
3-
<link href="/layui/css/layui.css" rel="stylesheet">
2+
<title>dingbot初始化</title>
3+
<link href="/layui/css/layui.css" rel="stylesheet">
44
</head>
5+
56
<body>
6-
<script src="/layui/layui.js"></script>
7-
<div style="margin: 15px;">
8-
<form class="layui-form">
9-
<div class="layui-form-item">
10-
<label class="layui-form-label">启动端口</label>
11-
<div class="layui-input-block">
12-
<input type="number" name="port" placeholder="请输入启动端口(pure number)" autocomplete="off" class="layui-input">
13-
</div>
14-
</div>
15-
<div class="layui-form-item">
16-
<label class="layui-form-label"></label>
17-
<button class="layui-btn" lay-submit lay-filter="init-submit">初始化</button>
18-
</div>
19-
</form>
20-
</div>
7+
<script src="/layui/layui.js"></script>
8+
<div style="margin: 15px;">
9+
<form class="layui-form">
10+
<div class="layui-form-item">
11+
<label class="layui-form-label">启动端口</label>
12+
<div class="layui-input-block">
13+
<input type="number" name="port" placeholder="请输入启动端口(pure number)" autocomplete="off" class="layui-input">
14+
</div>
15+
</div>
16+
<div class="layui-form-item">
17+
<label class="layui-form-label">数据库类型</label>
18+
<div class="layui-input-block">
19+
<select name="database">
20+
<option value=""></option>
21+
<option value="mysql">Mysql</option>
22+
<option value="sqlite">Sqlite3</option>
23+
</select>
24+
</div>
25+
</div>
26+
<div class="layui-form-item">
27+
<label class="layui-form-label">数据库源</label>
28+
<div class="layui-input-block">
29+
<input name="source" placeholder="例如data.db" autocomplete="off" class="layui-input">
30+
</div>
31+
</div>
32+
<div class="layui-form-item">
33+
<label class="layui-form-label"></label>
34+
<button class="layui-btn" lay-submit lay-filter="init-submit">初始化</button>
35+
</div>
36+
</form>
37+
</div>
2138
</body>
2239
<script>
23-
layui.use(function(){
24-
var form = layui.form;
25-
var layer = layui.layer;
26-
form.on('submit(init-submit)', function(data){
27-
var field = data.field; // 获取表单全部字段值
28-
layui.$.post("/api/init",field,function(data){
29-
if(data.code === 0)
30-
{
31-
layer.alert("请重启程序以正式启动dingbot", {
32-
title: '初始化成功!'
40+
layui.use(function () {
41+
var form = layui.form;
42+
var layer = layui.layer;
43+
form.on('submit(init-submit)', function (data) {
44+
var field = data.field; // 获取表单全部字段值
45+
layui.$.post("/api/init", field, function (data) {
46+
if (data.code === 0) {
47+
layer.alert("请重启程序以正式启动dingbot", {
48+
title: '初始化成功!'
49+
});
50+
}
51+
else {
52+
layer.alert(data.message, {
53+
title: '初始化失败!'
54+
});
55+
}
56+
});
57+
return false;
58+
});
3359
});
34-
}
35-
else
36-
{
37-
layer.alert(data.message, {
38-
title: '初始化失败!'
39-
});
40-
}
41-
});
42-
return false;
43-
});
44-
});
4560
</script>

0 commit comments

Comments
 (0)