mirror of
https://github.com/netfun2000/rttys_zhaojh329.git
synced 2026-02-27 09:53:24 +08:00
Adjust the database configuration format of the configuration file to support the selection of MySQL and SQLite3. (#88)
Signed-off-by: zhou_jiajian <zhou_0611@163.com>
This commit is contained in:
+24
@@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* @Author: 周家建
|
||||||
|
* @Mail: zhou_0611@163.com
|
||||||
|
* @Date: 2021-07-27 19:02:39
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"database/sql"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func instanceDB(str string) (*sql.DB, error) {
|
||||||
|
sp:= strings.Split(str, "://")
|
||||||
|
if len(sp) == 2 {
|
||||||
|
return sql.Open(sp[0], sp[1])
|
||||||
|
} else {
|
||||||
|
return sql.Open("mysql", str)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"database/sql"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@@ -94,7 +93,7 @@ func (dev *device) Close() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (dev *device) UpdateDb() {
|
func (dev *device) UpdateDb() {
|
||||||
db, err := sql.Open("mysql", dev.br.cfg.DB)
|
db, err := instanceDB(dev.br.cfg.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Msg(err.Error())
|
log.Error().Msg(err.Error())
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ require (
|
|||||||
github.com/dwdcth/consoleEx v0.0.0-20180521133551-f56f6eb78b76
|
github.com/dwdcth/consoleEx v0.0.0-20180521133551-f56f6eb78b76
|
||||||
github.com/gin-gonic/gin v1.5.0
|
github.com/gin-gonic/gin v1.5.0
|
||||||
github.com/go-sql-driver/mysql v1.6.0
|
github.com/go-sql-driver/mysql v1.6.0
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.8
|
||||||
github.com/gorilla/websocket v1.4.1
|
github.com/gorilla/websocket v1.4.1
|
||||||
github.com/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c
|
github.com/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c
|
||||||
github.com/json-iterator/go v1.1.9
|
github.com/json-iterator/go v1.1.9
|
||||||
|
|||||||
@@ -36,6 +36,8 @@ github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVc
|
|||||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||||
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
|
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
|
||||||
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
|
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.8 h1:gDp86IdQsN/xWjIEmr9MF6o9mpksUgh0fu+9ByFxzIU=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.8/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
|
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ func httpLogin(cfg *config.Config, creds *credentials) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := sql.Open("mysql", cfg.DB)
|
db, err := instanceDB(cfg.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Msg(err.Error())
|
log.Error().Msg(err.Error())
|
||||||
return false
|
return false
|
||||||
@@ -87,7 +87,7 @@ func isAdminUsername(cfg *config.Config, username string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := sql.Open("mysql", cfg.DB)
|
db, err := instanceDB(cfg.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Msg(err.Error())
|
log.Error().Msg(err.Error())
|
||||||
return false
|
return false
|
||||||
@@ -138,7 +138,7 @@ func httpStart(br *broker) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
authorized.GET("/fontsize", func(c *gin.Context) {
|
authorized.GET("/fontsize", func(c *gin.Context) {
|
||||||
db, err := sql.Open("mysql", cfg.DB)
|
db, err := instanceDB(cfg.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Msg(err.Error())
|
log.Error().Msg(err.Error())
|
||||||
c.Status(http.StatusInternalServerError)
|
c.Status(http.StatusInternalServerError)
|
||||||
@@ -170,7 +170,7 @@ func httpStart(br *broker) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := sql.Open("mysql", cfg.DB)
|
db, err := instanceDB(cfg.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Msg(err.Error())
|
log.Error().Msg(err.Error())
|
||||||
c.Status(http.StatusInternalServerError)
|
c.Status(http.StatusInternalServerError)
|
||||||
@@ -217,7 +217,7 @@ func httpStart(br *broker) {
|
|||||||
Online bool `json:"online"`
|
Online bool `json:"online"`
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := sql.Open("mysql", cfg.DB)
|
db, err := instanceDB(cfg.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Msg(err.Error())
|
log.Error().Msg(err.Error())
|
||||||
c.Status(http.StatusInternalServerError)
|
c.Status(http.StatusInternalServerError)
|
||||||
@@ -350,7 +350,7 @@ func httpStart(br *broker) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := sql.Open("mysql", cfg.DB)
|
db, err := instanceDB(cfg.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Msg(err.Error())
|
log.Error().Msg(err.Error())
|
||||||
c.Status(http.StatusInternalServerError)
|
c.Status(http.StatusInternalServerError)
|
||||||
@@ -405,7 +405,7 @@ func httpStart(br *broker) {
|
|||||||
|
|
||||||
users := []string{}
|
users := []string{}
|
||||||
|
|
||||||
db, err := sql.Open("mysql", cfg.DB)
|
db, err := instanceDB(cfg.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Msg(err.Error())
|
log.Error().Msg(err.Error())
|
||||||
c.Status(http.StatusInternalServerError)
|
c.Status(http.StatusInternalServerError)
|
||||||
@@ -460,7 +460,7 @@ func httpStart(br *broker) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := sql.Open("mysql", cfg.DB)
|
db, err := instanceDB(cfg.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Msg(err.Error())
|
log.Error().Msg(err.Error())
|
||||||
return
|
return
|
||||||
@@ -502,7 +502,7 @@ func httpStart(br *broker) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := sql.Open("mysql", cfg.DB)
|
db, err := instanceDB(cfg.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Msg(err.Error())
|
log.Error().Msg(err.Error())
|
||||||
return
|
return
|
||||||
@@ -529,7 +529,7 @@ func httpStart(br *broker) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := sql.Open("mysql", cfg.DB)
|
db, err := instanceDB(cfg.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Msg(err.Error())
|
log.Error().Msg(err.Error())
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -14,10 +13,11 @@ import (
|
|||||||
"github.com/zhaojh329/rttys/version"
|
"github.com/zhaojh329/rttys/version"
|
||||||
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initDb(cfg *config.Config) error {
|
func initDb(cfg *config.Config) error {
|
||||||
db, err := sql.Open("mysql", cfg.DB)
|
db, err := instanceDB(cfg.DB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -20,4 +20,5 @@
|
|||||||
#white-list: rtty1 rtty2
|
#white-list: rtty1 rtty2
|
||||||
|
|
||||||
# mysql database source
|
# mysql database source
|
||||||
#db: rttys:rttys@tcp(localhost)/rttys
|
#db: mysql://rttys:rttys@tcp(localhost)/rttys
|
||||||
|
db: sqlite3://my.db
|
||||||
|
|||||||
Reference in New Issue
Block a user