Compare commits
No commits in common. "b0baf850784ca803b463850c9bfb9bda75056b63" and "dfa37fc7faf44eb5440fbed5e1a8eed8d5c7bcfc" have entirely different histories.
b0baf85078
...
dfa37fc7fa
4 changed files with 11 additions and 48 deletions
1
main.go
1
main.go
|
@ -26,7 +26,6 @@ func run() error {
|
|||
mux.Handle("GET /", routes.ServeFiles(embedFS, "static"))
|
||||
mux.Handle("POST /api/password", routes.CreatePassword(storage, 12*1024, base64.StdEncoding))
|
||||
mux.Handle("GET /api/password/{id}", routes.GetPassword(storage, base64.StdEncoding))
|
||||
mux.Handle("HEAD /api/password/{id}", routes.HasPassword(storage))
|
||||
|
||||
address := os.Getenv("PASSED_ADDRESS")
|
||||
if address == "" {
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
package routes
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.1e99.eu/1e99/passed/storage"
|
||||
)
|
||||
|
||||
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 {
|
||||
http.Error(res, "", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if found {
|
||||
res.WriteHeader(http.StatusNoContent)
|
||||
} else {
|
||||
res.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,9 +6,8 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
func NewRamStore(clearInterval time.Duration) Store {
|
||||
func NewRamStore() Store {
|
||||
store := &ram{
|
||||
clearInterval: clearInterval,
|
||||
passwords: make(map[string]entry),
|
||||
lock: sync.Mutex{},
|
||||
close: make(chan bool),
|
||||
|
@ -19,7 +18,6 @@ func NewRamStore(clearInterval time.Duration) Store {
|
|||
}
|
||||
|
||||
type ram struct {
|
||||
clearInterval time.Duration
|
||||
passwords map[string]entry
|
||||
lock sync.Mutex
|
||||
close chan bool
|
||||
|
@ -60,21 +58,13 @@ func (store *ram) GetPassword(id string) ([]byte, error) {
|
|||
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) Close() error {
|
||||
store.close <- true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *ram) clearExpired() error {
|
||||
ticker := time.NewTicker(store.clearInterval)
|
||||
ticker := time.NewTicker(20 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
|
@ -83,6 +73,7 @@ func (store *ram) clearExpired() error {
|
|||
return nil
|
||||
case <-ticker.C:
|
||||
store.lock.Lock()
|
||||
defer store.lock.Unlock()
|
||||
time := time.Now()
|
||||
|
||||
for id, password := range store.passwords {
|
||||
|
@ -90,8 +81,6 @@ func (store *ram) clearExpired() error {
|
|||
delete(store.passwords, id)
|
||||
}
|
||||
}
|
||||
|
||||
store.lock.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@ type entry struct {
|
|||
type Store interface {
|
||||
CreatePassword(password []byte, expiresAt time.Time) (string, error)
|
||||
GetPassword(id string) ([]byte, error)
|
||||
HasPassword(id string) (bool, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
|
@ -31,10 +30,10 @@ func NewStore() (Store, error) {
|
|||
|
||||
switch storeType {
|
||||
case "ram":
|
||||
return NewRamStore(20 * time.Second), nil
|
||||
return NewRamStore(), nil
|
||||
|
||||
default:
|
||||
log.Printf("No PASSED_STORE_TYPE provided, defaulting to memory store.")
|
||||
return NewRamStore(20 * time.Second), nil
|
||||
return NewRamStore(), nil
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue