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 return
} }
id, err := store.CreatePassword( id, err := store.Create(
password, password,
time.Now().Add(expiresIn), time.Now().Add(expiresIn),
) )

View file

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

View file

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

View file

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

View file

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

View file

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