Changeable admin URL. Close #69.

This commit is contained in:
Henrique Dias 2016-06-07 07:54:10 +01:00
parent 4b58519ac0
commit 5953045264
2 changed files with 22 additions and 13 deletions

View File

@ -15,6 +15,7 @@ type Config struct {
Styles string // Admin styles path Styles string // Admin styles path
Args []string // Hugo arguments Args []string // Hugo arguments
Hugo string // Hugo executable path Hugo string // Hugo executable path
Admin string // Hugo admin URL
Git bool // Is this site a git repository Git bool // Is this site a git repository
} }
@ -22,6 +23,7 @@ type Config struct {
func ParseHugo(c *setup.Controller) (*Config, error) { func ParseHugo(c *setup.Controller) (*Config, error) {
conf := &Config{ conf := &Config{
Public: strings.Replace(c.Root, "./", "", -1), Public: strings.Replace(c.Root, "./", "", -1),
Admin: "/admin",
Path: "./", Path: "./",
Git: false, Git: false,
} }
@ -49,6 +51,13 @@ 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 "admin":
if !c.NextArg() {
return nil, c.ArgErr()
}
conf.Admin = c.Val()
conf.Admin = strings.TrimPrefix(conf.Admin, "/")
conf.Admin = "/" + conf.Admin
default: default:
key := "--" + c.Val() key := "--" + c.Val()
value := "true" value := "true"

26
hugo.go
View File

@ -73,8 +73,8 @@ type CaddyHugo struct {
// ServeHTTP is the main function of the whole plugin that routes every single // ServeHTTP is the main function of the whole plugin that routes every single
// request to its function. // request to its function.
func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { func (h CaddyHugo) 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(h.Config.Admin) {
var err error var err error
var page string var page string
code := 404 code := 404
@ -96,16 +96,16 @@ func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
} }
} }
// If the current page is only "/admin/", redirect to "/admin/browse/content/" // If the current page is only "/{admin}/", redirect to "/{admin}/browse/content/"
if r.URL.Path == "/admin/" { if r.URL.Path == h.Config.Admin+"/" {
http.Redirect(w, r, "/admin/browse/content/", http.StatusTemporaryRedirect) http.Redirect(w, r, h.Config.Admin+"/browse/content/", http.StatusTemporaryRedirect)
return 0, nil return 0, nil
} }
// If the url matches exactly with /admin/settings/ serve that page // If the url matches exactly with /{admin}/settings/ serve that page
// page variable isn't used here to avoid people using URLs like // page variable isn't used here to avoid people using URLs like
// "/admin/settings/something". // "/{admin}/settings/something".
if r.URL.Path == "/admin/settings/" { if r.URL.Path == h.Config.Admin+"/settings/" {
var frontmatter string 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 {
@ -120,13 +120,13 @@ func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
frontmatter = "toml" frontmatter = "toml"
} }
http.Redirect(w, r, "/admin/edit/config."+frontmatter, http.StatusTemporaryRedirect) http.Redirect(w, r, h.Config.Admin+"/edit/config."+frontmatter, http.StatusTemporaryRedirect)
return 0, nil return 0, nil
} }
// Serve the static assets // Serve the static assets
if page == "assets" { if page == "assets" {
code, err = serveAssets(w, r) code, err = serveAssets(w, r, h.Config)
} }
// Browse page // Browse page
@ -154,9 +154,9 @@ func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
return h.Next.ServeHTTP(w, r) return h.Next.ServeHTTP(w, r)
} }
// serveAssets handles the /admin/assets requests // serveAssets handles the /{admin}/assets requests
func serveAssets(w http.ResponseWriter, r *http.Request) (int, error) { func serveAssets(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
filename := strings.Replace(r.URL.Path, "/admin/assets", "public", 1) filename := strings.Replace(r.URL.Path, c.Admin+"/assets", "public", 1)
file, err := assets.Asset(filename) file, err := assets.Asset(filename)
if err != nil { if err != nil {