From 17088383167b71d048261b726969ee261870fd87 Mon Sep 17 00:00:00 2001 From: James Mills Date: Wed, 3 Feb 2021 00:14:37 +1000 Subject: [PATCH] Fixed cache template and handleer --- internal/context.go | 4 ++++ internal/handlers.go | 39 +++++++++++++++++++++++++++++++++- internal/templates/cache.html | 40 +++++++---------------------------- 3 files changed, 50 insertions(+), 33 deletions(-) diff --git a/internal/context.go b/internal/context.go index e1e4fed..98a249c 100644 --- a/internal/context.go +++ b/internal/context.go @@ -53,6 +53,10 @@ type Context struct { Title string Meta Meta + CachedURL string + CachedTitle string + CachedContent string + Matches uint64 Duration time.Duration Results Results diff --git a/internal/handlers.go b/internal/handlers.go index ca93a24..e0c2dac 100644 --- a/internal/handlers.go +++ b/internal/handlers.go @@ -1,9 +1,12 @@ package internal import ( + "encoding/json" "fmt" "html/template" + "io/ioutil" "net/http" + "path/filepath" "strings" "time" @@ -140,8 +143,42 @@ func (s *Server) AddHandler() httprouter.Handle { // CacheHandler ... func (s *Server) CacheHandler() httprouter.Handle { - return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { + return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { ctx := NewContext(s.config, s.db, r) + + hash := p.ByName("hash") + if hash == "" { + http.Error(w, "Bad Request", http.StatusBadRequest) + return + } + + fn := filepath.Join(s.config.Data, cacheDir, fmt.Sprintf("%s.json", hash)) + if !FileExists(fn) { + ctx.Error = true + ctx.Message = "Cached page not found!" + s.render("404", w, ctx) + return + } + + var entry Entry + + data, err := ioutil.ReadFile(fn) + if err != nil { + log.WithError(err).Error("error reading cached entry") + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } + + if err := json.Unmarshal(data, &entry); err != nil { + log.WithError(err).Error("error unmarshalling cached entry") + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } + + ctx.CachedURL = entry.URL + ctx.CachedTitle = entry.Title + ctx.CachedContent = entry.Content + s.render("cache", w, ctx) } } diff --git a/internal/templates/cache.html b/internal/templates/cache.html index d27db0e..9e0922b 100644 --- a/internal/templates/cache.html +++ b/internal/templates/cache.html @@ -1,32 +1,8 @@ - - -veri - - - - -

veri

-

This is a rudimentary cached copy of {{ .URL }}, go there to get the latest copy. No ownership of this content is implied.

-{{ if not . }}

No cache

{{ end }} -

{{ .Title }}

-
-{{ .Content }}
-
-

If your page appears here and you do not want it to, please contact ~ols/indexing@lists.sr.ht. Contact the same email if you want your page considered for indexing. Be aware this is a public mailing list.

-

about

- - +{{define "content"}} +
+

This is a rudimentary cached copy of {{ .CachedURL }}, go there to get the latest copy. No ownership of this content is implied.

+

{{ .CachedTitle }}

+
{{ .CachedContent }}
+ If your page appears here and you do not want it to, please contact Support +
+{{ end }}