mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
parent
4ea19cab92
commit
00e9e849ea
@ -8,21 +8,17 @@ import (
|
||||
|
||||
// Config is the add-on configuration set on Caddyfile
|
||||
type Config struct {
|
||||
Public string
|
||||
Content string
|
||||
Path string
|
||||
Styles string
|
||||
Command string
|
||||
Hugo bool
|
||||
Public string // Public content path
|
||||
Path string // Hugo files path
|
||||
Styles string // Admin styles path
|
||||
Args []string // Hugo arguments
|
||||
}
|
||||
|
||||
// ParseCMS parses the configuration file
|
||||
func ParseCMS(c *setup.Controller) (*Config, error) {
|
||||
// ParseHugo parses the configuration file
|
||||
func ParseHugo(c *setup.Controller) (*Config, error) {
|
||||
conf := &Config{
|
||||
Public: strings.Replace(c.Root, "./", "", -1),
|
||||
Content: "content",
|
||||
Hugo: true,
|
||||
Path: "./",
|
||||
Public: strings.Replace(c.Root, "./", "", -1),
|
||||
Path: "./",
|
||||
}
|
||||
|
||||
for c.Next() {
|
||||
@ -46,20 +42,18 @@ func ParseCMS(c *setup.Controller) (*Config, error) {
|
||||
conf.Styles = strings.TrimPrefix(conf.Styles, "/")
|
||||
// Add a beginning slash to make a
|
||||
conf.Styles = "/" + conf.Styles
|
||||
case "content":
|
||||
case "args":
|
||||
if !c.NextArg() {
|
||||
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, "-") {
|
||||
conf.Hugo = false
|
||||
// Get the arguments and split the array
|
||||
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-bindata -pkg assets -o assets/assets.go templates/ assets/css/ assets/js/ assets/fonts/
|
||||
|
||||
package cms
|
||||
package hugo
|
||||
|
||||
import (
|
||||
"mime"
|
||||
@ -22,21 +22,21 @@ import (
|
||||
|
||||
// Setup configures the middleware
|
||||
func Setup(c *setup.Controller) (middleware.Middleware, error) {
|
||||
config, _ := config.ParseCMS(c)
|
||||
config, _ := config.ParseHugo(c)
|
||||
utils.Run(config)
|
||||
|
||||
return func(next middleware.Handler) middleware.Handler {
|
||||
return &CaddyCMS{Next: next, Config: config}
|
||||
return &CaddyHugo{Next: next, Config: config}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CaddyCMS main type
|
||||
type CaddyCMS struct {
|
||||
// CaddyHugo main type
|
||||
type CaddyHugo struct {
|
||||
Next middleware.Handler
|
||||
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
|
||||
if middleware.Path(r.URL.Path).Matches("/admin") {
|
||||
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/" {
|
||||
http.Redirect(w, r, "/admin/browse/"+h.Config.Content+"/", http.StatusTemporaryRedirect)
|
||||
http.Redirect(w, r, "/admin/browse/content/", http.StatusTemporaryRedirect)
|
||||
return 0, nil
|
||||
}
|
||||
|
@ -2,12 +2,10 @@ package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"reflect"
|
||||
"strings"
|
||||
"text/template"
|
||||
@ -174,23 +172,7 @@ func Run(c *config.Config) {
|
||||
log.Print("Can't get working directory.")
|
||||
}
|
||||
|
||||
if !c.Hugo {
|
||||
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.ParseFlags(c.Args)
|
||||
commands.HugoCmd.Run(commands.HugoCmd, make([]string, 0))
|
||||
|
||||
err = os.Chdir(cwd)
|
||||
|
Loading…
Reference in New Issue
Block a user