organize better in sub packages

This commit is contained in:
Henrique Dias 2016-10-18 21:30:10 +01:00
parent 06c1a412a6
commit f2fbe92591
10 changed files with 47 additions and 35 deletions

View File

@ -32,6 +32,9 @@ const template = `<!DOCTYPE html>
color: #eee; color: #eee;
font-weight: bold; font-weight: bold;
} }
p {
line-height: 1.3;
}
</style> </style>
</head> </head>

View File

@ -17,6 +17,7 @@ import (
"github.com/hacdias/caddy-filemanager/assets" "github.com/hacdias/caddy-filemanager/assets"
"github.com/hacdias/caddy-filemanager/config" "github.com/hacdias/caddy-filemanager/config"
"github.com/hacdias/caddy-filemanager/errors" "github.com/hacdias/caddy-filemanager/errors"
"github.com/hacdias/caddy-filemanager/page"
"github.com/mholt/caddy/caddyhttp/httpserver" "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 return http.StatusInternalServerError, err
} }
p := &page{pageInfo: &pageInfo{Data: string(output)}} p := &page.Page{Info: &page.Info{Data: string(output)}}
return p.PrintAsJSON(w) return p.PrintAsJSON(w)
} }

View File

@ -13,7 +13,8 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/hacdias/caddy-filemanager/utils/variables" "github.com/hacdias/caddy-filemanager/utils"
"github.com/spf13/cast" "github.com/spf13/cast"
) )
@ -125,9 +126,9 @@ func rawToPretty(config interface{}, parent *Block) *Content {
} }
for name, element := range cnf { for name, element := range cnf {
if variables.IsMap(element) { if utils.IsMap(element) {
objects = append(objects, handleObjects(element, parent, name)) objects = append(objects, handleObjects(element, parent, name))
} else if variables.IsSlice(element) { } else if utils.IsSlice(element) {
arrays = append(arrays, handleArrays(element, parent, name)) arrays = append(arrays, handleArrays(element, parent, name))
} else { } else {
if name == "title" && parent.Name == mainName { if name == "title" && parent.Name == mainName {

View File

@ -10,6 +10,7 @@ import (
humanize "github.com/dustin/go-humanize" humanize "github.com/dustin/go-humanize"
"github.com/hacdias/caddy-filemanager/config" "github.com/hacdias/caddy-filemanager/config"
"github.com/hacdias/caddy-filemanager/page"
) )
// FileInfo contains the information about a particular file or directory // 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 return 0, nil
} }
p := &page{ p := &page.Page{
pageInfo: &pageInfo{ Info: &page.Info{
Name: i.Name(), Name: i.Name(),
Path: i.VirtualPath, Path: i.VirtualPath,
IsDir: false, IsDir: false,

View File

@ -12,7 +12,9 @@ import (
"strings" "strings"
"github.com/hacdias/caddy-filemanager/config" "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" "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) file, err := u.FileSystem.OpenFile(i.VirtualPath, os.O_RDONLY, 0)
if err != nil { if err != nil {
return errors.ToHTTPCode(err), err return utils.ErrorToHTTPCode(err, true), err
} }
defer file.Close() defer file.Close()
@ -203,8 +205,8 @@ func (i *FileInfo) serveListing(w http.ResponseWriter, r *http.Request, c *confi
return http.StatusOK, nil return http.StatusOK, nil
} }
page := &page{ page := &page.Page{
pageInfo: &pageInfo{ Info: &page.Info{
Name: listing.Name, Name: listing.Name,
Path: i.VirtualPath, Path: i.VirtualPath,
IsDir: true, IsDir: true,

View File

@ -1,4 +1,5 @@
package filemanager // Package page is used to render the HTML to the end user
package page
import ( import (
"bytes" "bytes"
@ -10,17 +11,17 @@ import (
"github.com/hacdias/caddy-filemanager/assets" "github.com/hacdias/caddy-filemanager/assets"
"github.com/hacdias/caddy-filemanager/config" "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 // Page contains the informations and functions needed to show the Page
type page struct { type Page struct {
*pageInfo *Info
Minimal bool Minimal bool
} }
// pageInfo contains the information of a page // Info contains the information of a Page
type pageInfo struct { type Info struct {
Name string Name string
Path string Path string
IsDir bool IsDir bool
@ -31,7 +32,7 @@ type pageInfo struct {
// BreadcrumbMap returns p.Path where every element is a map // BreadcrumbMap returns p.Path where every element is a map
// of URLs and path segment names. // of URLs and path segment names.
func (i pageInfo) BreadcrumbMap() map[string]string { func (i Info) BreadcrumbMap() map[string]string {
result := map[string]string{} result := map[string]string{}
if len(i.Path) == 0 { if len(i.Path) == 0 {
@ -62,7 +63,7 @@ func (i pageInfo) BreadcrumbMap() map[string]string {
} }
// PreviousLink returns the path of the previous folder // 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.TrimSuffix(i.Path, "/")
path = strings.TrimPrefix(path, "/") path = strings.TrimPrefix(path, "/")
path = i.Config.AbsoluteURL + "/" + 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 // 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 // Create the functions map, then the template, check for erros and
// execute the template if there aren't errors // execute the template if there aren't errors
functions := template.FuncMap{ functions := template.FuncMap{
"Defined": variables.Defined, "Defined": utils.Defined,
"CSS": func(s string) template.CSS { "CSS": func(s string) template.CSS {
return template.CSS(s) 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 each template, add it to the the tpl variable
for i, t := range templates { for i, t := range templates {
// Get the template from the assets // 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 // Check if there is some error. If so, the template doesn't exist
if err != nil { 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 // If it's the first iteration, creates a new template and add the
// functions map // functions map
if i == 0 { if i == 0 {
tpl, err = template.New(t).Funcs(functions).Parse(string(page)) tpl, err = template.New(t).Funcs(functions).Parse(string(Page))
} else { } else {
tpl, err = tpl.Parse(string(page)) tpl, err = tpl.Parse(string(Page))
} }
if err != nil { if err != nil {
@ -124,7 +125,7 @@ func (p page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, erro
} }
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err := tpl.Execute(buf, p.pageInfo) err := tpl.Execute(buf, p.Info)
if err != nil { if err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
@ -135,9 +136,9 @@ func (p page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, erro
return http.StatusOK, nil return http.StatusOK, nil
} }
// PrintAsJSON prints the current page infromation in JSON // PrintAsJSON prints the current Page infromation in JSON
func (p page) PrintAsJSON(w http.ResponseWriter) (int, error) { func (p Page) PrintAsJSON(w http.ResponseWriter) (int, error) {
marsh, err := json.Marshal(p.pageInfo.Data) marsh, err := json.Marshal(p.Info.Data)
if err != nil { if err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
} }

View File

@ -1,17 +1,20 @@
package errors package utils
import ( import (
"net/http" "net/http"
"os" "os"
) )
// ToHTTPCode gets the respective HTTP code for an error func ErrorToHTTPCode(err error, gone bool) int {
func ToHTTPCode(err error) int {
switch { switch {
case os.IsPermission(err): case os.IsPermission(err):
return http.StatusForbidden return http.StatusForbidden
case os.IsNotExist(err): case os.IsNotExist(err):
return http.StatusNotFound if !gone {
return http.StatusNotFound
}
return http.StatusGone
case os.IsExist(err): case os.IsExist(err):
return http.StatusGone return http.StatusGone
default: default:

View File

@ -1,4 +1,4 @@
package variables package utils
import "reflect" import "reflect"

View File

@ -1,4 +1,4 @@
package variables package utils
import ( import (
"errors" "errors"

View File

@ -1,4 +1,4 @@
package variables package utils
import "testing" import "testing"