organize better in sub packages

pull/144/head
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;
font-weight: bold;
}
p {
line-height: 1.3;
}
</style>
</head>

View File

@ -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)
}

View File

@ -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 {

View File

@ -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,

View File

@ -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,

View File

@ -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
}

View File

@ -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:

View File

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

View File

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

View File

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