passed/routes/get_password.go

41 lines
875 B
Go

package routes
import (
"encoding/json"
"net/http"
"git.1e99.eu/1e99/passed/storage"
)
func GetPassword(store storage.Store) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
id := req.PathValue("id")
password, err := store.Get(id)
switch {
case err == storage.ErrNotFound:
http.Error(res, "Password not found", http.StatusNotFound)
return
case err != nil:
http.Error(res, "", http.StatusInternalServerError)
return
}
err = store.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: password,
}
err = json.NewEncoder(res).Encode(&resBody)
if err != nil {
http.Error(res, "", http.StatusInternalServerError)
return
}
}
}