passed/routes/get_password.go

42 lines
875 B
Go
Raw Normal View History

2024-10-29 23:08:11 +00:00
package routes
import (
"encoding/json"
"net/http"
"git.1e99.eu/1e99/passed/storage"
)
func GetPassword(store storage.Store) http.HandlerFunc {
2024-10-29 23:08:11 +00:00
return func(res http.ResponseWriter, req *http.Request) {
id := req.PathValue("id")
2024-10-30 10:17:48 +00:00
password, err := store.Get(id)
2024-10-29 23:08:11 +00:00
switch {
case err == storage.ErrNotFound:
http.Error(res, "Password not found", http.StatusNotFound)
return
case err != nil:
http.Error(res, "", http.StatusInternalServerError)
return
}
2024-10-30 10:17:48 +00:00
err = store.Delete(id)
if err != nil {
http.Error(res, "", http.StatusInternalServerError)
return
}
2024-10-29 23:08:11 +00:00
resBody := struct {
2024-11-08 15:02:26 +00:00
// Go automatically encodes byte arrays using Base64
Password []byte `json:"password"`
2024-10-29 23:08:11 +00:00
}{
2024-11-08 15:02:26 +00:00
Password: password,
2024-10-29 23:08:11 +00:00
}
err = json.NewEncoder(res).Encode(&resBody)
if err != nil {
http.Error(res, "", http.StatusInternalServerError)
return
}
}
}