github.com/prysmaticlabs/prysm@v1.4.4/shared/backuputil/http_backup_handler.go (about)

     1  package backuputil
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/sirupsen/logrus"
     9  )
    10  
    11  // BackupExporter defines a backup exporter methods.
    12  type BackupExporter interface {
    13  	Backup(ctx context.Context, outputPath string, permissionOverride bool) error
    14  }
    15  
    16  // BackupHandler for accepting requests to initiate a new database backup.
    17  func BackupHandler(bk BackupExporter, outputDir string) func(http.ResponseWriter, *http.Request) {
    18  	log := logrus.WithField("prefix", "db")
    19  
    20  	return func(w http.ResponseWriter, r *http.Request) {
    21  		log.Debug("Creating database backup from HTTP webhook")
    22  
    23  		_, permissionOverride := r.URL.Query()["permissionOverride"]
    24  
    25  		if err := bk.Backup(context.Background(), outputDir, permissionOverride); err != nil {
    26  			log.WithError(err).Error("Failed to create backup")
    27  			w.WriteHeader(http.StatusInternalServerError)
    28  			return
    29  		}
    30  		w.WriteHeader(http.StatusOK)
    31  		_, err := fmt.Fprint(w, "OK")
    32  		if err != nil {
    33  			log.WithError(err).Error("Failed to write OK")
    34  		}
    35  	}
    36  }