diff --git a/DirectoryIndexers/DirectoryIndexer.go b/DirectoryIndexers/DirectoryIndexer.go index 243c755..b9cc937 100644 --- a/DirectoryIndexers/DirectoryIndexer.go +++ b/DirectoryIndexers/DirectoryIndexer.go @@ -29,7 +29,6 @@ func Index(path string, results chan FileList) { // Initialize the RWMutex HERE manually because *IT IS A POINTER TO A MUTEX*, so it defaults to a nil value FL.RWMutex = &sync.RWMutex{} - FL.Lock() fmt.Println("Scanning archive...") @@ -89,8 +88,6 @@ func Index(path string, results chan FileList) { } } - FL.Unlock() - results <- FL close(results) diff --git a/main.go b/main.go index bbff073..3a79ffc 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,8 @@ import ( "fmt" "net/http" "os" + "strconv" + "sync" "time" "ytdlp-viewer/DirectoryIndexers" "ytdlp-viewer/PageHandlers" @@ -14,10 +16,14 @@ import ( func main() { var path string flag.StringVar(&path, "path", "./", "the full path to the ytdlp archive (with a / suffix)") + var port int + flag.IntVar(&port, "port", 8000, "the port for the web panel to listen on") flag.Parse() var FL DirectoryIndexers.FileList + FL.RWMutex = &sync.RWMutex{} + go func() { for { resultChannel := make(chan DirectoryIndexers.FileList) @@ -25,7 +31,11 @@ func main() { fmt.Println("Starting scanner at", path) go DirectoryIndexers.Index(path, resultChannel) - FL = <-resultChannel + refreshedFL := <-resultChannel + FL.Lock() + FL.Files = refreshedFL.Files + FL.Unlock() + time.Sleep(60 * time.Second) } }() @@ -44,7 +54,7 @@ func main() { }) http.Handle("/videos/", http.StripPrefix("/videos/", http.FileServer(http.Dir(path)))) - err := http.ListenAndServe(":8000", nil) + err := http.ListenAndServe(":" + strconv.Itoa(port), nil) if err != nil { fmt.Println(err) os.Exit(1)