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: // sources:
// assets/public/css/styles.css // assets/public/css/styles.css
// assets/public/js/application.js // assets/public/js/application.js
// assets/templates/template.tmpl // assets/templates/base.tmpl
// assets/templates/listing.tmpl
// DO NOT EDIT! // DO NOT EDIT!
package filemanager package filemanager
@ -65,10 +66,28 @@ func publicJsApplicationJs() (*asset, error) {
return a, err return a, err
} }
// templatesTemplateTmpl reads file data from disk. It returns an error on failure. // templatesBaseTmpl reads file data from disk. It returns an error on failure.
func templatesTemplateTmpl() (*asset, error) { func templatesBaseTmpl() (*asset, error) {
path := "D:\\Code\\Go\\src\\github.com\\hacdias\\caddy-filemanager\\assets\\templates\\template.tmpl" path := "D:\\Code\\Go\\src\\github.com\\hacdias\\caddy-filemanager\\assets\\templates\\base.tmpl"
name := "templates/template.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) bytes, err := bindataRead(path, name)
if err != nil { if err != nil {
return nil, err return nil, err
@ -137,7 +156,8 @@ func AssetNames() []string {
var _bindata = map[string]func() (*asset, error){ var _bindata = map[string]func() (*asset, error){
"public/css/styles.css": publicCssStylesCss, "public/css/styles.css": publicCssStylesCss,
"public/js/application.js": publicJsApplicationJs, "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 // 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{ "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") w.Header().Set("Content-Type", "application/json; charset=utf-8")
default: // There's no 'application/json' in the 'Accept' header; browse normally 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 return http.StatusInternalServerError, err
} }
w.Header().Set("Content-Type", "text/html; charset=utf-8") 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) _, err = buf.Write(marsh)
return buf, err 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
}

59
page.go
View File

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

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"text/template"
"github.com/mholt/caddy" "github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver" "github.com/mholt/caddy/caddyhttp/httpserver"
@ -19,20 +18,6 @@ func init() {
// setup configures a new Browse middleware instance. // setup configures a new Browse middleware instance.
func setup(c *caddy.Controller) error { 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) configs, err := fileManagerParse(c)
if err != nil { if err != nil {
return err return err