Fix upload logs

This commit is contained in:
Justin Hawkins 2021-10-10 14:44:12 +10:30
parent 4534394abc
commit 6ece881a52
3 changed files with 24 additions and 14 deletions

10
dau.go
View File

@ -33,7 +33,7 @@ type watch struct {
lastCheck time.Time lastCheck time.Time
newLastCheck time.Time newLastCheck time.Time
config config.Watcher config config.Watcher
uploader upload.Uploader uploader *upload.Uploader
} }
func main() { func main() {
@ -47,18 +47,18 @@ func main() {
config.LoadOrInit() config.LoadOrInit()
// create the uploader // create the uploader
up := upload.Uploader{} up := upload.NewUploader()
// log.Print("Opening web browser") // log.Print("Opening web browser")
// open.Start("http://localhost:9090") // open.Start("http://localhost:9090")
web := web.WebService{Config: config} web := web.WebService{Config: config, Uploader: up}
web.StartWebServer() web.StartWebServer()
go func() { checkUpdates() }() go func() { checkUpdates() }()
// create the watchers, restart them if config changes // create the watchers, restart them if config changes
// blocks forever // 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()) ctx, cancel := context.WithCancel(context.Background())
for _, c := range config.Config.Watchers { for _, c := range config.Config.Watchers {
log.Printf("Creating watcher for %s interval %d", c.Path, config.Config.WatchInterval) 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) go watcher.Watch(config.Config.WatchInterval, ctx)
} }
// wait for single that the config changed // wait for single that the config changed

View File

@ -23,7 +23,7 @@ import (
) )
type Uploader struct { type Uploader struct {
Uploads []*Upload Uploads []*Upload `json:"uploads"`
} }
type Upload struct { type Upload struct {
@ -45,6 +45,13 @@ type Upload struct {
Height int `json:"height"` 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) { func (u *Uploader) AddFile(file string, conf config.Watcher) {
thisUpload := Upload{ thisUpload := Upload{
Uploaded: false, 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 // Upload uploads any files that have not yet been uploaded
func (u *Uploader) Upload() { func (u *Uploader) Upload() {
for _, upload := range u.Uploads { for _, upload := range u.Uploads {
if !upload.Uploaded { if !upload.Uploaded {
upload.processUpload() upload.processUpload()

View File

@ -14,11 +14,13 @@ import (
"github.com/tardisx/discord-auto-upload/config" "github.com/tardisx/discord-auto-upload/config"
daulog "github.com/tardisx/discord-auto-upload/log" daulog "github.com/tardisx/discord-auto-upload/log"
"github.com/tardisx/discord-auto-upload/upload"
"github.com/tardisx/discord-auto-upload/version" "github.com/tardisx/discord-auto-upload/version"
) )
type WebService struct { type WebService struct {
Config *config.ConfigService Config *config.ConfigService
Uploader *upload.Uploader
} }
//go:embed data //go:embed data
@ -149,19 +151,19 @@ func (ws *WebService) handleConfig(w http.ResponseWriter, r *http.Request) {
w.Write(b) w.Write(b)
} }
// func getUploads(w http.ResponseWriter, r *http.Request) { func (ws *WebService) getUploads(w http.ResponseWriter, r *http.Request) {
// w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
// ups := uploads.Uploads ups := ws.Uploader.Uploads
// text, _ := json.Marshal(ups) text, _ := json.Marshal(ups)
// w.Write([]byte(text)) w.Write([]byte(text))
// } }
func (ws *WebService) StartWebServer() { func (ws *WebService) StartWebServer() {
http.HandleFunc("/", ws.getStatic) http.HandleFunc("/", ws.getStatic)
http.HandleFunc("/rest/logs", ws.getLogs) http.HandleFunc("/rest/logs", ws.getLogs)
// http.HandleFunc("/rest/uploads", getUploads) http.HandleFunc("/rest/uploads", ws.getUploads)
http.HandleFunc("/rest/config", ws.handleConfig) http.HandleFunc("/rest/config", ws.handleConfig)
go func() { go func() {