Skip to content

Commit

Permalink
Adjust the database configuration format of the configuration file to…
Browse files Browse the repository at this point in the history
… support the selection of MySQL and SQLite3. (#88)

Signed-off-by: zhou_jiajian <[email protected]>
  • Loading branch information
richard611 authored Jul 29, 2021
1 parent 9099061 commit 9527ae7
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 15 deletions.
24 changes: 24 additions & 0 deletions database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* @Author: 周家建
* @Mail: [email protected]
* @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)
}
}
3 changes: 1 addition & 2 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"crypto/rand"
"crypto/tls"
"crypto/x509"
"database/sql"
"encoding/binary"
"io"
"io/ioutil"
Expand Down Expand Up @@ -94,7 +93,7 @@ func (dev *device) Close() {
}

func (dev *device) UpdateDb() {
db, err := sql.Open("mysql", dev.br.cfg.DB)
db, err := instanceDB(dev.br.cfg.DB)
if err != nil {
log.Error().Msg(err.Error())
return
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/dwdcth/consoleEx v0.0.0-20180521133551-f56f6eb78b76
github.com/gin-gonic/gin v1.5.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/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c
github.com/json-iterator/go v1.1.9
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
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/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
Expand Down
20 changes: 10 additions & 10 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func httpLogin(cfg *config.Config, creds *credentials) bool {
return false
}

db, err := sql.Open("mysql", cfg.DB)
db, err := instanceDB(cfg.DB)
if err != nil {
log.Error().Msg(err.Error())
return false
Expand Down Expand Up @@ -87,7 +87,7 @@ func isAdminUsername(cfg *config.Config, username string) bool {
return false
}

db, err := sql.Open("mysql", cfg.DB)
db, err := instanceDB(cfg.DB)
if err != nil {
log.Error().Msg(err.Error())
return false
Expand Down Expand Up @@ -138,7 +138,7 @@ func httpStart(br *broker) {
})

authorized.GET("/fontsize", func(c *gin.Context) {
db, err := sql.Open("mysql", cfg.DB)
db, err := instanceDB(cfg.DB)
if err != nil {
log.Error().Msg(err.Error())
c.Status(http.StatusInternalServerError)
Expand Down Expand Up @@ -170,7 +170,7 @@ func httpStart(br *broker) {
return
}

db, err := sql.Open("mysql", cfg.DB)
db, err := instanceDB(cfg.DB)
if err != nil {
log.Error().Msg(err.Error())
c.Status(http.StatusInternalServerError)
Expand Down Expand Up @@ -217,7 +217,7 @@ func httpStart(br *broker) {
Online bool `json:"online"`
}

db, err := sql.Open("mysql", cfg.DB)
db, err := instanceDB(cfg.DB)
if err != nil {
log.Error().Msg(err.Error())
c.Status(http.StatusInternalServerError)
Expand Down Expand Up @@ -350,7 +350,7 @@ func httpStart(br *broker) {
return
}

db, err := sql.Open("mysql", cfg.DB)
db, err := instanceDB(cfg.DB)
if err != nil {
log.Error().Msg(err.Error())
c.Status(http.StatusInternalServerError)
Expand Down Expand Up @@ -405,7 +405,7 @@ func httpStart(br *broker) {

users := []string{}

db, err := sql.Open("mysql", cfg.DB)
db, err := instanceDB(cfg.DB)
if err != nil {
log.Error().Msg(err.Error())
c.Status(http.StatusInternalServerError)
Expand Down Expand Up @@ -460,7 +460,7 @@ func httpStart(br *broker) {
return
}

db, err := sql.Open("mysql", cfg.DB)
db, err := instanceDB(cfg.DB)
if err != nil {
log.Error().Msg(err.Error())
return
Expand Down Expand Up @@ -502,7 +502,7 @@ func httpStart(br *broker) {
return
}

db, err := sql.Open("mysql", cfg.DB)
db, err := instanceDB(cfg.DB)
if err != nil {
log.Error().Msg(err.Error())
return
Expand All @@ -529,7 +529,7 @@ func httpStart(br *broker) {
return
}

db, err := sql.Open("mysql", cfg.DB)
db, err := instanceDB(cfg.DB)
if err != nil {
log.Error().Msg(err.Error())
return
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"database/sql"
"fmt"
"os"
"runtime"
Expand All @@ -14,10 +13,11 @@ import (
"github.com/zhaojh329/rttys/version"

_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
)

func initDb(cfg *config.Config) error {
db, err := sql.Open("mysql", cfg.DB)
db, err := instanceDB(cfg.DB)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion rttys.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
#white-list: rtty1 rtty2

# mysql database source
#db: rttys:rttys@tcp(localhost)/rttys
#db: mysql://rttys:rttys@tcp(localhost)/rttys
db: sqlite3://my.db

0 comments on commit 9527ae7

Please sign in to comment.