rework storage interface

This commit is contained in:
1e99 2024-10-30 11:17:48 +01:00
parent 14de89989a
commit 28bb9f6753
6 changed files with 32 additions and 45 deletions

View file

@ -49,7 +49,7 @@ func CreatePassword(store storage.Store, maxLength int, encoding *base64.Encodin
return
}
id, err := store.CreatePassword(
id, err := store.Create(
password,
time.Now().Add(expiresIn),
)

View file

@ -11,7 +11,7 @@ import (
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.GetPassword(id)
password, err := store.Get(id)
switch {
case err == storage.ErrNotFound:
http.Error(res, "Password not found", http.StatusNotFound)
@ -21,8 +21,13 @@ func GetPassword(store storage.Store, encoding *base64.Encoding) http.HandlerFun
return
}
encodedPassword := encoding.EncodeToString(password)
err = store.Delete(id)
if err != nil {
http.Error(res, "", http.StatusInternalServerError)
return
}
encodedPassword := encoding.EncodeToString(password)
resBody := struct {
Password string `json:"password"`
}{

View file

@ -9,16 +9,16 @@ import (
func HasPassword(store storage.Store) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
id := req.PathValue("id")
found, err := store.HasPassword(id)
if err != nil {
_, err := store.Get(id)
switch {
case err == storage.ErrNotFound:
http.Error(res, "", http.StatusNotFound)
return
case err != nil:
http.Error(res, "", http.StatusInternalServerError)
return
}
if found {
res.WriteHeader(http.StatusNoContent)
} else {
res.WriteHeader(http.StatusNotFound)
}
res.WriteHeader(http.StatusNoContent)
}
}

View file

@ -27,7 +27,7 @@ type dir struct {
close chan bool
}
func (store *dir) CreatePassword(password []byte, expiresAt time.Time) (string, error) {
func (store *dir) Create(password []byte, expiresAt time.Time) (string, error) {
for range 1000 {
id := generateId(24)
path := store.getPath(id)
@ -63,7 +63,7 @@ func (store *dir) CreatePassword(password []byte, expiresAt time.Time) (string,
return "", ErrFull
}
func (store *dir) GetPassword(id string) ([]byte, error) {
func (store *dir) Get(id string) ([]byte, error) {
path := store.getPath(id)
file, err := os.OpenFile(
path,
@ -85,27 +85,17 @@ func (store *dir) GetPassword(id string) ([]byte, error) {
return nil, err
}
// Close file early as we need to delete it
file.Close()
err = os.Remove(path)
if err != nil {
return nil, err
}
return entry.Password, nil
}
func (store *dir) HasPassword(id string) (bool, error) {
func (store *dir) Delete(id string) error {
path := store.getPath(id)
_, err := os.Stat(path)
switch {
case os.IsNotExist(err):
return false, nil
case err != nil:
return false, err
err := os.Remove(path)
if err != nil {
return nil
}
return true, nil
return nil
}
func (store *dir) Close() error {
@ -153,11 +143,7 @@ func (store *dir) clearExpired() error {
if now.After(entry.ExpiresAt) {
// Close file early as we need to delete it
file.Close()
err := os.Remove(path)
if err != nil {
continue
}
store.Delete(id)
}
}
}

View file

@ -24,7 +24,7 @@ type ram struct {
close chan bool
}
func (store *ram) CreatePassword(password []byte, expiresAt time.Time) (string, error) {
func (store *ram) Create(password []byte, expiresAt time.Time) (string, error) {
store.lock.Lock()
defer store.lock.Unlock()
@ -46,7 +46,7 @@ func (store *ram) CreatePassword(password []byte, expiresAt time.Time) (string,
return "", ErrFull
}
func (store *ram) GetPassword(id string) ([]byte, error) {
func (store *ram) Get(id string) ([]byte, error) {
store.lock.Lock()
defer store.lock.Unlock()
@ -55,16 +55,12 @@ func (store *ram) GetPassword(id string) ([]byte, error) {
return nil, ErrNotFound
}
delete(store.passwords, id)
return password.Password, nil
}
func (store *ram) HasPassword(id string) (bool, error) {
store.lock.Lock()
defer store.lock.Unlock()
_, found := store.passwords[id]
return found, nil
func (store *ram) Delete(id string) error {
delete(store.passwords, id)
return nil
}
func (store *ram) Close() error {
@ -86,7 +82,7 @@ func (store *ram) clearExpired() error {
for id, password := range store.passwords {
if time.After(password.ExpiresAt) {
delete(store.passwords, id)
store.Delete(id)
}
}

View file

@ -20,9 +20,9 @@ type entry struct {
}
type Store interface {
CreatePassword(password []byte, expiresAt time.Time) (string, error)
GetPassword(id string) ([]byte, error)
HasPassword(id string) (bool, error)
Create(password []byte, expiresAt time.Time) (string, error)
Get(id string) ([]byte, error)
Delete(id string) error
Close() error
}