remove dangerous global variables

This commit is contained in:
Henrique Dias
2016-06-16 17:01:41 +01:00
parent 48878cc3b5
commit af2785e510
11 changed files with 66 additions and 82 deletions

View File

@@ -1,15 +1,12 @@
package browse
import (
"errors"
"net/http"
"strings"
"github.com/hacdias/caddy-hugo/config"
)
var conf *config.Config
type response struct {
Message string `json:"message"`
Location string `json:"location"`
@@ -19,20 +16,18 @@ type response struct {
// from Caddy. It handles the requests for DELETE, POST, GET and PUT related to
// /browse interface.
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
conf = c
// Removes the page main path from the URL
r.URL.Path = strings.Replace(r.URL.Path, c.Admin+"/browse", "", 1)
switch r.Method {
case "DELETE":
return DELETE(w, r)
return DELETE(w, r, c)
case "POST":
return POST(w, r, c)
case "GET":
return GET(w, r)
return GET(w, r, c)
case "PUT":
return PUT(w, r)
return PUT(w, r, c)
default:
return http.StatusMethodNotAllowed, errors.New("Invalid method.")
return http.StatusNotImplemented, nil
}
}

View File

@@ -5,16 +5,17 @@ import (
"os"
"strings"
"github.com/hacdias/caddy-hugo/config"
s "github.com/hacdias/caddy-hugo/tools/server"
)
// DELETE handles the delete requests on browse pages
func DELETE(w http.ResponseWriter, r *http.Request) (int, error) {
func DELETE(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
// Remove both beginning and trailing slashes
path := r.URL.Path
path = strings.TrimPrefix(path, "/")
path = strings.TrimSuffix(path, "/")
path = conf.Path + path
path = c.Path + path
message := "File deleted."

View File

@@ -4,6 +4,7 @@ import (
"net/http"
"text/template"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/tools/templates"
"github.com/hacdias/caddy-hugo/tools/variables"
"github.com/mholt/caddy/caddyhttp/browse"
@@ -12,7 +13,7 @@ import (
// GET handles the GET method on browse page and shows the files listing Using
// the Browse Caddy middleware.
func GET(w http.ResponseWriter, r *http.Request) (int, error) {
func GET(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
functions := template.FuncMap{
"CanBeEdited": templates.CanBeEdited,
"Defined": variables.Defined,
@@ -32,8 +33,8 @@ func GET(w http.ResponseWriter, r *http.Request) (int, error) {
Configs: []browse.Config{
{
PathScope: "/",
Root: http.Dir(conf.Path),
Variables: conf,
Root: http.Dir(c.Path),
Variables: c,
Template: tpl,
},
},

View File

@@ -23,7 +23,7 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
// If it's the upload of a file
if r.Header.Get("X-Upload") == "true" {
return upload(w, r)
return upload(w, r, c)
}
// Get the JSON information sent using a buffer
@@ -49,12 +49,12 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
filename = strings.TrimPrefix(filename, "/")
filename = strings.TrimSuffix(filename, "/")
url := c.Admin + "/edit/" + r.URL.Path + filename
filename = conf.Path + r.URL.Path + filename
filename = c.Path + r.URL.Path + filename
if strings.HasPrefix(filename, conf.Path+"content/") &&
if strings.HasPrefix(filename, c.Path+"content/") &&
(strings.HasSuffix(filename, ".md") || strings.HasSuffix(filename, ".markdown")) {
filename = strings.Replace(filename, conf.Path+"content/", "", 1)
filename = strings.Replace(filename, c.Path+"content/", "", 1)
args := []string{"new", filename}
archetype := info["archetype"].(string)
@@ -62,7 +62,7 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
args = append(args, "--kind", archetype)
}
if err := commands.Run(conf.Hugo, args, conf.Path); err != nil {
if err := commands.Run(c.Hugo, args, c.Path); err != nil {
return s.RespondJSON(w, &response{"Something went wrong.", ""}, http.StatusInternalServerError, err)
}
} else {
@@ -86,7 +86,7 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
return s.RespondJSON(w, &response{"File created!", url}, http.StatusOK, nil)
}
func upload(w http.ResponseWriter, r *http.Request) (int, error) {
func upload(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
// Parse the multipart form in the request
err := r.ParseMultipartForm(100000)
if err != nil {
@@ -105,7 +105,7 @@ func upload(w http.ResponseWriter, r *http.Request) (int, error) {
// Create the file
var outfile *os.File
if outfile, err = os.Create(conf.Path + r.URL.Path + hdr.Filename); nil != err {
if outfile, err = os.Create(c.Path + r.URL.Path + hdr.Filename); nil != err {
return s.RespondJSON(w, &response{"Something went wrong.", ""}, http.StatusInternalServerError, err)
}

View File

@@ -7,17 +7,18 @@ import (
"os"
"strings"
"github.com/hacdias/caddy-hugo/config"
"github.com/hacdias/caddy-hugo/tools/server"
)
// PUT handles the HTTP PUT request for all /{admin}/browse related requests.
// Renames a file and/or a folder.
func PUT(w http.ResponseWriter, r *http.Request) (int, error) {
func PUT(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
// Remove both beginning and trailing slashes
old := r.URL.Path
old = strings.TrimPrefix(old, "/")
old = strings.TrimSuffix(old, "/")
old = conf.Path + old
old = c.Path + old
// Get the JSON information sent using a buffer
buffer := new(bytes.Buffer)
@@ -37,7 +38,7 @@ func PUT(w http.ResponseWriter, r *http.Request) (int, error) {
new := info["filename"].(string)
new = strings.TrimPrefix(new, "/")
new = strings.TrimSuffix(new, "/")
new = conf.Path + new
new = c.Path + new
// Renames the file/folder
if err := os.Rename(old, new); err != nil {