Skip to content

Commit

Permalink
database: use mysql
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
zhaojh329 committed May 2, 2021
1 parent 091a5ba commit 2124254
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 15 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,31 @@ You can stop the service with:

sudo systemctl stop rttys

# Database Preparation

On database instance, login to database console as root:
```
mysql -u root -p
```

Create database user which will be used by Rttys, authenticated by password. This example uses 'rttys' as password. Please use a secure password for your instance.
```
CREATE USER 'rttys' IDENTIFIED BY 'rttys';
```

Create database with UTF-8 charset and collation. Make sure to use utf8mb4 charset instead of utf8 as the former supports all Unicode characters (including emojis) beyond Basic Multilingual Plane. Also, collation chosen depending on your expected content. When in doubt, use either unicode_ci or general_ci.
```
CREATE DATABASE rttys CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';
```

Grant all privileges on the database to database user created above.
```
GRANT ALL PRIVILEGES ON rttys.* TO 'rttys';
FLUSH PRIVILEGES;
```

Quit from database console by exit.

# Contributing
If you would like to help making [rttys](https://github.com/zhaojh329/rttys) better,
see the [CONTRIBUTING.md](https://github.com/zhaojh329/rttys/blob/master/CONTRIBUTING.md) file.
25 changes: 25 additions & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,31 @@

sudo systemctl stop rttys

# 数据库准备

在数据库实例上,以root用户身份登录到数据库控制台:
```
mysql -u root -p
```

创建将由 Rttys 使用的数据库用户,通过密码验证。本例使用 “rttys” 作为密码。请为您的实例使用安全密码。
```
CREATE USER 'rttys' IDENTIFIED BY 'rttys';
```

使用 UTF-8 字符集和排序规则创建数据库。确保使用 utf8mb4 字符集而不是 utf8,因为前者支持基本多语言平面
之外的所有 Unicode字符(包括emojis)。另外,根据您期望的内容选择排序规则。如有疑问,请使用 unicode_ci 或general_ci。
```
CREATE DATABASE rttys CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';
```
将数据库上的所有权限授予上面创建的数据库用户。
```
GRANT ALL PRIVILEGES ON rttys.* TO 'rttys';
FLUSH PRIVILEGES;
```

退出数据库控制台。

# 贡献代码
如果你想帮助[rttys](https://github.com/zhaojh329/rttys)变得更好,请参考
[CONTRIBUTING_ZH.md](https://github.com/zhaojh329/rttys/blob/master/CONTRIBUTING_ZH.md)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ go 1.16
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/gorilla/websocket v1.4.1
github.com/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c
github.com/json-iterator/go v1.1.9
github.com/kylelemons/go-gypsy v0.0.0-20160905020020-08cad365cd28
github.com/mattn/go-colorable v0.1.4
github.com/mattn/go-sqlite3 v1.14.6
github.com/rs/zerolog v1.18.0
github.com/urfave/cli/v2 v2.3.0
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ github.com/go-playground/locales v0.12.1 h1:2FITxuFt/xuCNP1Acdhv62OzaCiviiE4kotf
github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM=
github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rmGrCjJ8eAeUP/K/EKx4DM=
github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand All @@ -34,8 +36,6 @@ 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.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/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
9 changes: 4 additions & 5 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

"github.com/gin-gonic/gin"
_ "github.com/mattn/go-sqlite3"
"github.com/rs/zerolog/log"
"github.com/zhaojh329/rttys/cache"
"github.com/zhaojh329/rttys/config"
Expand Down Expand Up @@ -40,7 +39,7 @@ func httpLogin(cfg *config.Config, creds *credentials) bool {
return false
}

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

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

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

db, err := sql.Open("sqlite3", cfg.DB)
db, err := sql.Open("mysql", cfg.DB)
if err != nil {
log.Error().Msg(err.Error())
c.Status(http.StatusInternalServerError)
Expand Down
20 changes: 15 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,29 @@ import (
rlog "github.com/zhaojh329/rttys/log"
"github.com/zhaojh329/rttys/utils"
"github.com/zhaojh329/rttys/version"

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

func initDb(cfg *config.Config) {
db, err := sql.Open("sqlite3", cfg.DB)
db, err := sql.Open("mysql", cfg.DB)
if err != nil {
log.Error().Msg(err.Error())
return
}
defer db.Close()

db.Exec("CREATE TABLE IF NOT EXISTS config(name TEXT PRIMARY KEY NOT NULL, value TEXT NOT NULL)")
_, err = db.Exec("CREATE TABLE IF NOT EXISTS config(name VARCHAR(512) PRIMARY KEY NOT NULL, value TEXT NOT NULL)")
if err != nil {
log.Error().Msg(err.Error())
return
}

db.Exec("CREATE TABLE IF NOT EXISTS account(username TEXT PRIMARY KEY NOT NULL, password TEXT NOT NULL)")
_, err = db.Exec("CREATE TABLE IF NOT EXISTS account(username VARCHAR(512) PRIMARY KEY NOT NULL, password TEXT NOT NULL)")
if err != nil {
log.Error().Msg(err.Error())
return
}
}

func runRttys(c *cli.Context) {
Expand Down Expand Up @@ -134,8 +144,8 @@ func main() {
},
&cli.StringFlag{
Name: "db",
Value: "rttys.db",
Usage: "sqlite3 database path",
Value: "rttys:rttys@tcp(localhost)/rttys",
Usage: "mysql database source",
},
&cli.BoolFlag{
Name: "local-auth",
Expand Down
4 changes: 2 additions & 2 deletions rttys.conf
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
#white-list: "*"
#white-list: rtty1 rtty2

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

0 comments on commit 2124254

Please sign in to comment.