parent
4ea19cab92
commit
00e9e849ea
|
@ -8,20 +8,16 @@ import (
|
||||||
|
|
||||||
// Config is the add-on configuration set on Caddyfile
|
// Config is the add-on configuration set on Caddyfile
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Public string
|
Public string // Public content path
|
||||||
Content string
|
Path string // Hugo files path
|
||||||
Path string
|
Styles string // Admin styles path
|
||||||
Styles string
|
Args []string // Hugo arguments
|
||||||
Command string
|
|
||||||
Hugo bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseCMS parses the configuration file
|
// ParseHugo parses the configuration file
|
||||||
func ParseCMS(c *setup.Controller) (*Config, error) {
|
func ParseHugo(c *setup.Controller) (*Config, error) {
|
||||||
conf := &Config{
|
conf := &Config{
|
||||||
Public: strings.Replace(c.Root, "./", "", -1),
|
Public: strings.Replace(c.Root, "./", "", -1),
|
||||||
Content: "content",
|
|
||||||
Hugo: true,
|
|
||||||
Path: "./",
|
Path: "./",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,20 +42,18 @@ func ParseCMS(c *setup.Controller) (*Config, error) {
|
||||||
conf.Styles = strings.TrimPrefix(conf.Styles, "/")
|
conf.Styles = strings.TrimPrefix(conf.Styles, "/")
|
||||||
// Add a beginning slash to make a
|
// Add a beginning slash to make a
|
||||||
conf.Styles = "/" + conf.Styles
|
conf.Styles = "/" + conf.Styles
|
||||||
case "content":
|
case "args":
|
||||||
if !c.NextArg() {
|
if !c.NextArg() {
|
||||||
return nil, c.ArgErr()
|
return nil, c.ArgErr()
|
||||||
}
|
}
|
||||||
conf.Content = c.Val()
|
|
||||||
case "command":
|
|
||||||
if !c.NextArg() {
|
|
||||||
return nil, c.ArgErr()
|
|
||||||
}
|
|
||||||
conf.Command = c.Val()
|
|
||||||
|
|
||||||
if conf.Command != "" && !strings.HasPrefix(conf.Command, "-") {
|
// Get the arguments and split the array
|
||||||
conf.Hugo = false
|
args := strings.Split(c.Val(), " ")
|
||||||
|
for index, element := range args {
|
||||||
|
args[index] = strings.Replace(element, "\"", "", -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conf.Args = args
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
//go:generate go install github.com/jteeuwen/go-bindata/go-bindata
|
//go:generate go install github.com/jteeuwen/go-bindata/go-bindata
|
||||||
//go:generate go-bindata -pkg assets -o assets/assets.go templates/ assets/css/ assets/js/ assets/fonts/
|
//go:generate go-bindata -pkg assets -o assets/assets.go templates/ assets/css/ assets/js/ assets/fonts/
|
||||||
|
|
||||||
package cms
|
package hugo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"mime"
|
"mime"
|
||||||
|
@ -22,21 +22,21 @@ import (
|
||||||
|
|
||||||
// Setup configures the middleware
|
// Setup configures the middleware
|
||||||
func Setup(c *setup.Controller) (middleware.Middleware, error) {
|
func Setup(c *setup.Controller) (middleware.Middleware, error) {
|
||||||
config, _ := config.ParseCMS(c)
|
config, _ := config.ParseHugo(c)
|
||||||
utils.Run(config)
|
utils.Run(config)
|
||||||
|
|
||||||
return func(next middleware.Handler) middleware.Handler {
|
return func(next middleware.Handler) middleware.Handler {
|
||||||
return &CaddyCMS{Next: next, Config: config}
|
return &CaddyHugo{Next: next, Config: config}
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CaddyCMS main type
|
// CaddyHugo main type
|
||||||
type CaddyCMS struct {
|
type CaddyHugo struct {
|
||||||
Next middleware.Handler
|
Next middleware.Handler
|
||||||
Config *config.Config
|
Config *config.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h CaddyCMS) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
// Only handle /admin path
|
// Only handle /admin path
|
||||||
if middleware.Path(r.URL.Path).Matches("/admin") {
|
if middleware.Path(r.URL.Path).Matches("/admin") {
|
||||||
var err error
|
var err error
|
||||||
|
@ -60,9 +60,9 @@ func (h CaddyCMS) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the current page is only "/admin/", redirect to "/admin/browse/contents"
|
// If the current page is only "/admin/", redirect to "/admin/browse/content/"
|
||||||
if r.URL.Path == "/admin/" {
|
if r.URL.Path == "/admin/" {
|
||||||
http.Redirect(w, r, "/admin/browse/"+h.Config.Content+"/", http.StatusTemporaryRedirect)
|
http.Redirect(w, r, "/admin/browse/content/", http.StatusTemporaryRedirect)
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,12 +2,10 @@ package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
@ -174,23 +172,7 @@ func Run(c *config.Config) {
|
||||||
log.Print("Can't get working directory.")
|
log.Print("Can't get working directory.")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !c.Hugo {
|
commands.HugoCmd.ParseFlags(c.Args)
|
||||||
out, err := exec.Command(c.Command).Output()
|
|
||||||
fmt.Print(string(out))
|
|
||||||
if err != nil {
|
|
||||||
log.Panic("Can't execute the commands defined on Caddyfile.")
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
args := strings.Split(c.Command, " ")
|
|
||||||
|
|
||||||
for index, element := range args {
|
|
||||||
args[index] = strings.Replace(element, "\"", "", -1)
|
|
||||||
}
|
|
||||||
|
|
||||||
commands.HugoCmd.ParseFlags(args)
|
|
||||||
commands.HugoCmd.Run(commands.HugoCmd, make([]string, 0))
|
commands.HugoCmd.Run(commands.HugoCmd, make([]string, 0))
|
||||||
|
|
||||||
err = os.Chdir(cwd)
|
err = os.Chdir(cwd)
|
||||||
|
|
Loading…
Reference in New Issue