This commit is contained in:
parent
c33a997dc5
commit
57507756ec
14 changed files with 864 additions and 97 deletions
|
@ -36,6 +36,90 @@ func initTextEngines() {
|
|||
}
|
||||
}
|
||||
|
||||
func HandleTextSearchWithInstantAnswer(w http.ResponseWriter, settings UserSettings, query string, page int) {
|
||||
startTime := time.Now()
|
||||
|
||||
var instant *InstantAnswer
|
||||
done := make(chan struct{})
|
||||
|
||||
// Spawn instant detection in background
|
||||
go func() {
|
||||
instant = detectInstantAnswer(query)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
// Call original handler and capture its result template data
|
||||
cacheKey := CacheKey{
|
||||
Query: query,
|
||||
Page: page,
|
||||
Safe: settings.SafeSearch == "active",
|
||||
Lang: settings.SearchLanguage,
|
||||
Type: "text",
|
||||
}
|
||||
combinedResults := getTextResultsFromCacheOrFetch(cacheKey, query, settings.SafeSearch, settings.SearchLanguage, page)
|
||||
|
||||
hasPrevPage := page > 1
|
||||
|
||||
go prefetchPage(query, settings.SafeSearch, settings.SearchLanguage, page+1)
|
||||
if hasPrevPage {
|
||||
go prefetchPage(query, settings.SafeSearch, settings.SearchLanguage, page-1)
|
||||
}
|
||||
|
||||
elapsedTime := time.Since(startTime)
|
||||
|
||||
type DecoratedResult struct {
|
||||
TextSearchResult
|
||||
PrettyLink LinkParts
|
||||
FaviconID string
|
||||
}
|
||||
var decoratedResults []DecoratedResult
|
||||
for _, r := range combinedResults {
|
||||
if r.URL == "" {
|
||||
continue
|
||||
}
|
||||
prettyLink := FormatLinkHTML(r.URL)
|
||||
faviconID := faviconIDFromURL(prettyLink.RootURL)
|
||||
|
||||
decoratedResults = append(decoratedResults, DecoratedResult{
|
||||
TextSearchResult: r,
|
||||
PrettyLink: prettyLink,
|
||||
FaviconID: faviconID,
|
||||
})
|
||||
|
||||
go ensureFaviconIsCached(faviconID, prettyLink.RootURL)
|
||||
}
|
||||
|
||||
// Wait max 300ms for instant answer detection
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(300 * time.Millisecond):
|
||||
printInfo("InstantAnswer detection timed out")
|
||||
}
|
||||
|
||||
data := map[string]interface{}{
|
||||
"Results": decoratedResults,
|
||||
"Query": query,
|
||||
"Fetched": FormatElapsedTime(elapsedTime),
|
||||
"Page": page,
|
||||
"HasPrevPage": hasPrevPage,
|
||||
"HasNextPage": len(combinedResults) >= 50,
|
||||
"NoResults": len(combinedResults) == 0,
|
||||
"LanguageOptions": languageOptions,
|
||||
"CurrentLang": settings.SearchLanguage,
|
||||
"Theme": settings.Theme,
|
||||
"Safe": settings.SafeSearch,
|
||||
"IsThemeDark": settings.IsThemeDark,
|
||||
"Trans": Translate,
|
||||
"HardCacheEnabled": config.DriveCacheEnabled,
|
||||
}
|
||||
|
||||
if instant != nil {
|
||||
data["Instant"] = instant
|
||||
}
|
||||
|
||||
renderTemplate(w, "text.html", data)
|
||||
}
|
||||
|
||||
func HandleTextSearch(w http.ResponseWriter, settings UserSettings, query string, page int) {
|
||||
startTime := time.Now()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue