2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 07:57:21 +08:00

feat: 数据库管理提交1

This commit is contained in:
耗子
2024-11-22 02:58:31 +08:00
parent 39d2eee676
commit a96bbf3dfb
17 changed files with 764 additions and 65 deletions

View File

@@ -6,8 +6,6 @@ import (
_ "github.com/lib/pq"
"github.com/TheTNB/panel/pkg/io"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/types"
)
@@ -17,13 +15,15 @@ type Postgres struct {
username string
password string
address string
hbaFile string
port uint
}
func NewPostgres(username, password, address string, port uint, hbaFile string) (*Postgres, error) {
func NewPostgres(username, password, address string, port uint) (*Postgres, error) {
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=postgres sslmode=disable", address, port, username, password)
if password == "" {
if username == "" {
username = "postgres"
}
dsn = fmt.Sprintf("host=%s port=%d user=%s dbname=postgres sslmode=disable", address, port, username)
}
db, err := sql.Open("postgres", dsn)
@@ -38,7 +38,6 @@ func NewPostgres(username, password, address string, port uint, hbaFile string)
username: username,
password: password,
address: address,
hbaFile: hbaFile,
port: port,
}, nil
}
@@ -109,7 +108,6 @@ func (m *Postgres) UserDrop(user string) error {
return err
}
_, _ = shell.Execf(`sed -i '/%s/d' %s`, user, m.hbaFile)
return systemctl.Reload("postgresql")
}
@@ -134,24 +132,6 @@ func (m *Postgres) PrivilegesRevoke(user, database string) error {
return err
}
func (m *Postgres) HostAdd(database, user, host string) error {
config := fmt.Sprintf("host %s %s %s scram-sha-256", database, user, host)
if err := io.WriteAppend(m.hbaFile, config, 0644); err != nil {
return err
}
return systemctl.Reload("postgresql")
}
func (m *Postgres) HostRemove(database, user, host string) error {
regex := fmt.Sprintf(`host\s+%s\s+%s\s+%s`, database, user, host)
if _, err := shell.Execf(`sed -i '/%s/d' %s`, regex, m.hbaFile); err != nil {
return err
}
return systemctl.Reload("postgresql")
}
func (m *Postgres) Users() ([]types.PostgresUser, error) {
query := `
SELECT rolname,