add list of inactives

This commit is contained in:
AJ ONeal 2020-06-01 02:48:05 -06:00
parent ca72ad6d8b
commit 309ecf89f5
4 changed files with 33 additions and 2 deletions

View File

@ -1,4 +1,8 @@
TOKEN=$(go run cmd/signjwt/*.go)
echo "TOKEN: $TOKEN"
echo "Active:"
curl -L http://localhost:3000/api/devices -H "Authorization: Bearer ${TOKEN}"
echo "Inactive:"
curl -L http://localhost:3000/api/devices?inactive=true -H "Authorization: Bearer ${TOKEN}"

View File

@ -30,6 +30,7 @@ type Store interface {
Add(auth *Authorization) error
Set(auth *Authorization) error
Active() ([]Authorization, error)
Inactive() ([]Authorization, error)
Touch(id string) error
Get(id string) (*Authorization, error)
GetBySlug(id string) (*Authorization, error)

View File

@ -174,6 +174,25 @@ func (s *PGStore) Active() ([]Authorization, error) {
return auths, nil
}
func (s *PGStore) Inactive() ([]Authorization, error) {
ctx, done := context.WithDeadline(context.Background(), time.Now().Add(5*time.Second))
defer done()
auths := []Authorization{}
query := `
SELECT * FROM authorizations
WHERE deleted_at = '1970-01-01 00:00:00'
AND updated_at <= $1
AND slug != '*'
`
ago15Min := time.Now().Add(-15 * time.Minute)
err := s.dbx.SelectContext(ctx, &auths, query, ago15Min)
if nil != err {
return nil, err
}
return auths, nil
}
func (s *PGStore) Get(id string) (*Authorization, error) {
ctx, done := context.WithDeadline(context.Background(), time.Now().Add(5*time.Second))
defer done()

View File

@ -7,6 +7,7 @@ import (
"fmt"
"log"
"net/http"
"strings"
"time"
"git.coolaj86.com/coolaj86/go-telebitd/mplexer/mgmt/authstore"
@ -92,7 +93,13 @@ func handleDeviceRoutes(r chi.Router) {
})
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
things, err := store.Active()
var things []authstore.Authorization
var err error
if "true" == strings.Join(r.URL.Query()["inactive"], " ") {
things, err = store.Inactive()
} else {
things, err = store.Active()
}
if nil != err {
msg := `{"error":"not really sure what happened, but it didn't go well (check the logs)"}`
log.Printf("/api/devices/\n")
@ -101,7 +108,7 @@ func handleDeviceRoutes(r chi.Router) {
return
}
for i, _ := range things {
for i := range things {
auth := things[i]
// Redact private data
if "" != auth.MachinePPID {