pull/29/head
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 ## Configuration
``` ```
<<<<<<< HEAD
staticmin { staticmin {
=======
cms {
>>>>>>> 7b68c78b12d9b7aee61788d5e85cd3b0722906de
styles file 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. + **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 ## Build it from source

View File

@ -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 hugo package cms
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.ParseHugo(c) config, _ := config.ParseCMS(c)
utils.RunHugo(config) utils.Run(config)
return func(next middleware.Handler) middleware.Handler { return func(next middleware.Handler) middleware.Handler {
return &CaddyHugo{Next: next, Config: config} return &CaddyCMS{Next: next, Config: config}
}, nil }, nil
} }
// CaddyHugo main type // CaddyCMS main type
type CaddyHugo struct { type CaddyCMS struct {
Next middleware.Handler Next middleware.Handler
Config *config.Config 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 // 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
@ -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 // Whenever the header "X-Refenerate" is true, the website should be
// regenerated. Used in edit and settings, for example. // regenerated. Used in edit and settings, for example.
if r.Header.Get("X-Regenerate") == "true" { if r.Header.Get("X-Regenerate") == "true" {
utils.RunHugo(h.Config) utils.Run(h.Config)
} }
return code, err return code, err

View File

@ -1,6 +1,7 @@
package config package config
import ( import (
"strconv"
"strings" "strings"
"github.com/mholt/caddy/config/setup" "github.com/mholt/caddy/config/setup"
@ -8,15 +9,15 @@ 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 {
Styles string Styles string
Flags []string Hugo bool
Args []string
Command string
} }
// ParseHugo parses the configuration file // ParseCMS parses the configuration file
func ParseHugo(c *setup.Controller) (*Config, error) { func ParseCMS(c *setup.Controller) (*Config, error) {
conf := &Config{ conf := &Config{Hugo: true}
Styles: "",
}
for c.Next() { for c.Next() {
for c.NextBlock() { for c.NextBlock() {
@ -30,9 +31,23 @@ func ParseHugo(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 "flags": case "hugo":
conf.Flags = c.RemainingArgs() if !c.NextArg() {
if len(conf.Flags) == 0 { 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() return conf, c.ArgErr()
} }
} }
@ -44,7 +59,7 @@ func ParseHugo(c *setup.Controller) (*Config, error) {
} }
func (c *Config) parseFlags() { func (c *Config) parseFlags() {
for index, element := range c.Flags { for index, element := range c.Args {
c.Flags[index] = strings.Replace(element, "\"", "", -1) 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 return
} }
utils.RunHugo(c) utils.Run(c)
}) })
scheduler.Start() scheduler.Start()
} }

View File

@ -6,6 +6,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"os/exec"
"reflect" "reflect"
"strings" "strings"
"text/template" "text/template"
@ -158,9 +159,19 @@ func ParseComponents(r *http.Request) []string {
return components return components
} }
// RunHugo is used to run hugo // Run is used to run the static website generator
func RunHugo(c *config.Config) { func Run(c *config.Config) {
commands.HugoCmd.ParseFlags(c.Flags) 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)) commands.HugoCmd.Run(commands.HugoCmd, make([]string, 0))
} }