42 lines
921 B
Go
42 lines
921 B
Go
package routes
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"git.1e99.eu/1e99/passed/storage"
|
|
)
|
|
|
|
func GetPassword(store storage.Store, encoding *base64.Encoding) 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
|
|
}
|
|
}
|
|
}
|