Skip to content

Commit

Permalink
Merge pull request #20 from hacdias/caddy-cms
Browse files Browse the repository at this point in the history
progresses on #19
  • Loading branch information
hacdias committed Sep 27, 2015
2 parents 1062c30 + 77b3713 commit 7b68c78
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 27 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ Deploy your Hugo website and enjoy of an admin interface with Caddy server.
## Configuration

```
hugo {
cms {
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

Expand Down
16 changes: 8 additions & 8 deletions hugo.go → cms.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
39 changes: 27 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package config

import (
"strconv"
"strings"

"github.com/mholt/caddy/config/setup"
)

// 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() {
Expand All @@ -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()
}
}
Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion editor/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
17 changes: 14 additions & 3 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net/http"
"os"
"os/exec"
"reflect"
"strings"
"text/template"
Expand Down Expand Up @@ -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))
}

Expand Down

0 comments on commit 7b68c78

Please sign in to comment.