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,24 +1,16 @@
package git
import (
"errors"
"net/http"
"github.com/hacdias/caddy-hugo/config"
)
var (
conf *config.Config
)
// ServeHTTP is used to serve the content of GIT API.
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
conf = c
switch r.Method {
case "POST":
return POST(w, r)
default:
return http.StatusMethodNotAllowed, errors.New("Invalid method.")
if r.Method != http.MethodPost {
return http.StatusNotImplemented, nil
}
return POST(w, r, c)
}

View File

@@ -7,6 +7,7 @@ import (
"os/exec"
"strings"
"github.com/hacdias/caddy-hugo/config"
s "github.com/hacdias/caddy-hugo/tools/server"
)
@@ -15,7 +16,7 @@ type postError struct {
}
// POST handles the POST method on GIT page which is only an API.
func POST(w http.ResponseWriter, r *http.Request) (int, error) {
func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
// Check if git is installed on the computer
if _, err := exec.LookPath("git"); err != nil {
return s.RespondJSON(w, &postError{"Git is not installed on your computer."}, 400, nil)
@@ -46,7 +47,7 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) {
}
cmd := exec.Command("git", args...)
cmd.Dir = conf.Path
cmd.Dir = c.Path
output, err := cmd.CombinedOutput()
if err != nil {