Skip to content

Commit

Permalink
feat: 一些细节优化
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jun 18, 2024
1 parent eb2c6ca commit e0f642c
Show file tree
Hide file tree
Showing 24 changed files with 98 additions and 137 deletions.
31 changes: 24 additions & 7 deletions app/http/controllers/plugins/mysql_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,9 @@ func (r *MySQLController) Load(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusUnprocessableEntity, "MySQL root密码为空")
}

status, err := tools.ServiceStatus("mysqld")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL状态失败")
}
status, _ := tools.ServiceStatus("mysqld")
if !status {
return controllers.Error(ctx, http.StatusInternalServerError, "MySQL 未运行")
return controllers.Success(ctx, []types.NV{})
}

raw, err := tools.Exec("/www/server/mysql/bin/mysqladmin -uroot -p" + rootPassword + " extended-status 2>&1")
Expand Down Expand Up @@ -220,10 +217,20 @@ func (r *MySQLController) DatabaseList(ctx http.Context) http.Response {

db, err := sql.Open("mysql", "root:"+rootPassword+"@unix(/tmp/mysql.sock)/")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
return controllers.Success(ctx, http.Json{
"total": 0,
"items": []database{},
})
}
defer db.Close()

if err = db.Ping(); err != nil {
return controllers.Success(ctx, http.Json{
"total": 0,
"items": []database{},
})
}

rows, err := db.Query("SHOW DATABASES")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
Expand Down Expand Up @@ -432,10 +439,20 @@ func (r *MySQLController) UserList(ctx http.Context) http.Response {
rootPassword := r.setting.Get(models.SettingKeyMysqlRootPassword)
db, err := sql.Open("mysql", "root:"+rootPassword+"@unix(/tmp/mysql.sock)/")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
return controllers.Success(ctx, http.Json{
"total": 0,
"items": []user{},
})
}
defer db.Close()

if err = db.Ping(); err != nil {
return controllers.Success(ctx, http.Json{
"total": 0,
"items": []user{},
})
}

rows, err := db.Query("SELECT user, host FROM mysql.user")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
Expand Down
27 changes: 12 additions & 15 deletions app/http/controllers/plugins/openresty_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/TheTNB/panel/app/http/controllers"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)

