~ols/veri-index unlisted

70b323faf4b771f7192753663e561bf33e633b95 — Oliver Leaver-Smith 9 months ago 294ef97
add cache page
3 files changed, 54 insertions(+), 1 deletions(-)

A cache.gohtml
M main.go
M search.gohtml
A cache.gohtml => cache.gohtml +32 -0
@@ 0,0 1,32 @@
<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>

M main.go => main.go +21 -0
@@ 8,6 8,7 @@ import (
	"net/http"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"time"
	"unicode"


@@ 42,6 43,9 @@ func main() {
	r.Path("/search").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		DoSearch(w, r, Idx, entries)
	})
	r.Path("/cache").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ViewCache(w, r, entries)
	})
	http.Handle("/", r)
	srv := &http.Server{
		Handler:      r,


@@ 73,6 77,7 @@ func DoSearch(w http.ResponseWriter, r *http.Request, Idx index, entries []strin
		// TODO figure out wtf this is an off-by-one, for now an id-1 does the trick
		contents, _ := ioutil.ReadFile(entries[id-1])
		json.Unmarshal([]byte(contents), &entry)
		entry.ID = id - 1
		retEntries = append(retEntries, entry)
	}
	tmpl, err := template.ParseFiles("search.gohtml")


@@ 85,6 90,22 @@ func DoSearch(w http.ResponseWriter, r *http.Request, Idx index, entries []strin
	}
}

func ViewCache(w http.ResponseWriter, r *http.Request, entries []string) {
	var entry Entry
	tid := r.URL.Query().Get("id")
	id, _ := strconv.Atoi(tid)
	contents, _ := ioutil.ReadFile(entries[id])
	json.Unmarshal([]byte(contents), &entry)
	tmpl, err := template.ParseFiles("cache.gohtml")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
	err = tmpl.Execute(w, entry)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

func MakeIndex(entries []string) index {
	var entriesDoc []string
	var entry Entry

M search.gohtml => search.gohtml +1 -1
@@ 27,7 27,7 @@ body{padding:1em;}
<ol>
{{ if not . }}<p>No search results</p>{{ end }}
{{ range . }}
<li><a href="{{ .URL }}"><h3>{{ .Title }}</h3></a><p{{ if .Summary }}>{{ .Summary }}{{ else }} class="placeholder">No summary available{{ end }}</p><p class="small"><a href="{{ .URL }}">{{ .URL }}</a> {{ .Length }}</p>
<li><a href="{{ .URL }}"><h3>{{ .Title }}</h3></a><p{{ if .Summary }}>{{ .Summary }}{{ else }} class="placeholder">No summary available{{ end }}</p><p class="small"><a href="{{ .URL }}">{{ .URL }}</a> {{ .Length }} <a href="/cache?id={{ .ID }}">View cached</a></p>
{{ end }}
</ol>
<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>