2017-08-13 02:41:14 +00:00
|
|
|
package archive
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2017-08-13 03:47:03 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"math"
|
2017-08-13 02:41:14 +00:00
|
|
|
"net/http"
|
2017-08-13 03:47:03 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
pageNotSet int = -1
|
2017-08-13 02:41:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ArchiveState struct {
|
|
|
|
svr *ArchiveServer
|
|
|
|
log *LogSource
|
|
|
|
logBestSlug string
|
|
|
|
query string
|
|
|
|
queryIsRegex bool
|
|
|
|
ym YearMonth
|
|
|
|
page int
|
|
|
|
highestPage int
|
|
|
|
}
|
|
|
|
|
2017-08-13 03:04:40 +00:00
|
|
|
func NewArchiveState(svr *ArchiveServer) *ArchiveState {
|
2017-08-13 03:47:03 +00:00
|
|
|
return &ArchiveState{
|
|
|
|
svr: svr,
|
|
|
|
page: pageNotSet,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ArchiveState) selectSource(log *LogSource, slug string) {
|
|
|
|
this.log = log
|
|
|
|
this.logBestSlug = slug
|
2017-08-13 03:04:40 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 03:47:03 +00:00
|
|
|
// renderView renders a single page of log entries.
|
|
|
|
// - Mandatory: log, ym
|
|
|
|
// - Optional: page
|
|
|
|
func (this *ArchiveState) renderView(w http.ResponseWriter) {
|
|
|
|
if this.log == nil {
|
|
|
|
this.renderError(w, "Invalid log source selected")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fname, err := this.svr.LogFile(this.log, this.ym)
|
2017-08-13 02:41:14 +00:00
|
|
|
if err != nil {
|
2017-08-13 03:47:03 +00:00
|
|
|
this.renderError(w, err.Error())
|
|
|
|
return
|
2017-08-13 02:41:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 03:47:03 +00:00
|
|
|
fc, err := ioutil.ReadFile(fname)
|
|
|
|
if err != nil {
|
|
|
|
this.renderError(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(string(fc), "\n")
|
|
|
|
|
|
|
|
this.highestPage = int(math.Ceil(float64(len(lines))/float64(this.svr.cfg.LinesPerPage))) - 1
|
|
|
|
if this.page == pageNotSet || this.page > this.highestPage {
|
|
|
|
this.page = this.highestPage
|
|
|
|
}
|
|
|
|
if this.page < 0 {
|
|
|
|
this.page = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
startLine := this.svr.cfg.LinesPerPage * this.page
|
|
|
|
endLine := this.svr.cfg.LinesPerPage * (this.page + 1)
|
|
|
|
if endLine > len(lines) {
|
|
|
|
endLine = len(lines)
|
|
|
|
}
|
|
|
|
|
|
|
|
output := ""
|
|
|
|
for i := startLine; i < endLine; i += 1 {
|
|
|
|
output += template.HTMLEscapeString(lines[i]) + "<br>\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
this.renderTemplate(w, []byte(output))
|
2017-08-13 03:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ArchiveState) renderError(w http.ResponseWriter, msg string) {
|
|
|
|
this.renderTemplate(w, []byte(template.HTMLEscapeString(msg)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ArchiveState) renderTemplate(w http.ResponseWriter, body []byte) {
|
|
|
|
this.renderTemplateHead(w)
|
|
|
|
w.Write(body)
|
|
|
|
this.renderTemplateFoot(w)
|
2017-08-13 02:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ArchiveState) renderTemplateHead(w http.ResponseWriter) {
|
|
|
|
w.Header().Set(`Content-Type`, `text/html; charset=UTF-8`)
|
|
|
|
w.Header().Set(`X-UA-Compatible`, `IE=Edge`)
|
|
|
|
w.WriteHeader(200)
|
|
|
|
|
|
|
|
title := `Archives`
|
|
|
|
if this.log != nil {
|
|
|
|
title = this.log.Description + ` Archives`
|
|
|
|
}
|
|
|
|
|
|
|
|
showPageURLs := (this.log != nil && len(this.query) == 0)
|
|
|
|
|
|
|
|
w.Write([]byte(`<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
|
|
|
|
<title>` + template.HTMLEscapeString(title) + `</title>
|
|
|
|
<link rel="stylesheet" type="text/css" href="/style.css">
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div class="layout-top nav">
|
|
|
|
|
|
|
|
<div id="tr1" style="display:none;"></div>
|
|
|
|
<div id="tr2" style="display:none;"></div>
|
|
|
|
<div class="ddmenu" id="spm" style="display:none;">
|
|
|
|
<a href="/">Latest</a>
|
|
|
|
<a onclick="fontSize(1);">Font increase</a>
|
|
|
|
<a onclick="fontSize(-1);">Font decrease</a>
|
|
|
|
<a href="/download" onclick="return confirm('Are you sure you want to download a backup?');">Download backup</a>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<a onclick="toggleMenu();"><div id="logo" class="layout-pushdown"></div></a>
|
|
|
|
|
|
|
|
<span class="area-nav">
|
|
|
|
|
|
|
|
<form method="GET" id="frmHub">
|
|
|
|
<select name="h" id="selHub">
|
|
|
|
`))
|
|
|
|
|
|
|
|
for i, h := range this.svr.cfg.Logs {
|
|
|
|
slug, _ := this.svr.bestSlugFor(&this.svr.cfg.Logs[i])
|
|
|
|
current := (this.log == &this.svr.cfg.Logs[i])
|
|
|
|
|
2017-08-13 03:04:40 +00:00
|
|
|
w.Write([]byte(`<option value="` + template.HTMLEscapeString(slug) + `" ` + attr(current, "selected") + `>` + template.HTMLEscapeString(h.Description) + `</option>`))
|
2017-08-13 02:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Write([]byte(`
|
|
|
|
</select>
|
|
|
|
</form>
|
|
|
|
`))
|
|
|
|
|
|
|
|
if showPageURLs {
|
|
|
|
w.Write([]byte(`
|
|
|
|
|
|
|
|
<form method="GET">
|
|
|
|
<input type="hidden" name="h" value="` + template.HTMLEscapeString(this.logBestSlug) + `">
|
|
|
|
<select id="seldate" onchange="setYM(this);">
|
|
|
|
`))
|
|
|
|
|
|
|
|
// Generate month dropdown options
|
|
|
|
|
|
|
|
lastY := -1
|
2017-08-13 03:47:03 +00:00
|
|
|
limit := this.log.LatestDate().Next() // one off the end
|
|
|
|
for ympair := this.log.EarliestDate(); !ympair.Equals(limit); ympair = ympair.Next() {
|
2017-08-13 02:41:14 +00:00
|
|
|
if ympair.Year != lastY {
|
|
|
|
if lastY != -1 {
|
|
|
|
w.Write([]byte(`</optgroup>`))
|
|
|
|
}
|
2017-08-13 03:47:03 +00:00
|
|
|
w.Write([]byte(`<optgroup label="` + fmt.Sprintf("%d", ympair.Year) + `">`))
|
2017-08-13 02:41:14 +00:00
|
|
|
lastY = ympair.Year
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write([]byte(fmt.Sprintf(`<option value="%d-%d" %s>%s</option>`, ympair.Year, ympair.Month, attr(ympair.Equals(this.ym), "selected"), template.HTMLEscapeString(ympair.Month.String()))))
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
pageBase := fmt.Sprintf(`/%s/%d/%d`, this.logBestSlug, this.ym.Year, this.ym.Month)
|
|
|
|
|
|
|
|
previousPage := this.page - 1
|
|
|
|
if previousPage < 0 {
|
|
|
|
previousPage = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
nextPage := this.page + 1
|
|
|
|
if nextPage > this.highestPage {
|
|
|
|
nextPage = this.highestPage
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write([]byte(`
|
|
|
|
</optgroup>
|
|
|
|
|
|
|
|
</select>
|
|
|
|
<input type="hidden" name="y" id="f_y" value="">
|
|
|
|
<input type="hidden" name="m" id="f_m" value="">
|
|
|
|
<input type="hidden" name="p" value="0" >
|
|
|
|
</form>
|
|
|
|
|
|
|
|
<div class="mini-separator layout-pushdown"></div>
|
|
|
|
|
|
|
|
<a class="btn" href="` + pageBase + `/page-0">«</a><a
|
|
|
|
class="btn" id="pgprev" href="` + pageBase + `/page-` + fmt.Sprintf("%d", previousPage) + `">‹</a>
|
|
|
|
` + fmt.Sprintf("%d", this.page) + `
|
|
|
|
<a class="btn" id="pgnext" href="` + pageBase + `/page-` + fmt.Sprintf("%d", nextPage) + `">›</a><a
|
|
|
|
class="btn" href="` + pageBase + `">»</a>
|
|
|
|
`))
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write([]byte(`
|
|
|
|
<div class="pad"></div>
|
|
|
|
|
|
|
|
</span>
|
|
|
|
|
|
|
|
<span class="area-search">
|
|
|
|
|
|
|
|
<form method="GET">
|
|
|
|
<input type="hidden" name="h" value="` + template.HTMLEscapeString(this.logBestSlug) + `">
|
|
|
|
<input type="text" id="searchbox" name="q" value="` + template.HTMLEscapeString(this.query) + `" placeholder="Search...">
|
|
|
|
<input type="submit" value="»">
|
|
|
|
<input type="checkbox" class="layout-pushdown" name="rx" value="1" title="PCRE Regular Expression" ` + attr(this.queryIsRegex, "checked") + `>
|
|
|
|
</form>
|
|
|
|
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="layout-body" id="chatarea">
|
|
|
|
`))
|
|
|
|
|
|
|
|
// Header ends
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ArchiveState) renderTemplateFoot(w http.ResponseWriter) {
|
|
|
|
w.Write([]byte(`
|
|
|
|
</div>
|
2017-08-13 03:04:40 +00:00
|
|
|
<script type="text/javascript" src="/archive.js?nonce=` + fmt.Sprintf("%d", this.svr.startup.Unix()) + `"></script>
|
2017-08-13 02:41:14 +00:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`))
|
|
|
|
}
|