SetPostgresPassword 设置 postgres 用户密码
(w http.ResponseWriter, r *http.Request)
| 202 | |
| 203 | // SetPostgresPassword 设置 postgres 用户密码 |
| 204 | func (s *App) SetPostgresPassword(w http.ResponseWriter, r *http.Request) { |
| 205 | req, err := service.Bind[SetPostgresPassword](r) |
| 206 | if err != nil { |
| 207 | service.Error(w, http.StatusUnprocessableEntity, "%v", err) |
| 208 | return |
| 209 | } |
| 210 | |
| 211 | oldPassword, _ := s.settingRepo.Get(biz.SettingKeyPostgresPassword) |
| 212 | port := s.getPort() |
| 213 | postgres, err := db.NewPostgres("postgres", oldPassword, "127.0.0.1", port) |
| 214 | if err != nil { |
| 215 | // 直接修改密码 |
| 216 | if _, err = shell.Execf(`su - postgres -c "psql -p %d -c \"ALTER USER postgres WITH PASSWORD '%s';\""`, port, req.Password); err != nil { |
| 217 | service.Error(w, http.StatusInternalServerError, s.t.Get("failed to set postgres password: %v", err)) |
| 218 | return |
| 219 | } |
| 220 | } else { |
| 221 | defer postgres.Close() |
| 222 | if err = postgres.UserPassword("postgres", req.Password); err != nil { |
| 223 | service.Error(w, http.StatusInternalServerError, s.t.Get("failed to set postgres password: %v", err)) |
| 224 | return |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | if err = s.settingRepo.Set(biz.SettingKeyPostgresPassword, req.Password); err != nil { |
| 229 | service.Error(w, http.StatusInternalServerError, s.t.Get("failed to set postgres password: %v", err)) |
| 230 | return |
| 231 | } |
| 232 | |
| 233 | _ = s.databaseServerRepo.UpdatePassword("local_postgresql", req.Password) |
| 234 | |
| 235 | service.Success(w, nil) |
| 236 | } |
| 237 | |
| 238 | // GetConfigTune 获取 PostgreSQL 配置调整参数 |
| 239 | func (s *App) GetConfigTune(w http.ResponseWriter, r *http.Request) { |
nothing calls this directly
no test coverage detected