From 8ed78288a7e5301e858a47f4096a6bf45297e9d7 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 7 Jun 2016 16:08:52 +0100 Subject: [PATCH] updates on #71 --- config/config.go | 11 +++--- hugo.go | 45 +++++++++++++++--------- routes/browse/get.go | 8 ++--- {config => tools/installer}/installer.go | 2 +- 4 files changed, 39 insertions(+), 27 deletions(-) rename {config => tools/installer}/installer.go (99%) diff --git a/config/config.go b/config/config.go index 0dd7a04f..27685810 100644 --- a/config/config.go +++ b/config/config.go @@ -5,7 +5,8 @@ import ( "path/filepath" "strings" - "github.com/mholt/caddy/caddy/setup" + "github.com/hacdias/caddy-hugo/tools/installer" + "github.com/mholt/caddy" ) // Config is the add-on configuration set on Caddyfile @@ -19,16 +20,16 @@ type Config struct { Git bool // Is this site a git repository } -// ParseHugo parses the configuration file -func ParseHugo(c *setup.Controller) (*Config, error) { +// Parse parses the configuration file +func Parse(c *caddy.Controller, root string) (*Config, error) { conf := &Config{ - Public: strings.Replace(c.Root, "./", "", -1), + Public: strings.Replace(root, "./", "", -1), Admin: "/admin", Path: "./", Git: false, } - conf.Hugo = GetPath() + conf.Hugo = installer.GetPath() for c.Next() { args := c.RemainingArgs() diff --git a/hugo.go b/hugo.go index 03263162..bc0a211d 100644 --- a/hugo.go +++ b/hugo.go @@ -23,50 +23,61 @@ import ( "github.com/hacdias/caddy-hugo/tools/commands" "github.com/hacdias/caddy-hugo/tools/hugo" "github.com/hacdias/caddy-hugo/tools/server" - "github.com/mholt/caddy/caddy/setup" - "github.com/mholt/caddy/middleware" + "github.com/mholt/caddy" + "github.com/mholt/caddy/caddyhttp/httpserver" ) +func init() { + caddy.RegisterPlugin("hugo", caddy.Plugin{ + ServerType: "http", + Action: setup, + }) +} + // Setup is the init function of Caddy plugins and it configures the whole // middleware thing. -func Setup(c *setup.Controller) (middleware.Middleware, error) { - config, _ := config.ParseHugo(c) +func setup(c *caddy.Controller) error { + cnf := httpserver.GetConfig(c.Key) + conf, _ := config.Parse(c, cnf.Root) // Checks if there is an Hugo website in the path that is provided. // If not, a new website will be created. create := true - if _, err := os.Stat(config.Path + "config.yaml"); err == nil { + if _, err := os.Stat(conf.Path + "config.yaml"); err == nil { create = false } - if _, err := os.Stat(config.Path + "config.json"); err == nil { + if _, err := os.Stat(conf.Path + "config.json"); err == nil { create = false } - if _, err := os.Stat(config.Path + "config.toml"); err == nil { + if _, err := os.Stat(conf.Path + "config.toml"); err == nil { create = false } if create { - err := commands.Run(config.Hugo, []string{"new", "site", config.Path, "--force"}, ".") + err := commands.Run(conf.Hugo, []string{"new", "site", conf.Path, "--force"}, ".") if err != nil { log.Panic(err) } } // Generates the Hugo website for the first time the plugin is activated. - go hugo.Run(config, true) + go hugo.Run(conf, true) - return func(next middleware.Handler) middleware.Handler { - return &CaddyHugo{Next: next, Config: config} - }, nil + mid := func(next httpserver.Handler) httpserver.Handler { + return CaddyHugo{Next: next, Config: conf} + } + + cnf.AddMiddleware(mid) + return nil } // CaddyHugo contais the next middleware to be run and the configuration // of the current one. type CaddyHugo struct { - Next middleware.Handler + Next httpserver.Handler Config *config.Config } @@ -74,7 +85,7 @@ type CaddyHugo struct { // request to its function. func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { // Only handle /{admin} path - if middleware.Path(r.URL.Path).Matches(h.Config.Admin) { + if httpserver.Path(r.URL.Path).Matches(h.Config.Admin) { var err error var page string code := 404 @@ -108,15 +119,15 @@ func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error if r.URL.Path == h.Config.Admin+"/settings/" { var frontmatter string - if _, err := os.Stat(h.Config.Path + "config.yaml"); err == nil { + if _, err = os.Stat(h.Config.Path + "config.yaml"); err == nil { frontmatter = "yaml" } - if _, err := os.Stat(h.Config.Path + "config.json"); err == nil { + if _, err = os.Stat(h.Config.Path + "config.json"); err == nil { frontmatter = "json" } - if _, err := os.Stat(h.Config.Path + "config.toml"); err == nil { + if _, err = os.Stat(h.Config.Path + "config.toml"); err == nil { frontmatter = "toml" } diff --git a/routes/browse/get.go b/routes/browse/get.go index 130d7ea6..bc4ffb1d 100644 --- a/routes/browse/get.go +++ b/routes/browse/get.go @@ -6,8 +6,8 @@ import ( "github.com/hacdias/caddy-hugo/tools/templates" "github.com/hacdias/caddy-hugo/tools/variables" - "github.com/mholt/caddy/middleware" - "github.com/mholt/caddy/middleware/browse" + "github.com/mholt/caddy/caddyhttp/browse" + "github.com/mholt/caddy/caddyhttp/httpserver" ) // GET handles the GET method on browse page and shows the files listing Using @@ -26,13 +26,13 @@ func GET(w http.ResponseWriter, r *http.Request) (int, error) { // Using Caddy's Browse middleware b := browse.Browse{ - Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { + Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { return 404, nil }), Configs: []browse.Config{ { PathScope: "/", - Root: http.Dir(conf.Path), + Root: http.Dir(conf.Path), Variables: conf, Template: tpl, }, diff --git a/config/installer.go b/tools/installer/installer.go similarity index 99% rename from config/installer.go rename to tools/installer/installer.go index 3e08aa5e..4355f0c5 100644 --- a/config/installer.go +++ b/tools/installer/installer.go @@ -1,4 +1,4 @@ -package config +package installer import ( "crypto/sha256"