router: handle static assets, handle page errors

This commit is contained in:
mappu 2017-08-13 15:04:40 +12:00
parent c526153f60
commit 06a07af36d
3 changed files with 44 additions and 12 deletions

View File

@ -12,6 +12,7 @@ const (
type ArchiveServer struct {
timezone *time.Location
cfg *Config
startup time.Time
}
func NewArchiveServer(cfg *Config) (*ArchiveServer, error) {
@ -37,5 +38,6 @@ func NewArchiveServer(cfg *Config) (*ArchiveServer, error) {
return &ArchiveServer{
timezone: tz,
cfg: cfg,
startup: time.Now(),
}, nil
}

View File

@ -12,23 +12,34 @@ type ArchiveState struct {
logBestSlug string
query string
queryIsRegex bool
nonce string
ym YearMonth
page int
highestPage int
}
func NewArchiveState(svr *ArchiveServer, log *LogSource) (*ArchiveState, error) {
logBestSlug, err := svr.bestSlugFor(log)
func NewArchiveState(svr *ArchiveServer) *ArchiveState {
return &ArchiveState{svr: svr}
}
func (this *ArchiveState) selectSource(log *LogSource) error {
logBestSlug, err := this.svr.bestSlugFor(log)
if err != nil {
return nil, err
return err
}
return &ArchiveState{
svr: svr,
log: log,
logBestSlug: logBestSlug,
}, nil
this.log = log
this.logBestSlug = logBestSlug
return nil
}
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)
}
func (this *ArchiveState) renderTemplateHead(w http.ResponseWriter) {
@ -75,7 +86,7 @@ func (this *ArchiveState) renderTemplateHead(w http.ResponseWriter) {
slug, _ := this.svr.bestSlugFor(&this.svr.cfg.Logs[i])
current := (this.log == &this.svr.cfg.Logs[i])
w.Write([]byte(`<option value="` + template.HTMLEscapeString(slug) + attr(current, " selected") + `>` + template.HTMLEscapeString(h.Description) + `</option>`))
w.Write([]byte(`<option value="` + template.HTMLEscapeString(slug) + `" ` + attr(current, "selected") + `>` + template.HTMLEscapeString(h.Description) + `</option>`))
}
w.Write([]byte(`
@ -165,7 +176,7 @@ func (this *ArchiveState) renderTemplateHead(w http.ResponseWriter) {
func (this *ArchiveState) renderTemplateFoot(w http.ResponseWriter) {
w.Write([]byte(`
</div>
<script type="text/javascript" src="/archive.js?nonce=` + this.nonce + `"></script>
<script type="text/javascript" src="/archive.js?nonce=` + fmt.Sprintf("%d", this.svr.startup.Unix()) + `"></script>
</body>
</html>
`))

View File

@ -1,6 +1,7 @@
package archive
import (
"bytes"
"errors"
"fmt"
"net/http"
@ -62,11 +63,29 @@ func (this *ArchiveServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
if len(r.URL.Path) == 0 {
http.Error(w, "Invalid request", http.StatusBadRequest)
return
}
// Handle static assets
static, err := Asset(r.URL.Path[1:])
if err == nil {
http.ServeContent(w, r, r.URL.Path[1:], this.startup, bytes.NewReader(static))
return
}
// Handle application routes
arc := NewArchiveState(this)
if len(r.URL.Query().Get("y")) > 0 || len(r.URL.Query().Get("q")) > 0 || len(r.URL.Query().Get("h")) > 0 {
this.legacyRoute(w, r)
} else {
// TODO
arc.renderError(w, "Unknown route.")
}
}