Extract template loading
This commit is contained in:
parent
076a0faea3
commit
0f9752452d
|
|
@ -53,7 +53,8 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
type mainHandler struct {
|
type mainHandler struct {
|
||||||
Backend *memoryBackend
|
Backend *memoryBackend
|
||||||
|
Templates *template.Template
|
||||||
}
|
}
|
||||||
|
|
||||||
type session struct {
|
type session struct {
|
||||||
|
|
@ -76,6 +77,19 @@ type settingsPage struct {
|
||||||
Session session
|
Session session
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newMainHandler(backend *memoryBackend) (*mainHandler, error) {
|
||||||
|
h := &mainHandler{Backend: backend}
|
||||||
|
|
||||||
|
templates, err := template.ParseGlob("*.html")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
h.Templates = templates
|
||||||
|
return h, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
conn := pool.Get()
|
conn := pool.Get()
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
@ -117,18 +131,10 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
t, err := template.ParseFiles(
|
|
||||||
os.Getenv("EKSTER_TEMPLATES") + "/index.html",
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(w, "ERROR: %q\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var page indexPage
|
var page indexPage
|
||||||
page.Session = sess
|
page.Session = sess
|
||||||
|
|
||||||
err = t.ExecuteTemplate(w, "index.html", page)
|
err = h.Templates.ExecuteTemplate(w, "index.html", page)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(w, "ERROR: %q\n", err)
|
fmt.Fprintf(w, "ERROR: %q\n", err)
|
||||||
return
|
return
|
||||||
|
|
@ -226,18 +232,10 @@ func (h *mainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
t, err := template.ParseFiles(
|
|
||||||
os.Getenv("EKSTER_TEMPLATES") + "/settings.html",
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(w, "ERROR: %q\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var page settingsPage
|
var page settingsPage
|
||||||
page.Session = sess
|
page.Session = sess
|
||||||
|
|
||||||
err = t.ExecuteTemplate(w, "settings.html", page)
|
err = h.Templates.ExecuteTemplate(w, "settings.html", page)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(w, "ERROR: %q\n", err)
|
fmt.Fprintf(w, "ERROR: %q\n", err)
|
||||||
return
|
return
|
||||||
|
|
@ -372,10 +370,11 @@ func main() {
|
||||||
http.Handle("/incoming/", &incomingHandler{
|
http.Handle("/incoming/", &incomingHandler{
|
||||||
Backend: &hubBackend,
|
Backend: &hubBackend,
|
||||||
})
|
})
|
||||||
|
handler, err := newMainHandler(backend.(*memoryBackend))
|
||||||
http.Handle("/", &mainHandler{
|
if err != nil {
|
||||||
Backend: backend.(*memoryBackend),
|
log.Fatal(err)
|
||||||
})
|
}
|
||||||
|
http.Handle("/", handler)
|
||||||
|
|
||||||
backend.(*memoryBackend).run()
|
backend.(*memoryBackend).run()
|
||||||
hubBackend.run()
|
hubBackend.run()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user