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",
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.
type App interface {
AppID() string
LogTable() Table[Log]
LogTable() LogTable
CrashTable() CrashTable
}
+29
View File
@@ -77,6 +77,20 @@ func (b *Backend) deleteCrash(w http.ResponseWriter, r *http.Request) {
//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) {
hdr, err := b.ParseHeader(r)
if hdr.k == nil || hdr.k.Perm["management"] {
@@ -89,3 +103,18 @@ func (b *Backend) archiveCrash(w http.ResponseWriter, r *http.Request) {
}
//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) {}
+18 -7
View File
@@ -14,6 +14,7 @@ type Backend struct {
keyTable Table[ApiKey]
m *http.ServeMux
apps map[string]App
managementKeyID string
jwtPriv ed25519.PrivateKey
jwtPub ed25519.PublicKey
userMutex sync.RWMutex
@@ -50,19 +51,29 @@ func NewBackend(keyTable Table[ApiKey], apps ...App) (*Backend, error) {
b.m.HandleFunc("POST /crash", b.reportCrash)
b.m.HandleFunc("DELETE /crash/{crashID}", b.deleteCrash)
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
}
func (b *Backend) startCleanupLoop() {
go func() {
func (b *Backend) cleanupLoop() {
for range time.Tick(24 * time.Hour) {
//TODO
oldTim := time.Now().Add(-30 * 24 * time.Hour)
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) {
+6
View File
@@ -19,6 +19,12 @@ type Table[T IDStruct] interface {
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 {
Table[CrashReport]
// Move a crash type to archive. All instances that perfectly match that appear in CrashReport.Individual should be deleted.