From d41e4cc047ee914cbd3a218ec33ea69b1ef9fb43 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sat, 12 Mar 2016 09:52:04 +0000 Subject: [PATCH] simplify some stuff --- assets/templates/404.tmpl | 5 ----- hugo.go | 22 +--------------------- routes/browse/browse.go | 11 +++++++---- routes/browse/delete.go | 5 ++--- routes/browse/get.go | 7 +++---- routes/browse/post.go | 17 ++++++++--------- routes/browse/put.go | 7 +++---- routes/git/git.go | 8 +++++++- routes/git/post.go | 5 ++--- 9 files changed, 33 insertions(+), 54 deletions(-) delete mode 100644 assets/templates/404.tmpl diff --git a/assets/templates/404.tmpl b/assets/templates/404.tmpl deleted file mode 100644 index 4de316a2..00000000 --- a/assets/templates/404.tmpl +++ /dev/null @@ -1,5 +0,0 @@ -{{ define "content" }} -
-

404 Not Found

-
-{{ end }} diff --git a/hugo.go b/hugo.go index 7573fe9c..a76afbf5 100644 --- a/hugo.go +++ b/hugo.go @@ -1,6 +1,6 @@ //go:generate go get github.com/jteeuwen/go-bindata //go:generate go install github.com/jteeuwen/go-bindata/go-bindata -//go:generate go-bindata -debug -prefix assets/ -pkg assets -o routes/assets/assets.go assets/templates/ assets/public/... +//go:generate go-bindata -prefix assets/ -pkg assets -o routes/assets/assets.go assets/templates/ assets/public/... // Package hugo makes the bridge between the static website generator Hugo // and the webserver Caddy, also providing an administrative user interface. @@ -143,26 +143,6 @@ func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error code, err = git.ServeHTTP(w, r, h.Config) } - /* if err != nil { - log.Panic(err) - } */ - - /* - - // Create the functions map, then the template, check for erros and - // execute the template if there aren't errors - functions := template.FuncMap{ - "Defined": utils.Defined, - } - - switch code { - case 404: - tpl, _ := utils.GetTemplate(r, functions, "404") - tpl.Execute(w, nil) - code = 200 - err = nil - } */ - return code, err } diff --git a/routes/browse/browse.go b/routes/browse/browse.go index 9ea69bb9..95c50bcc 100644 --- a/routes/browse/browse.go +++ b/routes/browse/browse.go @@ -8,22 +8,25 @@ import ( "github.com/hacdias/caddy-hugo/config" ) +var conf *config.Config + // ServeHTTP is used to serve the content of Browse page using Browse middleware // 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, "/admin/browse", "", 1) switch r.Method { case "DELETE": - return DELETE(w, r, c) + return DELETE(w, r) case "POST": - return POST(w, r, c) + return POST(w, r) case "GET": - return GET(w, r, c) + return GET(w, r) case "PUT": - return PUT(w, r, c) + return PUT(w, r) default: return http.StatusMethodNotAllowed, errors.New("Invalid method.") } diff --git a/routes/browse/delete.go b/routes/browse/delete.go index 33627296..74a0ba28 100644 --- a/routes/browse/delete.go +++ b/routes/browse/delete.go @@ -5,17 +5,16 @@ import ( "os" "strings" - "github.com/hacdias/caddy-hugo/config" "github.com/hacdias/caddy-hugo/tools/server" ) // DELETE handles the delete requests on browse pages -func DELETE(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) { +func DELETE(w http.ResponseWriter, r *http.Request) (int, error) { // Remove both beginning and trailing slashes path := r.URL.Path path = strings.TrimPrefix(path, "/") path = strings.TrimSuffix(path, "/") - path = c.Path + path + path = conf.Path + path message := "File deleted." diff --git a/routes/browse/get.go b/routes/browse/get.go index ef22cbcf..6fc159a8 100644 --- a/routes/browse/get.go +++ b/routes/browse/get.go @@ -4,7 +4,6 @@ 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/middleware" @@ -13,7 +12,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, c *config.Config) (int, error) { +func GET(w http.ResponseWriter, r *http.Request) (int, error) { functions := template.FuncMap{ "CanBeEdited": templates.CanBeEdited, "Defined": variables.Defined, @@ -30,11 +29,11 @@ func GET(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { return 404, nil }), - Root: c.Path, + Root: conf.Path, Configs: []browse.Config{ { PathScope: "/", - Variables: c, + Variables: conf, Template: tpl, }, }, diff --git a/routes/browse/post.go b/routes/browse/post.go index 3a3440a6..fe42d195 100644 --- a/routes/browse/post.go +++ b/routes/browse/post.go @@ -10,20 +10,19 @@ import ( "path/filepath" "strings" - "github.com/hacdias/caddy-hugo/config" "github.com/hacdias/caddy-hugo/tools/commands" "github.com/hacdias/caddy-hugo/tools/server" ) // POST handles the POST method on browse page. It's used to create new files, // folders and upload content. -func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) { +func POST(w http.ResponseWriter, r *http.Request) (int, error) { // Remove prefix slash r.URL.Path = strings.TrimPrefix(r.URL.Path, "/") // If it's the upload of a file if r.Header.Get("X-Upload") == "true" { - return upload(w, r, c) + return upload(w, r) } // Get the JSON information sent using a buffer @@ -53,12 +52,12 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) filename = strings.TrimPrefix(filename, "/") filename = strings.TrimSuffix(filename, "/") url := "/admin/edit/" + r.URL.Path + filename - filename = c.Path + r.URL.Path + filename + filename = conf.Path + r.URL.Path + filename - if strings.HasPrefix(filename, c.Path+"content/") && + if strings.HasPrefix(filename, conf.Path+"content/") && (strings.HasSuffix(filename, ".md") || strings.HasSuffix(filename, ".markdown")) { - filename = strings.Replace(filename, c.Path+"content/", "", 1) + filename = strings.Replace(filename, conf.Path+"content/", "", 1) args := []string{"new", filename} archetype := info["archetype"].(string) @@ -66,7 +65,7 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) args = append(args, "--kind", archetype) } - if err := commands.Run(c.Hugo, args, c.Path); err != nil { + if err := commands.Run(conf.Hugo, args, conf.Path); err != nil { return server.RespondJSON(w, map[string]string{ "message": "Something went wrong.", }, 500, err) @@ -97,7 +96,7 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) }, 200, nil) } -func upload(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) { +func upload(w http.ResponseWriter, r *http.Request) (int, error) { // Parse the multipart form in the request err := r.ParseMultipartForm(100000) if err != nil { @@ -120,7 +119,7 @@ func upload(w http.ResponseWriter, r *http.Request, c *config.Config) (int, erro // Create the file var outfile *os.File - if outfile, err = os.Create(c.Path + r.URL.Path + hdr.Filename); nil != err { + if outfile, err = os.Create(conf.Path + r.URL.Path + hdr.Filename); nil != err { return server.RespondJSON(w, map[string]string{ "message": "Something went wrong.", }, 500, err) diff --git a/routes/browse/put.go b/routes/browse/put.go index 22be9d0d..33d3b2af 100644 --- a/routes/browse/put.go +++ b/routes/browse/put.go @@ -7,18 +7,17 @@ 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, c *config.Config) (int, error) { +func PUT(w http.ResponseWriter, r *http.Request) (int, error) { // Remove both beginning and trailing slashes old := r.URL.Path old = strings.TrimPrefix(old, "/") old = strings.TrimSuffix(old, "/") - old = c.Path + old + old = conf.Path + old // Get the JSON information sent using a buffer buffer := new(bytes.Buffer) @@ -40,7 +39,7 @@ func PUT(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) new := info["filename"].(string) new = strings.TrimPrefix(new, "/") new = strings.TrimSuffix(new, "/") - new = c.Path + new + new = conf.Path + new // Renames the file/folder if err := os.Rename(old, new); err != nil { diff --git a/routes/git/git.go b/routes/git/git.go index 0e8f127f..efd64ef2 100644 --- a/routes/git/git.go +++ b/routes/git/git.go @@ -7,11 +7,17 @@ import ( "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, c) + return POST(w, r) default: return http.StatusMethodNotAllowed, errors.New("Invalid method.") } diff --git a/routes/git/post.go b/routes/git/post.go index bccc332d..1037e49d 100644 --- a/routes/git/post.go +++ b/routes/git/post.go @@ -7,12 +7,11 @@ import ( "os/exec" "strings" - "github.com/hacdias/caddy-hugo/config" "github.com/hacdias/caddy-hugo/tools/server" ) // POST handles the POST method on GIT page which is only an API. -func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) { +func POST(w http.ResponseWriter, r *http.Request) (int, error) { // Check if git is installed on the computer if _, err := exec.LookPath("git"); err != nil { return server.RespondJSON(w, map[string]string{ @@ -49,7 +48,7 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) } cmd := exec.Command("git", args...) - cmd.Dir = c.Path + cmd.Dir = conf.Path output, err := cmd.CombinedOutput() if err != nil {