Skip to content

Commit

Permalink
增加sqlite数据库选项
Browse files Browse the repository at this point in the history
  • Loading branch information
baichachaa committed Jan 31, 2023
1 parent 59697f9 commit eaa1bf1
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 25 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
# rustdesk-api-server
RustDesk Api 服务器端 Go语言版本
RustDesk Api 服务器端 Go语言版本,支持sqlite3、mysql数据库

登录后可返回同账号下所有在用主机 以本号归属开头

[![Build Release](https://github.com/xiaoyi510/rustdesk-api-server/actions/workflows/build.yml/badge.svg)](https://github.com/xiaoyi510/rustdesk-api-server/actions/workflows/build.yml)

## 编译
安装Golang

安装GCC并配置PATH

## 使用方法


### 修改数据库连接
修改 conf/config.yml 中的配置项
```yaml
dbtype: mysql # 支持mysql或者sqlite3
mysql:
host: '127.0.0.1'
port: 3306
Expand Down
23 changes: 20 additions & 3 deletions app/services/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,29 @@ func (t *TokenService) Login(user *models.User, clientId, uuid, token2 string) b
LoginTime: time.Now().Unix(),
ExpireTime: time.Now().Unix() + 3600,
}
update, err := m.InsertOrUpdate(&md, "uid,client_id,uuid")
//update, err := m.InsertOrUpdate(&md, "uid,client_id,uuid")
// `sqlite3` nonsupport InsertOrUpdate in beego
// 此orm不支持sqlite3执行 InsertOrUpdate, 拆改两步完成
oldMd := models.Token{Uid: user.Id, ClientId: clientId, Uuid: uuid}
_ = m.Read(&oldMd, "uid", "client_id", "uuid")
rowId := int64(0)
var err error
// 存在主键更新
if oldMd.Id != 0 {
md.Id = oldMd.Id
rowId, err = m.Update(&md)
if err != nil {
return false
}
}
//不存在主键插入
rowId, err = m.Insert(&md)
if err != nil {
return false
}
if update > 0 {
log.Println("tokenUpdate", update)

if rowId > 0 {
log.Println("tokenUpdate", rowId)
}
return true
}
Expand Down
3 changes: 2 additions & 1 deletion boot/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func init() {
_, err := os.Stat(config)
if err != nil && os.IsNotExist(err) {
// 配置文件不存在
err := os.WriteFile(config, []byte(`mysql:
err := os.WriteFile(config, []byte(`dbtype: 'sqlite3'
mysql:
host: '127.0.0.1'
port: 3306
database: 'rustdesk'
Expand Down
55 changes: 37 additions & 18 deletions boot/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,49 @@ import (
"github.com/beego/beego/v2/client/orm"
"github.com/beego/beego/v2/core/logs"
_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
"rustdesk-api-server/global"
)

// 注册Mysql驱动
func init() {
orm.Debug = false
err := orm.RegisterDriver("mysql", orm.DRMySQL)
if err != nil {
logs.Error("mysql 注册驱动失败:", err)
}
orm.Debug = true

if global.ConfigVar.DBType == "mysql" {
logs.Info("数据库注册类型 mysql")

err := orm.RegisterDriver("mysql", orm.DRMySQL)
if err != nil {
logs.Error("mysql 注册驱动失败:", err)
}

// 格式化连接符
connStr := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8",
global.ConfigVar.Mysql.Username,
global.ConfigVar.Mysql.Password,
global.ConfigVar.Mysql.Host,
global.ConfigVar.Mysql.Port,
global.ConfigVar.Mysql.Database,
)

// 注册链接数据库
err = orm.RegisterDataBase("default", "mysql", connStr)
if err != nil {
logs.Error("mysql 数据库注册失败", err)
}

} else {
logs.Info("数据库注册类型 sqlite3")

err := orm.RegisterDriver("sqlite", orm.DRSqlite)
if err != nil {
logs.Error("sqlite3 注册驱动失败:", err)
}

// 格式化连接符
connStr := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8",
global.ConfigVar.Mysql.Username,
global.ConfigVar.Mysql.Password,
global.ConfigVar.Mysql.Host,
global.ConfigVar.Mysql.Port,
global.ConfigVar.Mysql.Database,
)

// 注册链接数据库
err = orm.RegisterDataBase("default", "mysql", connStr)
if err != nil {
logs.Error("mysql数据库注册失败", err)
err = orm.RegisterDataBase("default", "sqlite3", "sqlite3.db")
if err != nil {
logs.Error("sqlite3 数据库注册失败", err)
}
}

}
3 changes: 3 additions & 0 deletions conf/app.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# 应用名称
appname = rustdesk-api-server

# 监听IP
HTTPAddr = 127.0.0.1

# 端口号
httpport = 21114

Expand Down
5 changes: 3 additions & 2 deletions global/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package global
import "rustdesk-api-server/global/confDto"

type Config struct {
Mysql confDto.Mysql `json:"mysql"`
App confDto.AppConfig `json:"app"`
DBType string `json:"dbtype"`
Mysql confDto.Mysql `json:"mysql"`
App confDto.AppConfig `json:"app"`
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/fsnotify/fsnotify v1.6.0
github.com/go-sql-driver/mysql v1.6.0
github.com/mattn/go-sqlite3 v1.14.7
github.com/spf13/viper v1.14.0
)

Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ github.com/lib/pq v1.10.5 h1:J+gdV2cUmX7ZqL2B0lFcW0m+egaHC2V3lpO8nWxyYiQ=
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA=
github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand Down

0 comments on commit eaa1bf1

Please # to comment.