router: handle static assets, handle page errors
This commit is contained in:
parent
c526153f60
commit
06a07af36d
@ -12,6 +12,7 @@ const (
|
|||||||
type ArchiveServer struct {
|
type ArchiveServer struct {
|
||||||
timezone *time.Location
|
timezone *time.Location
|
||||||
cfg *Config
|
cfg *Config
|
||||||
|
startup time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewArchiveServer(cfg *Config) (*ArchiveServer, error) {
|
func NewArchiveServer(cfg *Config) (*ArchiveServer, error) {
|
||||||
@ -37,5 +38,6 @@ func NewArchiveServer(cfg *Config) (*ArchiveServer, error) {
|
|||||||
return &ArchiveServer{
|
return &ArchiveServer{
|
||||||
timezone: tz,
|
timezone: tz,
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
|
startup: time.Now(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
@ -12,23 +12,34 @@ type ArchiveState struct {
|
|||||||
logBestSlug string
|
logBestSlug string
|
||||||
query string
|
query string
|
||||||
queryIsRegex bool
|
queryIsRegex bool
|
||||||
nonce string
|
|
||||||
ym YearMonth
|
ym YearMonth
|
||||||
page int
|
page int
|
||||||
highestPage int
|
highestPage int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewArchiveState(svr *ArchiveServer, log *LogSource) (*ArchiveState, error) {
|
func NewArchiveState(svr *ArchiveServer) *ArchiveState {
|
||||||
logBestSlug, err := svr.bestSlugFor(log)
|
return &ArchiveState{svr: svr}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *ArchiveState) selectSource(log *LogSource) error {
|
||||||
|
logBestSlug, err := this.svr.bestSlugFor(log)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ArchiveState{
|
this.log = log
|
||||||
svr: svr,
|
this.logBestSlug = logBestSlug
|
||||||
log: log,
|
return nil
|
||||||
logBestSlug: logBestSlug,
|
}
|
||||||
}, 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) {
|
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])
|
slug, _ := this.svr.bestSlugFor(&this.svr.cfg.Logs[i])
|
||||||
current := (this.log == &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(`
|
w.Write([]byte(`
|
||||||
@ -165,7 +176,7 @@ func (this *ArchiveState) renderTemplateHead(w http.ResponseWriter) {
|
|||||||
func (this *ArchiveState) renderTemplateFoot(w http.ResponseWriter) {
|
func (this *ArchiveState) renderTemplateFoot(w http.ResponseWriter) {
|
||||||
w.Write([]byte(`
|
w.Write([]byte(`
|
||||||
</div>
|
</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>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
`))
|
`))
|
||||||
|
21
Router.go
21
Router.go
@ -1,6 +1,7 @@
|
|||||||
package archive
|
package archive
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -62,11 +63,29 @@ func (this *ArchiveServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
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 {
|
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)
|
this.legacyRoute(w, r)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// TODO
|
arc.renderError(w, "Unknown route.")
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user