From 6ece881a5241c278b489f3ee92c961b9b0e654c9 Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Sun, 10 Oct 2021 14:44:12 +1030 Subject: [PATCH] Fix upload logs --- dau.go | 10 +++++----- upload/upload.go | 10 +++++++++- web/server.go | 18 ++++++++++-------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/dau.go b/dau.go index ccef88c..ae2830a 100644 --- a/dau.go +++ b/dau.go @@ -33,7 +33,7 @@ type watch struct { lastCheck time.Time newLastCheck time.Time config config.Watcher - uploader upload.Uploader + uploader *upload.Uploader } func main() { @@ -47,18 +47,18 @@ func main() { config.LoadOrInit() // create the uploader - up := upload.Uploader{} + up := upload.NewUploader() // log.Print("Opening web browser") // open.Start("http://localhost:9090") - web := web.WebService{Config: config} + web := web.WebService{Config: config, Uploader: up} web.StartWebServer() go func() { checkUpdates() }() // create the watchers, restart them if config changes // blocks forever - startWatchers(config, &up, configChanged) + startWatchers(config, up, configChanged) } @@ -68,7 +68,7 @@ func startWatchers(config *config.ConfigService, up *upload.Uploader, configChan ctx, cancel := context.WithCancel(context.Background()) for _, c := range config.Config.Watchers { log.Printf("Creating watcher for %s interval %d", c.Path, config.Config.WatchInterval) - watcher := watch{uploader: *up, lastCheck: time.Now(), newLastCheck: time.Now(), config: c} + watcher := watch{uploader: up, lastCheck: time.Now(), newLastCheck: time.Now(), config: c} go watcher.Watch(config.Config.WatchInterval, ctx) } // wait for single that the config changed diff --git a/upload/upload.go b/upload/upload.go index bc750c1..c32e132 100644 --- a/upload/upload.go +++ b/upload/upload.go @@ -23,7 +23,7 @@ import ( ) type Uploader struct { - Uploads []*Upload + Uploads []*Upload `json:"uploads"` } type Upload struct { @@ -45,6 +45,13 @@ type Upload struct { Height int `json:"height"` } +func NewUploader() *Uploader { + u := Uploader{} + uploads := make([]*Upload, 0) + u.Uploads = uploads + return &u +} + func (u *Uploader) AddFile(file string, conf config.Watcher) { thisUpload := Upload{ Uploaded: false, @@ -58,6 +65,7 @@ func (u *Uploader) AddFile(file string, conf config.Watcher) { // Upload uploads any files that have not yet been uploaded func (u *Uploader) Upload() { + for _, upload := range u.Uploads { if !upload.Uploaded { upload.processUpload() diff --git a/web/server.go b/web/server.go index f0f42c2..906d4e4 100644 --- a/web/server.go +++ b/web/server.go @@ -14,11 +14,13 @@ import ( "github.com/tardisx/discord-auto-upload/config" daulog "github.com/tardisx/discord-auto-upload/log" + "github.com/tardisx/discord-auto-upload/upload" "github.com/tardisx/discord-auto-upload/version" ) type WebService struct { - Config *config.ConfigService + Config *config.ConfigService + Uploader *upload.Uploader } //go:embed data @@ -149,19 +151,19 @@ func (ws *WebService) handleConfig(w http.ResponseWriter, r *http.Request) { w.Write(b) } -// func getUploads(w http.ResponseWriter, r *http.Request) { -// w.Header().Set("Content-Type", "application/json") -// ups := uploads.Uploads -// text, _ := json.Marshal(ups) -// w.Write([]byte(text)) -// } +func (ws *WebService) getUploads(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + ups := ws.Uploader.Uploads + text, _ := json.Marshal(ups) + w.Write([]byte(text)) +} func (ws *WebService) StartWebServer() { http.HandleFunc("/", ws.getStatic) http.HandleFunc("/rest/logs", ws.getLogs) - // http.HandleFunc("/rest/uploads", getUploads) + http.HandleFunc("/rest/uploads", ws.getUploads) http.HandleFunc("/rest/config", ws.handleConfig) go func() {