filebrowser/setup.go

189 lines
4.4 KiB
Go
Raw Normal View History

package hugo
import (
2016-07-02 22:29:47 +00:00
"fmt"
2016-06-21 14:28:15 +00:00
"io/ioutil"
"log"
2016-06-21 14:28:15 +00:00
"net/http"
"os"
2016-06-29 09:45:15 +00:00
"path/filepath"
2016-07-01 15:14:08 +00:00
"reflect"
2016-06-21 14:28:15 +00:00
"strings"
2016-06-21 14:28:15 +00:00
"github.com/hacdias/caddy-filemanager"
2016-06-28 13:59:33 +00:00
"github.com/hacdias/caddy-filemanager/config"
2016-06-29 09:45:15 +00:00
"github.com/hacdias/caddy-filemanager/directory"
"github.com/hacdias/caddy-filemanager/frontmatter"
2016-06-21 14:28:15 +00:00
"github.com/hacdias/caddy-hugo/installer"
2016-06-28 13:59:33 +00:00
"github.com/hacdias/caddy-hugo/utils/commands"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
2016-06-29 09:45:15 +00:00
// AssetsURL is the base url for the assets
2016-06-28 13:59:33 +00:00
const AssetsURL = "/_hugointernal"
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 *caddy.Controller) error {
2016-06-22 20:32:33 +00:00
cnf := httpserver.GetConfig(c)
2016-06-28 13:59:33 +00:00
conf, _ := parse(c, cnf.Root)
2016-07-01 14:01:21 +00:00
format := "toml"
2016-06-29 09:45:15 +00:00
// Checks if there is an Hugo website in the path that is provided.
// If not, a new website will be created.
create := true
2016-06-21 14:28:15 +00:00
if _, err := os.Stat(conf.Root + "config.yaml"); err == nil {
2016-06-29 09:45:15 +00:00
format = "yaml"
create = false
}
2016-06-21 14:28:15 +00:00
if _, err := os.Stat(conf.Root + "config.json"); err == nil {
2016-06-29 09:45:15 +00:00
format = "json"
create = false
}
2016-06-21 14:28:15 +00:00
if _, err := os.Stat(conf.Root + "config.toml"); err == nil {
2016-06-29 09:45:15 +00:00
format = "toml"
create = false
}
if create {
2016-06-28 13:59:33 +00:00
err := commands.Run(conf.Hugo, []string{"new", "site", conf.Root, "--force"}, ".")
if err != nil {
2016-07-02 22:29:47 +00:00
log.Fatal(err)
}
}
2016-06-29 09:45:15 +00:00
// Get Default FrontMatter
bytes, err := ioutil.ReadFile(filepath.Clean(conf.Root + "/config." + format))
if err != nil {
2016-07-02 22:29:47 +00:00
log.Println(err)
fmt.Printf("Can't get the default frontmatter from the configuration. %s will be used.\n", format)
2016-07-01 15:14:08 +00:00
} else {
2016-07-02 22:29:47 +00:00
bytes = directory.AppendFrontMatterRune(bytes, format)
f, err := frontmatter.Unmarshal(bytes)
if err != nil {
log.Println(err)
fmt.Printf("Can't get the default frontmatter from the configuration. %s will be used.\n", format)
} else {
kind := reflect.TypeOf(f)
if kind == reflect.TypeOf(map[interface{}]interface{}{}) {
if val, ok := f.(map[interface{}]interface{})["metaDataFormat"]; ok {
format = val.(string)
}
} else {
if val, ok := f.(map[string]interface{})["metaDataFormat"]; ok {
format = val.(string)
}
}
2016-07-01 15:14:08 +00:00
}
2016-06-29 09:45:15 +00:00
}
// Generates the Hugo website for the first time the plugin is activated.
2016-06-21 14:28:15 +00:00
go RunHugo(conf, true)
mid := func(next httpserver.Handler) httpserver.Handler {
2016-06-21 14:28:15 +00:00
return &Hugo{
Next: next,
Config: conf,
FileManager: &filemanager.FileManager{
Next: next,
2016-06-28 13:59:33 +00:00
Configs: []config.Config{
config.Config{
2016-06-22 20:32:33 +00:00
HugoEnabled: true,
PathScope: conf.Root,
2016-06-29 09:45:15 +00:00
FrontMatter: format,
2016-06-22 20:32:33 +00:00
Root: http.Dir(conf.Root),
BaseURL: conf.BaseURL,
2016-07-01 14:47:36 +00:00
AbsoluteURL: strings.Replace(cnf.Addr.Path+"/"+conf.BaseURL, "//", "/", -1),
2016-07-01 14:51:59 +00:00
AddrPath: strings.TrimSuffix(cnf.Addr.Path, "/"),
2016-06-22 20:32:33 +00:00
StyleSheet: conf.Styles,
2016-06-21 14:28:15 +00:00
},
},
},
}
}
cnf.AddMiddleware(mid)
return nil
}
2016-06-21 14:28:15 +00:00
2016-06-28 13:59:33 +00:00
// Config is a configuration for managing a particular hugo website.
2016-06-21 14:28:15 +00:00
type Config struct {
2016-06-28 13:59:33 +00:00
Public string // Public content path
Root string // Hugo files path
Hugo string // Hugo executable location
Styles string // Admin styles path
Args []string // Hugo arguments
BaseURL string // BaseURL of admin interface
FileManager *filemanager.FileManager
2016-06-21 14:28:15 +00:00
}
2016-06-28 13:59:33 +00:00
// Parse parses the configuration set by the user so it can be
// used by the middleware
func parse(c *caddy.Controller, root string) (*Config, error) {
2016-06-21 14:28:15 +00:00
conf := &Config{
Public: strings.Replace(root, "./", "", -1),
BaseURL: "/admin",
Root: "./",
2016-06-21 15:39:53 +00:00
}
2016-06-28 13:59:33 +00:00
conf.Hugo = installer.GetPath()
2016-06-21 14:28:15 +00:00
for c.Next() {
args := c.RemainingArgs()
switch len(args) {
case 1:
conf.Root = args[0]
conf.Root = strings.TrimSuffix(conf.Root, "/")
conf.Root += "/"
}
for c.NextBlock() {
switch c.Val() {
case "styles":
if !c.NextArg() {
return conf, c.ArgErr()
}
2016-06-28 13:59:33 +00:00
tplBytes, err := ioutil.ReadFile(c.Val())
2016-06-21 14:28:15 +00:00
if err != nil {
return conf, err
}
2016-06-28 13:59:33 +00:00
conf.Styles = string(tplBytes)
2016-06-21 14:28:15 +00:00
case "admin":
if !c.NextArg() {
2016-06-28 13:59:33 +00:00
return conf, c.ArgErr()
2016-06-21 14:28:15 +00:00
}
conf.BaseURL = c.Val()
conf.BaseURL = strings.TrimPrefix(conf.BaseURL, "/")
conf.BaseURL = "/" + conf.BaseURL
default:
key := "--" + c.Val()
value := "true"
if c.NextArg() {
value = c.Val()
}
conf.Args = append(conf.Args, key+"="+value)
}
}
}
return conf, nil
}