github.com/weaviate/weaviate@v1.24.6/adapters/handlers/rest/clusterapi/backups.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package clusterapi 13 14 import ( 15 "context" 16 "encoding/json" 17 "fmt" 18 "io" 19 "net/http" 20 21 "github.com/weaviate/weaviate/usecases/backup" 22 ) 23 24 type backupManager interface { 25 OnCanCommit(ctx context.Context, req *backup.Request) *backup.CanCommitResponse 26 OnCommit(ctx context.Context, req *backup.StatusRequest) error 27 OnAbort(ctx context.Context, req *backup.AbortRequest) error 28 OnStatus(ctx context.Context, req *backup.StatusRequest) *backup.StatusResponse 29 } 30 31 type backups struct { 32 manager backupManager 33 auth auth 34 } 35 36 func NewBackups(manager backupManager, auth auth) *backups { 37 return &backups{manager: manager, auth: auth} 38 } 39 40 func (b *backups) CanCommit() http.Handler { 41 return b.auth.handleFunc(b.canCommitHandler()) 42 } 43 44 func (b *backups) canCommitHandler() http.HandlerFunc { 45 return func(w http.ResponseWriter, r *http.Request) { 46 body, err := io.ReadAll(r.Body) 47 if err != nil { 48 status := http.StatusInternalServerError 49 http.Error(w, fmt.Errorf("read request body: %w", err).Error(), status) 50 return 51 } 52 defer r.Body.Close() 53 54 var req backup.Request 55 if err := json.Unmarshal(body, &req); err != nil { 56 status := http.StatusInternalServerError 57 http.Error(w, fmt.Errorf("unmarshal request: %w", err).Error(), status) 58 return 59 } 60 61 resp := b.manager.OnCanCommit(r.Context(), &req) 62 b, err := json.Marshal(&resp) 63 if err != nil { 64 status := http.StatusInternalServerError 65 http.Error(w, fmt.Errorf("marshal response: %w", err).Error(), status) 66 return 67 } 68 69 w.WriteHeader(http.StatusOK) 70 w.Write(b) 71 } 72 } 73 74 func (b *backups) Commit() http.Handler { 75 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 76 body, err := io.ReadAll(r.Body) 77 if err != nil { 78 status := http.StatusInternalServerError 79 http.Error(w, fmt.Errorf("read request body: %w", err).Error(), status) 80 return 81 } 82 defer r.Body.Close() 83 84 var req backup.StatusRequest 85 if err := json.Unmarshal(body, &req); err != nil { 86 status := http.StatusInternalServerError 87 http.Error(w, fmt.Errorf("unmarshal request: %w", err).Error(), status) 88 return 89 } 90 91 if err := b.manager.OnCommit(r.Context(), &req); err != nil { 92 status := http.StatusInternalServerError 93 http.Error(w, fmt.Errorf("commit: %w", err).Error(), status) 94 return 95 } 96 97 w.WriteHeader(http.StatusCreated) 98 }) 99 } 100 101 func (b *backups) Abort() http.Handler { 102 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 103 body, err := io.ReadAll(r.Body) 104 if err != nil { 105 status := http.StatusInternalServerError 106 http.Error(w, fmt.Errorf("read request body: %w", err).Error(), status) 107 return 108 } 109 defer r.Body.Close() 110 111 var req backup.AbortRequest 112 if err := json.Unmarshal(body, &req); err != nil { 113 status := http.StatusInternalServerError 114 http.Error(w, fmt.Errorf("unmarshal request: %w", err).Error(), status) 115 return 116 } 117 118 if err := b.manager.OnAbort(r.Context(), &req); err != nil { 119 status := http.StatusInternalServerError 120 http.Error(w, fmt.Errorf("abort: %w", err).Error(), status) 121 return 122 } 123 124 w.WriteHeader(http.StatusNoContent) 125 }) 126 } 127 128 func (b *backups) Status() http.Handler { 129 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 130 body, err := io.ReadAll(r.Body) 131 if err != nil { 132 status := http.StatusInternalServerError 133 http.Error(w, fmt.Errorf("read request body: %w", err).Error(), status) 134 return 135 } 136 defer r.Body.Close() 137 138 var req backup.StatusRequest 139 if err := json.Unmarshal(body, &req); err != nil { 140 status := http.StatusInternalServerError 141 http.Error(w, fmt.Errorf("unmarshal request: %w", err).Error(), status) 142 return 143 } 144 145 resp := b.manager.OnStatus(r.Context(), &req) 146 b, err := json.Marshal(&resp) 147 if err != nil { 148 status := http.StatusInternalServerError 149 http.Error(w, fmt.Errorf("marshal response: %w", err).Error(), status) 150 return 151 } 152 153 w.WriteHeader(http.StatusOK) 154 w.Write(b) 155 }) 156 }