Fixed cache template and handleer
This commit is contained in:
@@ -53,6 +53,10 @@ type Context struct {
|
||||
Title string
|
||||
Meta Meta
|
||||
|
||||
CachedURL string
|
||||
CachedTitle string
|
||||
CachedContent string
|
||||
|
||||
Matches uint64
|
||||
Duration time.Duration
|
||||
Results Results
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +1,8 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>veri</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<style type="text/css">
|
||||
body{max-width:920px;margin:0 auto;padding:2em;font-family:-apple-system,blinkmacsystemfont,"Helvetica Neue","Helvetica","Segoe UI",roboto,oxygen-sans,ubuntu,cantarell,"Helvetica Neue",sans-serif;color:#394b41;}
|
||||
.small{font-size:0.8em;color:grey;}
|
||||
.placeholder{font-style:oblique;}
|
||||
.bold{font-weight:bold;}
|
||||
a{color:blue;}
|
||||
a:hover{color:red;}
|
||||
ol{padding:1em;}
|
||||
li{border-bottom:solid 2px grey;}
|
||||
input{width:100%;font-size:1.5em;}
|
||||
pre{white-space:pre-wrap;}
|
||||
@media (max-width:40em) {
|
||||
body{padding:1em;}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<a href="/"><h1>veri</h1></a>
|
||||
<p class="bold">This is a rudimentary cached copy of <a href="{{ .URL }}">{{ .URL }}</a>, go there to get the latest copy. No ownership of this content is implied.</p>
|
||||
{{ if not . }}<p>No cache</p>{{ end }}
|
||||
<h2>{{ .Title }}</h2>
|
||||
<pre>
|
||||
{{ .Content }}
|
||||
</pre>
|
||||
<p>If your page appears here and you do not want it to, please contact <a href="mailto:~ols/indexing@lists.sr.ht">~ols/indexing@lists.sr.ht</a>. Contact the same email if you want your page considered for indexing. Be aware this is a public mailing list.</p>
|
||||
<p><a href="https://sr.ht/~ols/veri/">about</a></p>
|
||||
</body>
|
||||
</html>
|
||||
{{define "content"}}
|
||||
<div class="container">
|
||||
<p>This is a rudimentary cached copy of <a href="{{ .CachedURL }}">{{ .CachedURL }}</a>, go there to get the latest copy. No ownership of this content is implied.</p>
|
||||
<h2>{{ .CachedTitle }}</h2>
|
||||
<pre>{{ .CachedContent }}</pre>
|
||||
<small>If your page appears here and you do not want it to, please contact <a href="/support">Support</a></small>
|
||||
</div>
|
||||
{{ end }}
|
||||
|
||||
Reference in New Issue
Block a user