Skip to content

Commit

Permalink
fix: 首页mysql数据库数量不显示
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 24, 2024
1 parent ffb7bfd commit a2b30b0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 64 deletions.
4 changes: 2 additions & 2 deletions internal/data/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func (r *backupRepo) createMySQL(to string, name string) error {
if err != nil {
return err
}
mysql, err := db.NewMySQL("root", rootPassword, "/tmp/mysql.sock")
mysql, err := db.NewMySQL("root", rootPassword, "/tmp/mysql.sock", "unix")
if err != nil {
return err
}
Expand Down Expand Up @@ -401,7 +401,7 @@ func (r *backupRepo) restoreMySQL(backup, target string) error {
if err != nil {
return err
}
mysql, err := db.NewMySQL("root", rootPassword, "/tmp/mysql.sock")
mysql, err := db.NewMySQL("root", rootPassword, "/tmp/mysql.sock", "unix")
if err != nil {
return err
}
Expand Down
68 changes: 14 additions & 54 deletions internal/service/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,79 +124,39 @@ func (s *DashboardService) CountInfo(w http.ResponseWriter, r *http.Request) {
return
}

mysqlInstalled, _ := s.appRepo.IsInstalled("slug like ?", "mysql%")
postgresqlInstalled, _ := s.appRepo.IsInstalled("slug like ?", "postgresql%")
mysqlInstalled, _ := s.appRepo.IsInstalled("slug = ?", "mysql")
postgresqlInstalled, _ := s.appRepo.IsInstalled("slug = ?", "postgresql")

type database struct {
Name string `json:"name"`
}
var databaseCount int64
var databaseCount int
if mysqlInstalled {
rootPassword, _ := s.settingRepo.Get(biz.SettingKeyMySQLRootPassword)
mysql, err := db.NewMySQL("root", rootPassword, "/tmp/mysql.sock")
mysql, err := db.NewMySQL("root", rootPassword, "/tmp/mysql.sock", "unix")
if err == nil {
defer mysql.Close()
if err = mysql.Ping(); err != nil {
databaseCount = -1
} else {
rows, err := mysql.Query("SHOW DATABASES")
if err != nil {
databaseCount = -1
} else {
defer rows.Close()
var databases []database
for rows.Next() {
var d database
if err := rows.Scan(&d.Name); err != nil {
continue
}
if d.Name == "information_schema" || d.Name == "performance_schema" || d.Name == "mysql" || d.Name == "sys" {
continue
}

databases = append(databases, d)
}
databaseCount = int64(len(databases))
}
databases, err := mysql.Databases()
if err == nil {
databaseCount += len(databases)
}
}
}
if postgresqlInstalled {
postgres, err := db.NewPostgres("postgres", "", "127.0.0.1", fmt.Sprintf("%s/server/postgresql/data/pg_hba.conf", app.Root), 5432)
if err == nil {
defer postgres.Close()
if err = postgres.Ping(); err != nil {
databaseCount = -1
} else {
rows, err := postgres.Query("SELECT datname FROM pg_database WHERE datistemplate = false")
if err != nil {
databaseCount = -1
} else {
defer rows.Close()
var databases []database
for rows.Next() {
var d database
if err = rows.Scan(&d.Name); err != nil {
continue
}
if d.Name == "postgres" || d.Name == "template0" || d.Name == "template1" {
continue
}
databases = append(databases, d)
}
databaseCount = int64(len(databases))
}
databases, err := postgres.Databases()
if err == nil {
databaseCount += len(databases)
}
}
}

var ftpCount int64
var ftpCount int
ftpInstalled, _ := s.appRepo.IsInstalled("slug = ?", "pureftpd")
if ftpInstalled {
listRaw, err := shell.Execf("pure-pw list")
if len(listRaw) != 0 && err == nil {
listArr := strings.Split(listRaw, "\n")
ftpCount = int64(len(listArr))
ftpCount = len(listArr)
}
}

Expand All @@ -214,8 +174,8 @@ func (s *DashboardService) CountInfo(w http.ResponseWriter, r *http.Request) {
}

func (s *DashboardService) InstalledDbAndPhp(w http.ResponseWriter, r *http.Request) {
mysqlInstalled, _ := s.appRepo.IsInstalled("slug like ?", "mysql%")
postgresqlInstalled, _ := s.appRepo.IsInstalled("slug like ?", "postgresql%")
mysqlInstalled, _ := s.appRepo.IsInstalled("slug = ?", "mysql")
postgresqlInstalled, _ := s.appRepo.IsInstalled("slug = ?", "postgresql")
php, _ := s.appRepo.GetInstalledAll("slug like ?", "php%")

var phpData []types.LVInt
Expand Down
10 changes: 7 additions & 3 deletions pkg/db/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"database/sql"
"fmt"
"slices"

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

Expand All @@ -23,10 +24,10 @@ func NewMySQL(username, password, address string, typ ...string) (*MySQL, error)
}
db, err := sql.Open("mysql", dsn)
if err != nil {
return nil, fmt.Errorf("初始化MySQL连接失败: %w", err)
return nil, fmt.Errorf("init mysql connection failed: %w", err)
}
if db.Ping() != nil {
return nil, fmt.Errorf("连接MySQL失败: %w", err)
if err = db.Ping(); err != nil {
return nil, fmt.Errorf("connect to mysql failed: %w", err)
}
return &MySQL{
db: db,
Expand Down Expand Up @@ -168,6 +169,9 @@ func (m *MySQL) Databases() ([]types.MySQLDatabase, error) {
if err := rows.Scan(&database); err != nil {
continue
}
if slices.Contains([]string{"information_schema", "performance_schema", "mysql", "sys"}, database) {
continue
}
databases = append(databases, types.MySQLDatabase{
Name: database,
})
Expand Down
6 changes: 3 additions & 3 deletions pkg/db/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ func NewPostgres(username, password, address, hbaFile string, port uint) (*Postg
}
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, fmt.Errorf("初始化Postgres连接失败: %w", err)
return nil, fmt.Errorf("init postgres connection failed: %w", err)
}
if db.Ping() != nil {
return nil, fmt.Errorf("连接Postgres失败: %w", err)
if err = db.Ping(); err != nil {
return nil, fmt.Errorf("connect to postgres failed: %w", err)
}
return &Postgres{
db: db,
Expand Down
2 changes: 1 addition & 1 deletion web/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"placeholder": "admin"
},
"email": {
"label": "Email (may be useful later)",
"label": "Certificate default email",
"placeholder": "admin{'@'}example.com"
},
"port": {
Expand Down
2 changes: 1 addition & 1 deletion web/src/i18n/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"placeholder": "admin"
},
"email": {
"label": "邮箱(以后可能会有用)",
"label": "证书默认邮箱",
"placeholder": "admin{'@'}example.com"
},
"port": {
Expand Down

0 comments on commit a2b30b0

Please sign in to comment.