Added cleanup loop

Added LogTable interface
This commit is contained in:
Caleb Gardner
2024-05-31 06:59:30 -05:00
parent 228f0ff86d
commit 2040631737
5 changed files with 63 additions and 17 deletions
+1 -1
View File
@@ -29,7 +29,7 @@ The special appID "darkstormManagement" is used to manage all apps.
{ {
id: "UUID", id: "UUID",
platform: "android", platform: "android",
Date: 20240519 // YYYYMMD Date: 20240519 // YYYYMMDD as int
} }
``` ```
+1 -1
View File
@@ -5,7 +5,7 @@ import "net/http"
// An application interface. Both LogTable and CrashTable are optional, if they return nil then requests will be forbidden. // An application interface. Both LogTable and CrashTable are optional, if they return nil then requests will be forbidden.
type App interface { type App interface {
AppID() string AppID() string
LogTable() Table[Log] LogTable() LogTable
CrashTable() CrashTable CrashTable() CrashTable
} }
+29
View File
@@ -77,6 +77,20 @@ func (b *Backend) deleteCrash(w http.ResponseWriter, r *http.Request) {
//TODO //TODO
} }
func (b *Backend) managementDeleteCrash(w http.ResponseWriter, r *http.Request) {
hdr, err := b.ParseHeader(r)
if hdr.k == nil || hdr.k.Perm["management"] || errors.Is(err, ErrApiKeyUnauthorized) {
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
return
} else if err != nil {
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
return
}
//TODO
}
func (b *Backend) actualCrashDelete(w http.ResponseWriter, ap App, crashID string) {}
func (b *Backend) archiveCrash(w http.ResponseWriter, r *http.Request) { func (b *Backend) archiveCrash(w http.ResponseWriter, r *http.Request) {
hdr, err := b.ParseHeader(r) hdr, err := b.ParseHeader(r)
if hdr.k == nil || hdr.k.Perm["management"] { if hdr.k == nil || hdr.k.Perm["management"] {
@@ -89,3 +103,18 @@ func (b *Backend) archiveCrash(w http.ResponseWriter, r *http.Request) {
} }
//TODO //TODO
} }
func (b *Backend) managementArchiveCrash(w http.ResponseWriter, r *http.Request) {
hdr, err := b.ParseHeader(r)
if hdr.k == nil || hdr.k.Perm["management"] {
w.WriteHeader(http.StatusUnauthorized)
return
}
if err != nil {
//TODO
return
}
//TODO
}
func (b *Backend) actualCrashArchive(w http.ResponseWriter, ap App, toArchive ArchivedCrash) {}
+26 -15
View File
@@ -10,13 +10,14 @@ import (
) )
type Backend struct { type Backend struct {
userTable Table[User] userTable Table[User]
keyTable Table[ApiKey] keyTable Table[ApiKey]
m *http.ServeMux m *http.ServeMux
apps map[string]App apps map[string]App
jwtPriv ed25519.PrivateKey managementKeyID string
jwtPub ed25519.PublicKey jwtPriv ed25519.PrivateKey
userMutex sync.RWMutex jwtPub ed25519.PublicKey
userMutex sync.RWMutex
} }
func NewBackend(keyTable Table[ApiKey], apps ...App) (*Backend, error) { func NewBackend(keyTable Table[ApiKey], apps ...App) (*Backend, error) {
@@ -50,19 +51,29 @@ func NewBackend(keyTable Table[ApiKey], apps ...App) (*Backend, error) {
b.m.HandleFunc("POST /crash", b.reportCrash) b.m.HandleFunc("POST /crash", b.reportCrash)
b.m.HandleFunc("DELETE /crash/{crashID}", b.deleteCrash) b.m.HandleFunc("DELETE /crash/{crashID}", b.deleteCrash)
b.m.HandleFunc("POST /crash/archive", b.archiveCrash) b.m.HandleFunc("POST /crash/archive", b.archiveCrash)
b.m.HandleFunc("DELETE /{appID}/crash/{crashID}", b.deleteCrash)
b.m.HandleFunc("POST /{appID}/crash/archive", b.archiveCrash)
} }
b.startCleanupLoop() go b.cleanupLoop()
return b, nil return b, nil
} }
func (b *Backend) startCleanupLoop() { func (b *Backend) cleanupLoop() {
go func() { for range time.Tick(24 * time.Hour) {
for range time.Tick(24 * time.Hour) { oldTim := time.Now().Add(-30 * 24 * time.Hour)
//TODO old := (oldTim.Year() * 10000) + (int(oldTim.Month()) * 100) + oldTim.Day()
for _, a := range b.apps {
tab := a.LogTable()
if tab == nil {
continue
}
tab.RemoveOldLogs(old)
} }
}() }
}
func (b *Backend) EnableManagementKey(managementID string) {
b.managementKeyID = managementID
b.m.HandleFunc("DELETE /{appID}/crash/{crashID}", b.managementDeleteCrash)
b.m.HandleFunc("POST /{appID}/crash/archive", b.managementArchiveCrash)
} }
func (b *Backend) AddUserAuth(userTable Table[User], privKey, pubKey []byte) { func (b *Backend) AddUserAuth(userTable Table[User], privKey, pubKey []byte) {
+6
View File
@@ -19,6 +19,12 @@ type Table[T IDStruct] interface {
PartUpdate(ID string, update map[string]any) error PartUpdate(ID string, update map[string]any) error
} }
type LogTable interface {
Table[Log]
// Remove all Log items that have a Log.Date value less then the given value.
RemoveOldLogs(date int)
}
type CrashTable interface { type CrashTable interface {
Table[CrashReport] Table[CrashReport]
// Move a crash type to archive. All instances that perfectly match that appear in CrashReport.Individual should be deleted. // Move a crash type to archive. All instances that perfectly match that appear in CrashReport.Individual should be deleted.