|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "reflect" |
| 8 | + |
| 9 | + pgbackrestserver "github.com/superfly/percona-postgresql-operator/internal/pgbackrest-server" |
| 10 | +) |
| 11 | + |
| 12 | +type RestoreCommandOptions struct { |
| 13 | + // General Options |
| 14 | + Stanza string `json:"stanza"` // Required |
| 15 | + Set string `json:"set,omitempty"` |
| 16 | + Delta bool `json:"delta,omitempty"` |
| 17 | + Force bool `json:"force,omitempty"` |
| 18 | + Repo int `json:"repo,omitempty"` |
| 19 | + |
| 20 | + // Filter Options |
| 21 | + DBInclude []string `json:"db_include,omitempty"` |
| 22 | + |
| 23 | + // Target & Recovery Options (Point-in-Time Recovery) |
| 24 | + TargetType string `json:"target_type,omitempty"` // e.g., 'time', 'xid', 'lsn' |
| 25 | + Target string `json:"target,omitempty"` |
| 26 | + TargetAction string `json:"target_action,omitempty"` // e.g., 'pause', 'promote', 'shutdown' |
| 27 | + RecoveryOption map[string]string `json:"recovery_option,omitempty"` |
| 28 | +} |
| 29 | + |
| 30 | +func (c *RestoreCommandOptions) AsSlice() []string { |
| 31 | + return pgbackrestserver.IntoOpts(reflect.ValueOf(c)) |
| 32 | +} |
| 33 | + |
| 34 | +func RestoreCommandHandler(w http.ResponseWriter, r *http.Request) { |
| 35 | + var opts RestoreCommandOptions |
| 36 | + if err := json.NewDecoder(r.Body).Decode(&opts); err != nil { |
| 37 | + http.Error(w, fmt.Sprintf("could not decode json body: %v", err), http.StatusBadRequest) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + cmd := pgbackrestserver.BackrestCommand{ |
| 42 | + Command: "restore", |
| 43 | + Opts: opts.AsSlice(), |
| 44 | + } |
| 45 | + |
| 46 | + stdout, _, err := cmd.Run(r.Context()) |
| 47 | + if err != nil { |
| 48 | + http.Error(w, fmt.Sprintf("failed to run pgbackrest restore command: %v", err), http.StatusInternalServerError) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + out := map[string]string{"data": stdout.String()} |
| 53 | + w.WriteHeader(http.StatusOK) |
| 54 | + if err := json.NewEncoder(w).Encode(out); err != nil { |
| 55 | + http.Error(w, fmt.Sprintf("failed to encode json response: %v", err), http.StatusInternalServerError) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments