updates on #71

This commit is contained in:
Henrique Dias 2016-06-07 16:08:52 +01:00
parent 5953045264
commit 8ed78288a7
4 changed files with 39 additions and 27 deletions

View File

@ -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()

45
hugo.go
View File

@ -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"
}

View File

@ -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,
},

View File

@ -1,4 +1,4 @@
package config
package installer
import (
"crypto/sha256"