Fixed cache template and handleer
This commit is contained in:
@@ -53,6 +53,10 @@ type Context struct {
|
|||||||
Title string
|
Title string
|
||||||
Meta Meta
|
Meta Meta
|
||||||
|
|
||||||
|
CachedURL string
|
||||||
|
CachedTitle string
|
||||||
|
CachedContent string
|
||||||
|
|
||||||
Matches uint64
|
Matches uint64
|
||||||
Duration time.Duration
|
Duration time.Duration
|
||||||
Results Results
|
Results Results
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -140,8 +143,42 @@ func (s *Server) AddHandler() httprouter.Handle {
|
|||||||
|
|
||||||
// CacheHandler ...
|
// CacheHandler ...
|
||||||
func (s *Server) CacheHandler() httprouter.Handle {
|
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)
|
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)
|
s.render("cache", w, ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,32 +1,8 @@
|
|||||||
<html>
|
{{define "content"}}
|
||||||
<head>
|
<div class="container">
|
||||||
<title>veri</title>
|
<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>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<h2>{{ .CachedTitle }}</h2>
|
||||||
<style type="text/css">
|
<pre>{{ .CachedContent }}</pre>
|
||||||
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>If your page appears here and you do not want it to, please contact <a href="/support">Support</a></small>
|
||||||
.small{font-size:0.8em;color:grey;}
|
</div>
|
||||||
.placeholder{font-style:oblique;}
|
{{ end }}
|
||||||
.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>
|
|
||||||
|
|||||||
Reference in New Issue
Block a user