From f2fbe92591df25ac96fcf4a8ab80f4978f0d4a56 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 18 Oct 2016 21:30:10 +0100 Subject: [PATCH] organize better in sub packages --- errors/errors.go | 3 ++ filemanager.go | 3 +- frontmatter/frontmatter.go | 7 +++-- info.go | 5 ++-- listing.go | 10 ++++--- page.go => page/page.go | 37 +++++++++++++------------ utils/{errors/http.go => errors.go} | 11 +++++--- utils/{variables => }/types.go | 2 +- utils/{variables => }/variables.go | 2 +- utils/{variables => }/variables_test.go | 2 +- 10 files changed, 47 insertions(+), 35 deletions(-) rename page.go => page/page.go (76%) rename utils/{errors/http.go => errors.go} (62%) rename utils/{variables => }/types.go (94%) rename utils/{variables => }/variables.go (98%) rename utils/{variables => }/variables_test.go (97%) diff --git a/errors/errors.go b/errors/errors.go index d404e54e..b859a345 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -32,6 +32,9 @@ const template = ` color: #eee; font-weight: bold; } + p { + line-height: 1.3; + } diff --git a/filemanager.go b/filemanager.go index bc8f1e18..8532159f 100644 --- a/filemanager.go +++ b/filemanager.go @@ -17,6 +17,7 @@ import ( "github.com/hacdias/caddy-filemanager/assets" "github.com/hacdias/caddy-filemanager/config" "github.com/hacdias/caddy-filemanager/errors" + "github.com/hacdias/caddy-filemanager/page" "github.com/mholt/caddy/caddyhttp/httpserver" ) @@ -198,6 +199,6 @@ func command(w http.ResponseWriter, r *http.Request, c *config.Config, u *config return http.StatusInternalServerError, err } - p := &page{pageInfo: &pageInfo{Data: string(output)}} + p := &page.Page{Info: &page.Info{Data: string(output)}} return p.PrintAsJSON(w) } diff --git a/frontmatter/frontmatter.go b/frontmatter/frontmatter.go index 5703addc..18edbc5b 100644 --- a/frontmatter/frontmatter.go +++ b/frontmatter/frontmatter.go @@ -13,7 +13,8 @@ import ( "gopkg.in/yaml.v2" "github.com/BurntSushi/toml" - "github.com/hacdias/caddy-filemanager/utils/variables" + "github.com/hacdias/caddy-filemanager/utils" + "github.com/spf13/cast" ) @@ -125,9 +126,9 @@ func rawToPretty(config interface{}, parent *Block) *Content { } for name, element := range cnf { - if variables.IsMap(element) { + if utils.IsMap(element) { objects = append(objects, handleObjects(element, parent, name)) - } else if variables.IsSlice(element) { + } else if utils.IsSlice(element) { arrays = append(arrays, handleArrays(element, parent, name)) } else { if name == "title" && parent.Name == mainName { diff --git a/info.go b/info.go index ed6da747..542c13b3 100644 --- a/info.go +++ b/info.go @@ -10,6 +10,7 @@ import ( humanize "github.com/dustin/go-humanize" "github.com/hacdias/caddy-filemanager/config" + "github.com/hacdias/caddy-filemanager/page" ) // FileInfo contains the information about a particular file or directory @@ -117,8 +118,8 @@ func (i *FileInfo) serveSingleFile(w http.ResponseWriter, r *http.Request, c *co return 0, nil } - p := &page{ - pageInfo: &pageInfo{ + p := &page.Page{ + Info: &page.Info{ Name: i.Name(), Path: i.VirtualPath, IsDir: false, diff --git a/listing.go b/listing.go index 78dc6b47..e57be46f 100644 --- a/listing.go +++ b/listing.go @@ -12,7 +12,9 @@ import ( "strings" "github.com/hacdias/caddy-filemanager/config" - "github.com/hacdias/caddy-filemanager/utils/errors" + "github.com/hacdias/caddy-filemanager/page" + "github.com/hacdias/caddy-filemanager/utils" + "github.com/mholt/caddy/caddyhttp/httpserver" ) @@ -152,7 +154,7 @@ func (i *FileInfo) serveListing(w http.ResponseWriter, r *http.Request, c *confi file, err := u.FileSystem.OpenFile(i.VirtualPath, os.O_RDONLY, 0) if err != nil { - return errors.ToHTTPCode(err), err + return utils.ErrorToHTTPCode(err, true), err } defer file.Close() @@ -203,8 +205,8 @@ func (i *FileInfo) serveListing(w http.ResponseWriter, r *http.Request, c *confi return http.StatusOK, nil } - page := &page{ - pageInfo: &pageInfo{ + page := &page.Page{ + Info: &page.Info{ Name: listing.Name, Path: i.VirtualPath, IsDir: true, diff --git a/page.go b/page/page.go similarity index 76% rename from page.go rename to page/page.go index f511b7a8..2b2ca46e 100644 --- a/page.go +++ b/page/page.go @@ -1,4 +1,5 @@ -package filemanager +// Package page is used to render the HTML to the end user +package page import ( "bytes" @@ -10,17 +11,17 @@ import ( "github.com/hacdias/caddy-filemanager/assets" "github.com/hacdias/caddy-filemanager/config" - "github.com/hacdias/caddy-filemanager/utils/variables" + "github.com/hacdias/caddy-filemanager/utils" ) -// page contains the informations and functions needed to show the page -type page struct { - *pageInfo +// Page contains the informations and functions needed to show the Page +type Page struct { + *Info Minimal bool } -// pageInfo contains the information of a page -type pageInfo struct { +// Info contains the information of a Page +type Info struct { Name string Path string IsDir bool @@ -31,7 +32,7 @@ type pageInfo struct { // BreadcrumbMap returns p.Path where every element is a map // of URLs and path segment names. -func (i pageInfo) BreadcrumbMap() map[string]string { +func (i Info) BreadcrumbMap() map[string]string { result := map[string]string{} if len(i.Path) == 0 { @@ -62,7 +63,7 @@ func (i pageInfo) BreadcrumbMap() map[string]string { } // PreviousLink returns the path of the previous folder -func (i pageInfo) PreviousLink() string { +func (i Info) PreviousLink() string { path := strings.TrimSuffix(i.Path, "/") path = strings.TrimPrefix(path, "/") path = i.Config.AbsoluteURL + "/" + path @@ -76,11 +77,11 @@ func (i pageInfo) PreviousLink() string { } // PrintAsHTML formats the page in HTML and executes the template -func (p page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, error) { +func (p Page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, error) { // Create the functions map, then the template, check for erros and // execute the template if there aren't errors functions := template.FuncMap{ - "Defined": variables.Defined, + "Defined": utils.Defined, "CSS": func(s string) template.CSS { return template.CSS(s) }, @@ -101,7 +102,7 @@ func (p page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, erro // For each template, add it to the the tpl variable for i, t := range templates { // Get the template from the assets - page, err := assets.Asset("templates/" + t + ".tmpl") + Page, err := assets.Asset("templates/" + t + ".tmpl") // Check if there is some error. If so, the template doesn't exist if err != nil { @@ -112,9 +113,9 @@ func (p page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, erro // If it's the first iteration, creates a new template and add the // functions map if i == 0 { - tpl, err = template.New(t).Funcs(functions).Parse(string(page)) + tpl, err = template.New(t).Funcs(functions).Parse(string(Page)) } else { - tpl, err = tpl.Parse(string(page)) + tpl, err = tpl.Parse(string(Page)) } if err != nil { @@ -124,7 +125,7 @@ func (p page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, erro } buf := &bytes.Buffer{} - err := tpl.Execute(buf, p.pageInfo) + err := tpl.Execute(buf, p.Info) if err != nil { return http.StatusInternalServerError, err @@ -135,9 +136,9 @@ func (p page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, erro return http.StatusOK, nil } -// PrintAsJSON prints the current page infromation in JSON -func (p page) PrintAsJSON(w http.ResponseWriter) (int, error) { - marsh, err := json.Marshal(p.pageInfo.Data) +// PrintAsJSON prints the current Page infromation in JSON +func (p Page) PrintAsJSON(w http.ResponseWriter) (int, error) { + marsh, err := json.Marshal(p.Info.Data) if err != nil { return http.StatusInternalServerError, err } diff --git a/utils/errors/http.go b/utils/errors.go similarity index 62% rename from utils/errors/http.go rename to utils/errors.go index 644f9c55..35841d85 100644 --- a/utils/errors/http.go +++ b/utils/errors.go @@ -1,17 +1,20 @@ -package errors +package utils import ( "net/http" "os" ) -// ToHTTPCode gets the respective HTTP code for an error -func ToHTTPCode(err error) int { +func ErrorToHTTPCode(err error, gone bool) int { switch { case os.IsPermission(err): return http.StatusForbidden case os.IsNotExist(err): - return http.StatusNotFound + if !gone { + return http.StatusNotFound + } + + return http.StatusGone case os.IsExist(err): return http.StatusGone default: diff --git a/utils/variables/types.go b/utils/types.go similarity index 94% rename from utils/variables/types.go rename to utils/types.go index ee43dad3..7e6b408b 100644 --- a/utils/variables/types.go +++ b/utils/types.go @@ -1,4 +1,4 @@ -package variables +package utils import "reflect" diff --git a/utils/variables/variables.go b/utils/variables.go similarity index 98% rename from utils/variables/variables.go rename to utils/variables.go index 7a0168b4..28f8383a 100644 --- a/utils/variables/variables.go +++ b/utils/variables.go @@ -1,4 +1,4 @@ -package variables +package utils import ( "errors" diff --git a/utils/variables/variables_test.go b/utils/variables_test.go similarity index 97% rename from utils/variables/variables_test.go rename to utils/variables_test.go index ec76d459..7122478d 100644 --- a/utils/variables/variables_test.go +++ b/utils/variables_test.go @@ -1,4 +1,4 @@ -package variables +package utils import "testing"