diff --git a/mplexer/mgmt-active.sh b/mplexer/mgmt-active.sh index f940bf5..b93290f 100644 --- a/mplexer/mgmt-active.sh +++ b/mplexer/mgmt-active.sh @@ -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}" diff --git a/mplexer/mgmt/authstore/authstore.go b/mplexer/mgmt/authstore/authstore.go index a131e77..a361fd7 100644 --- a/mplexer/mgmt/authstore/authstore.go +++ b/mplexer/mgmt/authstore/authstore.go @@ -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) diff --git a/mplexer/mgmt/authstore/postgresql.go b/mplexer/mgmt/authstore/postgresql.go index ed759f3..de69f1e 100644 --- a/mplexer/mgmt/authstore/postgresql.go +++ b/mplexer/mgmt/authstore/postgresql.go @@ -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() diff --git a/mplexer/mgmt/cmd/mgmt/devices.go b/mplexer/mgmt/cmd/mgmt/devices.go index 5005bfb..c4a9a27 100644 --- a/mplexer/mgmt/cmd/mgmt/devices.go +++ b/mplexer/mgmt/cmd/mgmt/devices.go @@ -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 {