This commit is contained in:
Henrique Dias 2015-09-27 19:30:43 +01:00
commit 750d72b2e7
5 changed files with 58 additions and 26 deletions

View File

@ -8,15 +8,21 @@ Deploy your Hugo website and enjoy of an admin interface with Caddy server.
## Configuration
```
<<<<<<< HEAD
staticmin {
=======
cms {
>>>>>>> 7b68c78b12d9b7aee61788d5e85cd3b0722906de
styles file
flags flags...
hugo true / false # default is true
command command # needed when hugo is false
args args... # hugo or whatever command flags/args
}
```
+ **file** is the relative path to ```public``` folder of the admin UI styles. They will not replace the defaults, they will be added.
+ **flags** are the Hugo flags (those which can be set in the command line) and they must follow one of these syntaxes: ```-f=value``` and ```--flag=value```.
+ **args** are the Hugo flags (those which can be set in the command line) and they must follow one of these syntaxes: ```-f=value``` and ```--flag=value```.
## Build it from source

View File

@ -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 hugo
package cms
import (
"mime"
@ -22,21 +22,21 @@ import (
// Setup configures the middleware
func Setup(c *setup.Controller) (middleware.Middleware, error) {
config, _ := config.ParseHugo(c)
utils.RunHugo(config)
config, _ := config.ParseCMS(c)
utils.Run(config)
return func(next middleware.Handler) middleware.Handler {
return &CaddyHugo{Next: next, Config: config}
return &CaddyCMS{Next: next, Config: config}
}, nil
}
// CaddyHugo main type
type CaddyHugo struct {
// CaddyCMS main type
type CaddyCMS struct {
Next middleware.Handler
Config *config.Config
}
func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
func (h CaddyCMS) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
// Only handle /admin path
if middleware.Path(r.URL.Path).Matches("/admin") {
var err error
@ -106,7 +106,7 @@ func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
// Whenever the header "X-Refenerate" is true, the website should be
// regenerated. Used in edit and settings, for example.
if r.Header.Get("X-Regenerate") == "true" {
utils.RunHugo(h.Config)
utils.Run(h.Config)
}
return code, err

View File

@ -1,6 +1,7 @@
package config
import (
"strconv"
"strings"
"github.com/mholt/caddy/config/setup"
@ -8,15 +9,15 @@ import (
// Config is the add-on configuration set on Caddyfile
type Config struct {
Styles string
Flags []string
Styles string
Hugo bool
Args []string
Command string
}
// ParseHugo parses the configuration file
func ParseHugo(c *setup.Controller) (*Config, error) {
conf := &Config{
Styles: "",
}
// ParseCMS parses the configuration file
func ParseCMS(c *setup.Controller) (*Config, error) {
conf := &Config{Hugo: true}
for c.Next() {
for c.NextBlock() {
@ -30,9 +31,23 @@ func ParseHugo(c *setup.Controller) (*Config, error) {
conf.Styles = strings.TrimPrefix(conf.Styles, "/")
// Add a beginning slash to make a
conf.Styles = "/" + conf.Styles
case "flags":
conf.Flags = c.RemainingArgs()
if len(conf.Flags) == 0 {
case "hugo":
if !c.NextArg() {
return nil, c.ArgErr()
}
var err error
conf.Hugo, err = strconv.ParseBool(c.Val())
if err != nil {
return conf, err
}
case "command":
if !c.NextArg() {
return nil, c.ArgErr()
}
conf.Command = c.Val()
case "args":
conf.Args = c.RemainingArgs()
if len(conf.Args) == 0 {
return conf, c.ArgErr()
}
}
@ -44,7 +59,7 @@ func ParseHugo(c *setup.Controller) (*Config, error) {
}
func (c *Config) parseFlags() {
for index, element := range c.Flags {
c.Flags[index] = strings.Replace(element, "\"", "", -1)
for index, element := range c.Args {
c.Args[index] = strings.Replace(element, "\"", "", -1)
}
}

View File

@ -151,7 +151,7 @@ func parseCompleteFile(r *http.Request, c *config.Config, rawFile map[string]int
return
}
utils.RunHugo(c)
utils.Run(c)
})
scheduler.Start()
}

View File

@ -6,6 +6,7 @@ import (
"log"
"net/http"
"os"
"os/exec"
"reflect"
"strings"
"text/template"
@ -158,9 +159,19 @@ func ParseComponents(r *http.Request) []string {
return components
}
// RunHugo is used to run hugo
func RunHugo(c *config.Config) {
commands.HugoCmd.ParseFlags(c.Flags)
// Run is used to run the static website generator
func Run(c *config.Config) {
if !c.Hugo {
out, err := exec.Command(c.Command, c.Args...).Output()
if err != nil {
log.Panic("Can't execute the commands defined on Caddyfile.")
log.Print(out)
}
return
}
commands.HugoCmd.ParseFlags(c.Args)
commands.HugoCmd.Run(commands.HugoCmd, make([]string, 0))
}