type OpenRestyController struct {
Expand Down Expand Up @@ -112,21 +113,17 @@ func (r *OpenRestyController) Load(ctx http.Context) http.Response {
client := resty.New().SetTimeout(10 * time.Second)
resp, err := client.R().Get("http://127.0.0.1/nginx_status")
if err != nil || !resp.IsSuccess() {
return controllers.Error(ctx, http.StatusInternalServerError, "获取负载失败")
return controllers.Success(ctx, []types.NV{})
}

raw := resp.String()
type nginxStatus struct {
Name string `json:"name"`
Value string `json:"value"`
}
var data []nginxStatus
var data []types.NV

workers, err := tools.Exec("ps aux | grep nginx | grep 'worker process' | wc -l")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取负载失败")
}
data = append(data, nginxStatus{
data = append(data, types.NV{
Name: "工作进程",
Value: workers,
})
Expand All @@ -136,46 +133,46 @@ func (r *OpenRestyController) Load(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, "获取负载失败")
}
mem := tools.FormatBytes(cast.ToFloat64(out))
data = append(data, nginxStatus{
data = append(data, types.NV{
Name: "内存占用",
Value: mem,
})

match := regexp.MustCompile(`Active connections:\s+(\d+)`).FindStringSubmatch(raw)
if len(match) == 2 {
data = append(data, nginxStatus{
data = append(data, types.NV{
Name: "活跃连接数",
Value: match[1],
})
}

match = regexp.MustCompile(`server accepts handled requests\s+(\d+)\s+(\d+)\s+(\d+)`).FindStringSubmatch(raw)
if len(match) == 4 {
data = append(data, nginxStatus{
data = append(data, types.NV{
Name: "总连接次数",
Value: match[1],
})
data = append(data, nginxStatus{
data = append(data, types.NV{
Name: "总握手次数",
Value: match[2],
})
data = append(data, nginxStatus{
data = append(data, types.NV{
Name: "总请求次数",
Value: match[3],
})
}

match = regexp.MustCompile(`Reading:\s+(\d+)\s+Writing:\s+(\d+)\s+Waiting:\s+(\d+)`).FindStringSubmatch(raw)
if len(match) == 4 {
data = append(data, nginxStatus{
data = append(data, types.NV{
Name: "请求数",
Value: match[1],
})
data = append(data, nginxStatus{
data = append(data, types.NV{
Name: "响应数",
Value: match[2],
})
data = append(data, nginxStatus{
data = append(data, types.NV{
Name: "驻留进程",
Value: match[3],
})
Expand Down
50 changes: 30 additions & 20 deletions app/http/controllers/plugins/postgresql_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,9 @@ func (r *PostgreSQLController) SaveUserConfig(ctx http.Context) http.Response {

// Load 获取负载
func (r *PostgreSQLController) Load(ctx http.Context) http.Response {
status, err := tools.ServiceStatus("postgresql")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL状态失败")
}
status, _ := tools.ServiceStatus("postgresql")
if !status {
return controllers.Error(ctx, http.StatusInternalServerError, "PostgreSQL已停止运行")
return controllers.Success(ctx, []types.NV{})
}

time, err := tools.Exec(`echo "select pg_postmaster_start_time();" | su - postgres -c "psql" | sed -n 3p | cut -d'.' -f1`)
Expand Down Expand Up @@ -148,12 +145,25 @@ func (r *PostgreSQLController) ClearLog(ctx http.Context) http.Response {

// DatabaseList 获取数据库列表
func (r *PostgreSQLController) DatabaseList(ctx http.Context) http.Response {
type database struct {
Name string `json:"name"`
Owner string `json:"owner"`
Encoding string `json:"encoding"`
}

db, err := sql.Open("postgres", "host=localhost port=5432 user=postgres dbname=postgres sslmode=disable")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
return controllers.Success(ctx, http.Json{
"total": 0,
"items": []database{},
})
}

if err = db.Ping(); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
return controllers.Success(ctx, http.Json{
"total": 0,
"items": []database{},
})
}

query := `
Expand All @@ -167,12 +177,6 @@ func (r *PostgreSQLController) DatabaseList(ctx http.Context) http.Response {
}
defer rows.Close()

type database struct {
Name string `json:"name"`
Owner string `json:"owner"`
Encoding string `json:"encoding"`
}

var databases []database
for rows.Next() {
var db database
Expand Down Expand Up @@ -370,12 +374,23 @@ func (r *PostgreSQLController) RestoreBackup(ctx http.Context) http.Response {

// RoleList 角色列表
func (r *PostgreSQLController) RoleList(ctx http.Context) http.Response {
type role struct {
Role string `json:"role"`
Attributes []string `json:"attributes"`
}

db, err := sql.Open("postgres", "host=localhost port=5432 user=postgres dbname=postgres sslmode=disable")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
return controllers.Success(ctx, http.Json{
"total": 0,
"items": []role{},
})
}
if err = db.Ping(); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
return controllers.Success(ctx, http.Json{
"total": 0,
"items": []role{},
})
}

query := `
Expand All @@ -394,11 +409,6 @@ func (r *PostgreSQLController) RoleList(ctx http.Context) http.Response {
}
defer rows.Close()

type role struct {
Role string `json:"role"`
Attributes []string `json:"attributes"`
}

var roles []role
for rows.Next() {
var r role
Expand Down
31 changes: 3 additions & 28 deletions app/http/requests/cert/cert_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package requests
import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
"github.com/spf13/cast"
)

type CertStore struct {
Type string `form:"type" json:"type"`
Domains []string `form:"domains" json:"domains"`
AutoRenew bool `form:"auto_renew" json:"auto_renew"`
UserID uint `form:"user_id" json:"user_id"`
DNSID uint `form:"dns_id" json:"dns_id"`
WebsiteID uint `form:"website_id" json:"website_id"`
UserID uint `form:"user_id" json:"user_id" filter:"uint"`
DNSID uint `form:"dns_id" json:"dns_id" filter:"uint"`
WebsiteID uint `form:"website_id" json:"website_id" filter:"uint"`
}

func (r *CertStore) Authorize(ctx http.Context) error {
Expand All @@ -39,29 +38,5 @@ func (r *CertStore) Attributes(ctx http.Context) map[string]string {
}

func (r *CertStore) PrepareForValidation(ctx http.Context, data validation.Data) error {
// TODO 由于验证器 filter 标签的问题,暂时这里这样处理
userID, exist := data.Get("user_id")
if exist {
err := data.Set("user_id", cast.ToUint(userID))
if err != nil {
return err
}
}
dnsID, exist := data.Get("dns_id")
if exist {
err := data.Set("dns_id", cast.ToUint(dnsID))
if err != nil {
return err
}

}
websiteID, exist := data.Get("website_id")
if exist {
err := data.Set("website_id", cast.ToUint(websiteID))
if err != nil {
return err
}
}

return nil
}
31 changes: 3 additions & 28 deletions app/http/requests/cert/cert_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ package requests
import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
"github.com/spf13/cast"
)

type CertUpdate struct {
ID uint `form:"id" json:"id"`
Type string `form:"type" json:"type"`
Domains []string `form:"domains" json:"domains"`
AutoRenew bool `form:"auto_renew" json:"auto_renew"`
UserID uint `form:"user_id" json:"user_id"`
DNSID uint `form:"dns_id" json:"dns_id"`
WebsiteID uint `form:"website_id" json:"website_id"`
UserID uint `form:"user_id" json:"user_id" filter:"uint"`
DNSID uint `form:"dns_id" json:"dns_id" filter:"uint"`
WebsiteID uint `form:"website_id" json:"website_id" filter:"uint"`
}

func (r *CertUpdate) Authorize(ctx http.Context) error {
Expand Down Expand Up @@ -41,29 +40,5 @@ func (r *CertUpdate) Attributes(ctx http.Context) map[string]string {
}

func (r *CertUpdate) PrepareForValidation(ctx http.Context, data validation.Data) error {
// TODO 由于验证器 filter 标签的问题,暂时这里这样处理
userID, exist := data.Get("user_id")
if exist {
err := data.Set("user_id", cast.ToUint(userID))
if err != nil {
return err
}
}
dnsID, exist := data.Get("dns_id")
if exist {
err := data.Set("dns_id", cast.ToUint(dnsID))
if err != nil {
return err
}

}
websiteID, exist := data.Get("website_id")
if exist {
err := data.Set("website_id", cast.ToUint(websiteID))
if err != nil {
return err
}
}

return nil
}
4 changes: 2 additions & 2 deletions app/http/requests/file/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func (r *Archive) Authorize(ctx http.Context) error {
func (r *Archive) Rules(ctx http.Context) map[string]string {
return map[string]string{
"paths": "array",
"paths.*": `regex:^/[a-zA-Z0-9_.@#$%\-\s\[\]()]+(/[a-zA-Z0-9_.@#$%\-\s\[\]()]+)*$|path_exists`,
"file": `regex:^/[a-zA-Z0-9_.@#$%\-\s\[\]()]+(/[a-zA-Z0-9_.@#$%\-\s\[\]()]+)*$|path_not_exists`,
"paths.*": `regex:^/.*$|path_exists`,
"file": `regex:^/.*$|path_not_exists`,
}
}

Expand Down
4 changes: 2 additions & 2 deletions app/http/requests/file/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func (r *Copy) Authorize(ctx http.Context) error {

func (r *Copy) Rules(ctx http.Context) map[string]string {
return map[string]string{
"source": `regex:^/[a-zA-Z0-9_.@#$%\-\s\[\]()]+(/[a-zA-Z0-9_.@#$%\-\s\[\]()]+)*$|path_exists`,
"target": `regex:^/[a-zA-Z0-9_.@#$%\-\s\[\]()]+(/[a-zA-Z0-9_.@#$%\-\s\[\]()]+)*$`,
"source": `regex:^/.*$|path_exists`,
"target": `regex:^/.*$`,
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/http/requests/file/exist.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (r *Exist) Authorize(ctx http.Context) error {

func (r *Exist) Rules(ctx http.Context) map[string]string {
return map[string]string{
"path": `regex:^/([a-zA-Z0-9_.@#$%\-\s\[\]()]+(/[a-zA-Z0-9_.@#$%\-\s\[\]()]+)*)?$|path_exists`,
"path": `regex:^/.*$|path_exists`,
}
}

Expand Down
4 changes: 2 additions & 2 deletions app/http/requests/file/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func (r *Move) Authorize(ctx http.Context) error {

func (r *Move) Rules(ctx http.Context) map[string]string {
return map[string]string{
"source": `regex:^/[a-zA-Z0-9_.@#$%\-\s\[\]()]+(/[a-zA-Z0-9_.@#$%\-\s\[\]()]+)*$|path_exists`,
"target": `regex:^/[a-zA-Z0-9_.@#$%\-\s\[\]()]+(/[a-zA-Z0-9_.@#$%\-\s\[\]()]+)*$`,
"source": `regex:^/.*$|path_exists`,
"target": `regex:^/.*$`,
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/http/requests/file/not_exist.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (r *NotExist) Authorize(ctx http.Context) error {

func (r *NotExist) Rules(ctx http.Context) map[string]string {
return map[string]string{
"path": `regex:^/[a-zA-Z0-9_.@#$%\-\s\[\]()]+(/[a-zA-Z0-9_.@#$%\-\s\[\]()]+)*$|path_not_exists`,
"path": `regex:^/.*$|path_not_exists`,
}
}

Expand Down
Loading

0 comments on commit e0f642c

Please sign in to comment.