passed/route/get_password.go
2025-05-21 17:15:55 +02:00

41 lines
881 B
Go

package route
import (
"encoding/json"
"net/http"
"git.1e99.eu/1e99/passed/password"
)
func GetPassword(storage password.Storage) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
id := req.PathValue("id")
passwd, err := storage.Get(id)
switch {
case err == password.ErrNotFound:
http.Error(res, "Password not found", http.StatusNotFound)
return
case err != nil:
http.Error(res, "", http.StatusInternalServerError)
return
}
err = storage.Delete(id)
if err != nil {
http.Error(res, "", http.StatusInternalServerError)
return
}
resBody := struct {
// Go automatically encodes byte arrays using Base64
Password []byte `json:"password"`
}{
Password: passwd,
}
err = json.NewEncoder(res).Encode(&resBody)
if err != nil {
http.Error(res, "", http.StatusInternalServerError)
return
}
}
}