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("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))
|
||||||
mux.Handle("GET /api/password/{id}", routes.GetPassword(storage, 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")
|
address := os.Getenv("PASSED_ADDRESS")
|
||||||
if 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,12 +6,11 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewRamStore(clearInterval time.Duration) Store {
|
func NewRamStore() Store {
|
||||||
store := &ram{
|
store := &ram{
|
||||||
clearInterval: clearInterval,
|
passwords: make(map[string]entry),
|
||||||
passwords: make(map[string]entry),
|
lock: sync.Mutex{},
|
||||||
lock: sync.Mutex{},
|
close: make(chan bool),
|
||||||
close: make(chan bool),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
go store.clearExpired()
|
go store.clearExpired()
|
||||||
|
@ -19,10 +18,9 @@ func NewRamStore(clearInterval time.Duration) Store {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ram struct {
|
type ram struct {
|
||||||
clearInterval time.Duration
|
passwords map[string]entry
|
||||||
passwords map[string]entry
|
lock sync.Mutex
|
||||||
lock sync.Mutex
|
close chan bool
|
||||||
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) {
|
||||||
|
@ -60,21 +58,13 @@ func (store *ram) GetPassword(id string) ([]byte, error) {
|
||||||
return password.password, nil
|
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 {
|
func (store *ram) Close() error {
|
||||||
store.close <- true
|
store.close <- true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (store *ram) clearExpired() error {
|
func (store *ram) clearExpired() error {
|
||||||
ticker := time.NewTicker(store.clearInterval)
|
ticker := time.NewTicker(20 * time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
@ -83,6 +73,7 @@ func (store *ram) clearExpired() error {
|
||||||
return nil
|
return nil
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
store.lock.Lock()
|
store.lock.Lock()
|
||||||
|
defer store.lock.Unlock()
|
||||||
time := time.Now()
|
time := time.Now()
|
||||||
|
|
||||||
for id, password := range store.passwords {
|
for id, password := range store.passwords {
|
||||||
|
@ -90,8 +81,6 @@ func (store *ram) clearExpired() error {
|
||||||
delete(store.passwords, id)
|
delete(store.passwords, id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
store.lock.Unlock()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,6 @@ 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)
|
||||||
HasPassword(id string) (bool, error)
|
|
||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,10 +30,10 @@ func NewStore() (Store, error) {
|
||||||
|
|
||||||
switch storeType {
|
switch storeType {
|
||||||
case "ram":
|
case "ram":
|
||||||
return NewRamStore(20 * time.Second), nil
|
return NewRamStore(), nil
|
||||||
|
|
||||||
default:
|
default:
|
||||||
log.Printf("No PASSED_STORE_TYPE provided, defaulting to memory store.")
|
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