Added cleanup loop
Added LogTable interface
This commit is contained in:
@@ -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
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {}
|
||||
|
||||
@@ -10,13 +10,14 @@ import (
|
||||
)
|
||||
|
||||
type Backend struct {
|
||||
userTable Table[User]
|
||||
keyTable Table[ApiKey]
|
||||
m *http.ServeMux
|
||||
apps map[string]App
|
||||
jwtPriv ed25519.PrivateKey
|
||||
jwtPub ed25519.PublicKey
|
||||
userMutex sync.RWMutex
|
||||
userTable Table[User]
|
||||
keyTable Table[ApiKey]
|
||||
m *http.ServeMux
|
||||
apps map[string]App
|
||||
managementKeyID string
|
||||
jwtPriv ed25519.PrivateKey
|
||||
jwtPub ed25519.PublicKey
|
||||
userMutex sync.RWMutex
|
||||
}
|
||||
|
||||
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("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() {
|
||||
for range time.Tick(24 * time.Hour) {
|
||||
//TODO
|
||||
func (b *Backend) cleanupLoop() {
|
||||
for range time.Tick(24 * time.Hour) {
|
||||
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) {
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user