simplify some stuff

pull/68/head
Henrique Dias 2016-03-12 09:52:04 +00:00
parent 3301b60485
commit d41e4cc047
9 changed files with 33 additions and 54 deletions

View File

@ -1,5 +0,0 @@
{{ define "content" }}
<main>
<h1>404 Not Found</h1>
</main>
{{ end }}

22
hugo.go
View File

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

View File

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

View File

@ -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."

View File

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

View File

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

View File

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

View File

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

View File

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