allow storage to close
This commit is contained in:
parent
7d21bde292
commit
dfa37fc7fa
3 changed files with 28 additions and 18 deletions
2
main.go
2
main.go
|
@ -20,6 +20,8 @@ func run() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer storage.Close()
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.Handle("GET /", routes.ServeFiles(embedFS, "static"))
|
mux.Handle("GET /", routes.ServeFiles(embedFS, "static"))
|
||||||
mux.Handle("POST /api/password", routes.CreatePassword(storage, 12*1024, base64.StdEncoding))
|
mux.Handle("POST /api/password", routes.CreatePassword(storage, 12*1024, base64.StdEncoding))
|
||||||
|
|
|
@ -10,23 +10,17 @@ func NewRamStore() Store {
|
||||||
store := &ram{
|
store := &ram{
|
||||||
passwords: make(map[string]entry),
|
passwords: make(map[string]entry),
|
||||||
lock: sync.Mutex{},
|
lock: sync.Mutex{},
|
||||||
|
close: make(chan bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go store.clearExpired()
|
||||||
ticker := time.Tick(25 * time.Second)
|
|
||||||
|
|
||||||
for {
|
|
||||||
store.clearExpired()
|
|
||||||
<-ticker
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
|
|
||||||
type ram struct {
|
type ram struct {
|
||||||
passwords map[string]entry
|
passwords map[string]entry
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
|
close chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *ram) CreatePassword(password []byte, expiresAt time.Time) (string, error) {
|
func (store *ram) CreatePassword(password []byte, expiresAt time.Time) (string, error) {
|
||||||
|
@ -64,18 +58,31 @@ func (store *ram) GetPassword(id string) ([]byte, error) {
|
||||||
return password.password, nil
|
return password.password, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *ram) clearExpired() error {
|
func (store *ram) Close() error {
|
||||||
store.lock.Lock()
|
store.close <- true
|
||||||
defer store.lock.Unlock()
|
return nil
|
||||||
time := time.Now()
|
}
|
||||||
|
|
||||||
for id, password := range store.passwords {
|
func (store *ram) clearExpired() error {
|
||||||
if time.After(password.expiresAt) {
|
ticker := time.NewTicker(20 * time.Second)
|
||||||
delete(store.passwords, id)
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-store.close:
|
||||||
|
return nil
|
||||||
|
case <-ticker.C:
|
||||||
|
store.lock.Lock()
|
||||||
|
defer store.lock.Unlock()
|
||||||
|
time := time.Now()
|
||||||
|
|
||||||
|
for id, password := range store.passwords {
|
||||||
|
if time.After(password.expiresAt) {
|
||||||
|
delete(store.passwords, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *ram) generateId(length int) string {
|
func (store *ram) generateId(length int) string {
|
||||||
|
|
|
@ -21,6 +21,7 @@ type entry struct {
|
||||||
type Store interface {
|
type Store interface {
|
||||||
CreatePassword(password []byte, expiresAt time.Time) (string, error)
|
CreatePassword(password []byte, expiresAt time.Time) (string, error)
|
||||||
GetPassword(id string) ([]byte, error)
|
GetPassword(id string) ([]byte, error)
|
||||||
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStore() (Store, error) {
|
func NewStore() (Store, error) {
|
||||||
|
|
Loading…
Reference in a new issue