added functionality HardCacheDuration to config.ini

This commit is contained in:
partisan 2024-10-16 22:51:13 +02:00
parent 49f613ddeb
commit 8fe6783ecb
7 changed files with 52 additions and 73 deletions

View file

@ -2,14 +2,11 @@ package main
import (
"bufio"
"log"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/fsnotify/fsnotify"
"gopkg.in/ini.v1"
)
@ -158,44 +155,8 @@ func loadConfig() Config {
WebsiteEnabled: websiteEnabled,
LogLevel: logLevel,
HardCacheDuration: hardCacheDuration,
HardCacheEnabled: hardCacheDuration != 0,
}
return config
}
var configLock sync.RWMutex
func startFileWatcher() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
printErr("%v", err)
}
go func() {
defer watcher.Close()
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
printInfo("Modified file: %v", event.Name)
configLock.Lock()
config = loadConfig()
configLock.Unlock()
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
printWarn("Error watching configuration file: %v", err)
}
}
}()
err = watcher.Add(configFilePath)
if err != nil {
log.Fatal(err)
}
}

View file

@ -30,7 +30,7 @@ var (
var fileResultsChan = make(chan []TorrentResult)
func initializeTorrentSites() {
func init() {
torrentGalaxy = NewTorrentGalaxy()
// nyaa = NewNyaa()
thePirateBay = NewThePirateBay()

View file

@ -41,6 +41,7 @@ func handleImageSearch(w http.ResponseWriter, settings UserSettings, query strin
"Theme": settings.Theme,
"Safe": settings.SafeSearch,
"IsThemeDark": settings.IsThemeDark,
"HardCacheEnabled": config.HardCacheEnabled,
}
// Render the template without measuring the time
@ -98,7 +99,8 @@ func fetchImageResults(query, safe, lang string, page int) []ImageSearchResult {
for _, result := range searchResults {
imageResult := result.(ImageSearchResult)
if config.HardCacheDuration > 0 {
if config.HardCacheEnabled == true {
// Generate hash from the original full-size image URL
hasher := md5.New()
hasher.Write([]byte(imageResult.Full))

21
init.go
View file

@ -1,6 +1,7 @@
package main
import (
"log"
"time"
)
@ -43,8 +44,7 @@ func main() {
}
loadNodeConfig()
go startFileWatcher()
go checkMasterHeartbeat()
// go checkMasterHeartbeat() // Not currently used
if config.AuthCode == "" {
config.AuthCode = generateStrongRandomString(64)
@ -59,14 +59,17 @@ func main() {
}
config.PeerID = hostID
if len(config.Peers) > 0 {
time.Sleep(2 * time.Second) // Give some time for connections to establish
startElection()
}
// if len(config.Peers) > 0 {
// time.Sleep(2 * time.Second) // Give some time for connections to establish
// startElection()
// }
if config.HardCacheDuration > 0 {
config.HardCacheEnabled = true
}
// Start automatic update checker, not used now
//go checkForUpdates()
generateOpenSearchXML(config)
log.Println("Hard cache:", config.HardCacheEnabled)
go startNodeClient()

View file

@ -210,14 +210,7 @@ func runServer() {
w.Header().Set("Content-Type", "application/opensearchdescription+xml")
http.ServeFile(w, r, "static/opensearch.xml")
})
initializeTorrentSites()
config := loadConfig()
generateOpenSearchXML(config)
printMessage("Server is listening on http://localhost:%d", config.Port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil))
// Start automatic update checker
go checkForUpdates()
}

View file

@ -24,7 +24,6 @@ type Message struct {
}
func loadNodeConfig() {
config := loadConfig()
authCode = config.AuthCode
peers = config.Peers
}

View file

@ -117,7 +117,7 @@
<div id="image-viewer-overlay" style="display: none;"></div>
<div id="template-data" data-page="{{ .Page }}" data-query="{{ .Query }}" data-type="image"></div>
<div id="template-data" data-page="{{ .Page }}" data-query="{{ .Query }}" data-type="image" data-hard-cache-enabled="{{ .HardCacheEnabled }}"></div>
<script defer src="/static/js/dynamicscrolling.js"></script>
<script defer src="/static/js/autocomplete.js"></script>
<script defer src="/static/js/imagetitletrim.js"></script>
@ -228,6 +228,26 @@
<!-- JavaScript to Load Images -->
<script>
document.addEventListener("DOMContentLoaded", function() {
const templateData = document.getElementById('template-data');
const hardCacheEnabled = templateData.getAttribute('data-hard-cache-enabled') === 'true';
console.log(!hardCacheEnabled)
if (!hardCacheEnabled) {
// Hard cache is disabled, so skip polling and load images directly
const images = document.querySelectorAll("img[data-id]");
images.forEach((img) => {
// Use the ProxyFull URL directly
img.src = img.dataset.proxyFull;
img.onload = function() {
img.classList.add('loaded');
};
img.onerror = function() {
console.error('Failed to load image:', img.dataset.proxyFull);
};
});
return;
}
let imageMap = {}; // Map of image IDs to img elements
let loadedImageIDs = new Set(); // Keep track of loaded image IDs
let pollingInterval = 2000; // Initial polling interval in milliseconds
@ -344,6 +364,7 @@
checkImageStatus();
});
</script>
<script>
// Check if JavaScript is enabled and modify the DOM accordingly
document.getElementById('content').classList.remove('js-enabled');