Make slight updates

This commit is contained in:
Henrique Dias 2016-06-21 15:02:30 +01:00
parent 929f0b3454
commit c54dccec17
2 changed files with 48 additions and 29 deletions

View File

@ -163,7 +163,15 @@ func (fi FileInfo) serveSingleFile(w http.ResponseWriter, r *http.Request, c *Co
}, },
} }
return page.PrintAsHTML(w, "single") templates := []string{"single", "actions", "base"}
for _, t := range templates {
code, err := page.AddTemplate(t, Asset)
if err != nil {
return code, err
}
}
return page.PrintAsHTML(w)
} }
func (fi FileInfo) serveListing(w http.ResponseWriter, r *http.Request, c *Config) (int, error) { func (fi FileInfo) serveListing(w http.ResponseWriter, r *http.Request, c *Config) (int, error) {
@ -218,7 +226,15 @@ func (fi FileInfo) serveListing(w http.ResponseWriter, r *http.Request, c *Confi
}, },
} }
return page.PrintAsHTML(w, "listing") templates := []string{"listing", "actions", "base"}
for _, t := range templates {
code, err := page.AddTemplate(t, Asset)
if err != nil {
return code, err
}
}
return page.PrintAsHTML(w)
} }
func (fi FileInfo) loadDirectoryContents(file http.File, c *Config) (*Listing, error) { func (fi FileInfo) loadDirectoryContents(file http.File, c *Config) (*Listing, error) {

27
page.go
View File

@ -11,8 +11,12 @@ import (
// Page contains the informations and functions needed to show the page // Page contains the informations and functions needed to show the page
type Page struct { type Page struct {
Info *PageInfo Info *PageInfo
Tpl *template.Template
} }
// AssetFunc is an Assets function
type AssetFunc func(name string) ([]byte, error)
// PageInfo contains the information of a page // PageInfo contains the information of a page
type PageInfo struct { type PageInfo struct {
Name string Name string
@ -67,15 +71,11 @@ func (p PageInfo) PreviousLink() string {
return parts[len(parts)-2] return parts[len(parts)-2]
} }
// PrintAsHTML formats the page in HTML and executes the template // AddTemplate adds a template file to the page template
func (p Page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, error) { func (p *Page) AddTemplate(name string, assets AssetFunc) (int, error) {
templates = append(templates, "actions", "base")
var tpl *template.Template
// For each template, add it to the the tpl variable
for i, t := range templates {
// Get the template from the assets // Get the template from the assets
page, err := Asset("templates/" + t + ".tmpl") page, err := assets("templates/" + name + ".tmpl")
// Check if there is some error. If so, the template doesn't exist // Check if there is some error. If so, the template doesn't exist
if err != nil { if err != nil {
@ -85,20 +85,23 @@ func (p Page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, erro
// If it's the first iteration, creates a new template and add the // If it's the first iteration, creates a new template and add the
// functions map // functions map
if i == 0 { if p.Tpl == nil {
tpl, err = template.New(t).Parse(string(page)) p.Tpl, err = template.New(name).Parse(string(page))
} else { } else {
tpl, err = tpl.Parse(string(page)) p.Tpl, err = p.Tpl.Parse(string(page))
} }
if err != nil { if err != nil {
log.Print(err) log.Print(err)
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
} }
} return 0, nil
}
// PrintAsHTML formats the page in HTML and executes the template
func (p Page) PrintAsHTML(w http.ResponseWriter) (int, error) {
w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("Content-Type", "text/html; charset=utf-8")
err := tpl.Execute(w, p.Info) err := p.Tpl.Execute(w, p.Info)
if err != nil { if err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err