make some updates

This commit is contained in:
Henrique Dias 2016-06-10 22:38:52 +01:00
parent 65d3c33381
commit 77e5bd2b9e
4 changed files with 66 additions and 52 deletions

View File

@ -2,7 +2,8 @@
// sources:
// assets/public/css/styles.css
// assets/public/js/application.js
// assets/templates/template.tmpl
// assets/templates/base.tmpl
// assets/templates/listing.tmpl
// DO NOT EDIT!
package filemanager
@ -65,10 +66,28 @@ func publicJsApplicationJs() (*asset, error) {
return a, err
}
// templatesTemplateTmpl reads file data from disk. It returns an error on failure.
func templatesTemplateTmpl() (*asset, error) {
path := "D:\\Code\\Go\\src\\github.com\\hacdias\\caddy-filemanager\\assets\\templates\\template.tmpl"
name := "templates/template.tmpl"
// templatesBaseTmpl reads file data from disk. It returns an error on failure.
func templatesBaseTmpl() (*asset, error) {
path := "D:\\Code\\Go\\src\\github.com\\hacdias\\caddy-filemanager\\assets\\templates\\base.tmpl"
name := "templates/base.tmpl"
bytes, err := bindataRead(path, name)
if err != nil {
return nil, err
}
fi, err := os.Stat(path)
if err != nil {
err = fmt.Errorf("Error reading asset info %s at %s: %v", name, path, err)
}
a := &asset{bytes: bytes, info: fi}
return a, err
}
// templatesListingTmpl reads file data from disk. It returns an error on failure.
func templatesListingTmpl() (*asset, error) {
path := "D:\\Code\\Go\\src\\github.com\\hacdias\\caddy-filemanager\\assets\\templates\\listing.tmpl"
name := "templates/listing.tmpl"
bytes, err := bindataRead(path, name)
if err != nil {
return nil, err
@ -137,7 +156,8 @@ func AssetNames() []string {
var _bindata = map[string]func() (*asset, error){
"public/css/styles.css": publicCssStylesCss,
"public/js/application.js": publicJsApplicationJs,
"templates/template.tmpl": templatesTemplateTmpl,
"templates/base.tmpl": templatesBaseTmpl,
"templates/listing.tmpl": templatesListingTmpl,
}
// AssetDir returns the file names below a certain
@ -189,7 +209,8 @@ var _bintree = &bintree{nil, map[string]*bintree{
}},
}},
"templates": &bintree{nil, map[string]*bintree{
"template.tmpl": &bintree{templatesTemplateTmpl, map[string]*bintree{}},
"base.tmpl": &bintree{templatesBaseTmpl, map[string]*bintree{}},
"listing.tmpl": &bintree{templatesListingTmpl, map[string]*bintree{}},
}},
}}

View File

@ -218,7 +218,7 @@ func (f FileManager) ServeListing(w http.ResponseWriter, r *http.Request, reques
w.Header().Set("Content-Type", "application/json; charset=utf-8")
default: // There's no 'application/json' in the 'Accept' header; browse normally
if buf, err = f.formatAsHTML(listing, bc); err != nil {
if buf, err = f.formatAsHTML(listing, bc, "listing"); err != nil {
return http.StatusInternalServerError, err
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
@ -240,10 +240,3 @@ func (f FileManager) formatAsJSON(listing *Listing, bc *Config) (*bytes.Buffer,
_, err = buf.Write(marsh)
return buf, err
}
func (f FileManager) formatAsHTML(listing *Listing, fmc *Config) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
listing.StyleSheet = fmc.StyleSheet
err := Template.Execute(buf, listing)
return buf, err
}

57
page.go
View File

@ -1,36 +1,51 @@
package filemanager
import (
"errors"
"net/http"
"bytes"
"html/template"
"log"
)
// Page is the base type for each page
type Page struct {
GET, POST, PUT, DELETE func(w http.ResponseWriter, r *http.Request) (int, error)
DoGET, DoPOST, DoPUT, DoDELETE bool
Config *Config
Data interface{}
}
// Route redirects the request for the respective method
func (p Page) Route(w http.ResponseWriter, r *http.Request) (int, error) {
switch r.Method {
case "DELETE":
if p.DoDELETE {
return p.DELETE(w, r)
func (f FileManager) formatAsHTML(data interface{}, fmc *Config, templates ...string) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
pg := &Page{
Config: fmc,
Data: data,
}
case "POST":
if p.DoPOST {
return p.POST(w, r)
templates = append(templates, "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
page, err := Asset("templates/" + t + ".tmpl")
// Check if there is some error. If so, the template doesn't exist
if err != nil {
log.Print(err)
return new(bytes.Buffer), err
}
case "GET":
if p.DoGET {
return p.GET(w, r)
// If it's the first iteration, creates a new template and add the
// functions map
if i == 0 {
tpl, err = template.New(t).Parse(string(page))
} else {
tpl, err = tpl.Parse(string(page))
}
case "PUT":
if p.DoPUT {
return p.PUT(w, r)
if err != nil {
log.Print(err)
return new(bytes.Buffer), err
}
}
return http.StatusMethodNotAllowed, errors.New("Invalid method.")
err := Template.Execute(buf, pg)
return buf, err
}

View File

@ -4,7 +4,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"text/template"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
@ -19,20 +18,6 @@ func init() {
// setup configures a new Browse middleware instance.
func setup(c *caddy.Controller) error {
// Second argument would be the template file to use
tplBytes, err := Asset("templates/template.tmpl")
if err != nil {
return err
}
tplText := string(tplBytes)
// Build the template
tpl, err := template.New("listing").Parse(tplText)
if err != nil {
return err
}
Template = tpl
configs, err := fileManagerParse(c)
if err != nil {
return err