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 ( import (
"bufio" "bufio"
"log"
"os" "os"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/fsnotify/fsnotify"
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
) )
@ -158,44 +155,8 @@ func loadConfig() Config {
WebsiteEnabled: websiteEnabled, WebsiteEnabled: websiteEnabled,
LogLevel: logLevel, LogLevel: logLevel,
HardCacheDuration: hardCacheDuration, HardCacheDuration: hardCacheDuration,
HardCacheEnabled: hardCacheDuration != 0,
} }
return config 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) var fileResultsChan = make(chan []TorrentResult)
func initializeTorrentSites() { func init() {
torrentGalaxy = NewTorrentGalaxy() torrentGalaxy = NewTorrentGalaxy()
// nyaa = NewNyaa() // nyaa = NewNyaa()
thePirateBay = NewThePirateBay() thePirateBay = NewThePirateBay()

View file

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

21
init.go
View file

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

View file

@ -210,14 +210,7 @@ func runServer() {
w.Header().Set("Content-Type", "application/opensearchdescription+xml") w.Header().Set("Content-Type", "application/opensearchdescription+xml")
http.ServeFile(w, r, "static/opensearch.xml") http.ServeFile(w, r, "static/opensearch.xml")
}) })
initializeTorrentSites()
config := loadConfig()
generateOpenSearchXML(config)
printMessage("Server is listening on http://localhost:%d", config.Port) printMessage("Server is listening on http://localhost:%d", config.Port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)) 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() { func loadNodeConfig() {
config := loadConfig()
authCode = config.AuthCode authCode = config.AuthCode
peers = config.Peers peers = config.Peers
} }

View file

@ -117,7 +117,7 @@
<div id="image-viewer-overlay" style="display: none;"></div> <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/dynamicscrolling.js"></script>
<script defer src="/static/js/autocomplete.js"></script> <script defer src="/static/js/autocomplete.js"></script>
<script defer src="/static/js/imagetitletrim.js"></script> <script defer src="/static/js/imagetitletrim.js"></script>
@ -228,6 +228,26 @@
<!-- JavaScript to Load Images --> <!-- JavaScript to Load Images -->
<script> <script>
document.addEventListener("DOMContentLoaded", function() { 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 imageMap = {}; // Map of image IDs to img elements
let loadedImageIDs = new Set(); // Keep track of loaded image IDs let loadedImageIDs = new Set(); // Keep track of loaded image IDs
let pollingInterval = 2000; // Initial polling interval in milliseconds let pollingInterval = 2000; // Initial polling interval in milliseconds
@ -344,6 +364,7 @@
checkImageStatus(); checkImageStatus();
}); });
</script> </script>
<script> <script>
// Check if JavaScript is enabled and modify the DOM accordingly // Check if JavaScript is enabled and modify the DOM accordingly
document.getElementById('content').classList.remove('js-enabled'); document.getElementById('content').classList.remove('js-enabled');