remove dangerous global variables
This commit is contained in:
@@ -1,30 +1,23 @@
|
||||
package editor
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/hacdias/caddy-hugo/config"
|
||||
)
|
||||
|
||||
var (
|
||||
filename string
|
||||
conf *config.Config
|
||||
)
|
||||
|
||||
// ServeHTTP serves the editor page
|
||||
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
||||
conf = c
|
||||
filename = strings.Replace(r.URL.Path, c.Admin+"/edit/", "", 1)
|
||||
filename := strings.Replace(r.URL.Path, c.Admin+"/edit/", "", 1)
|
||||
filename = c.Path + filename
|
||||
|
||||
switch r.Method {
|
||||
case "POST":
|
||||
return POST(w, r)
|
||||
case "GET":
|
||||
return GET(w, r)
|
||||
case http.MethodPost:
|
||||
return POST(w, r, c, filename)
|
||||
case http.MethodGet:
|
||||
return GET(w, r, c, filename)
|
||||
default:
|
||||
return http.StatusMethodNotAllowed, errors.New("Invalid method.")
|
||||
return http.StatusNotImplemented, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ type editor struct {
|
||||
}
|
||||
|
||||
// GET handles the GET method on editor page
|
||||
func GET(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
func GET(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
|
||||
// Check if the file format is supported. If not, send a "Not Acceptable"
|
||||
// header and an error
|
||||
if !templates.CanBeEdited(filename) {
|
||||
@@ -55,8 +55,8 @@ func GET(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
// Create a new editor variable and set the extension
|
||||
page := new(editor)
|
||||
page.Mode = strings.TrimPrefix(filepath.Ext(filename), ".")
|
||||
page.Name = strings.Replace(filename, conf.Path, "", 1)
|
||||
page.Config = conf
|
||||
page.Name = strings.Replace(filename, c.Path, "", 1)
|
||||
page.Config = c
|
||||
page.IsPost = false
|
||||
|
||||
// Sanitize the extension
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hacdias/caddy-hugo/config"
|
||||
"github.com/hacdias/caddy-hugo/tools/hugo"
|
||||
"github.com/hacdias/caddy-hugo/tools/server"
|
||||
"github.com/robfig/cron"
|
||||
@@ -30,7 +31,7 @@ type response struct {
|
||||
}
|
||||
|
||||
// POST handles the POST method on editor page
|
||||
func POST(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
func POST(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
|
||||
var data info
|
||||
|
||||
// Get the JSON information sent using a buffer
|
||||
@@ -50,7 +51,7 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
|
||||
switch data.ContentType {
|
||||
case "frontmatter-only":
|
||||
file, code, err = parseFrontMatterOnlyFile(data)
|
||||
file, code, err = parseFrontMatterOnlyFile(data, filename)
|
||||
if err != nil {
|
||||
return server.RespondJSON(w, &response{err.Error()}, code, err)
|
||||
}
|
||||
@@ -61,7 +62,7 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
|
||||
file = []byte(mainContent)
|
||||
case "complete":
|
||||
file, code, err = parseCompleteFile(data)
|
||||
file, code, err = parseCompleteFile(data, filename, c)
|
||||
if err != nil {
|
||||
return server.RespondJSON(w, &response{err.Error()}, code, err)
|
||||
}
|
||||
@@ -77,13 +78,13 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
}
|
||||
|
||||
if data.Regenerate {
|
||||
go hugo.Run(conf, false)
|
||||
go hugo.Run(c, false)
|
||||
}
|
||||
|
||||
return server.RespondJSON(w, nil, http.StatusOK, nil)
|
||||
}
|
||||
|
||||
func parseFrontMatterOnlyFile(data info) ([]byte, int, error) {
|
||||
func parseFrontMatterOnlyFile(data info, filename string) ([]byte, int, error) {
|
||||
frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
|
||||
var mark rune
|
||||
|
||||
@@ -121,7 +122,7 @@ func parseFrontMatterOnlyFile(data info) ([]byte, int, error) {
|
||||
return f, http.StatusOK, nil
|
||||
}
|
||||
|
||||
func parseCompleteFile(data info) ([]byte, int, error) {
|
||||
func parseCompleteFile(data info, filename string, c *config.Config) ([]byte, int, error) {
|
||||
// The main content of the file
|
||||
mainContent := data.Content["content"].(string)
|
||||
mainContent = "\n\n" + strings.TrimSpace(mainContent) + "\n"
|
||||
@@ -164,7 +165,7 @@ func parseCompleteFile(data info) ([]byte, int, error) {
|
||||
return
|
||||
}
|
||||
|
||||
go hugo.Run(conf, false)
|
||||
go hugo.Run(c, false)
|
||||
})
|
||||
scheduler.Start()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user