Hugo preview mode. Update Plugin registration.

Former-commit-id: b6c33f0ae36e7caa8cf813eeb6f8fdeb554659e9 [formerly 4d62c13f0b7e5b7610a0c9037d646c2005e9ba88] [formerly 1e9ded0427d81084c92a68d5eddb8c7620fb189c [formerly ae8eaf96c43e252245bead932e43685c542d85e1]]
Former-commit-id: 76699219a6d176777f4478eab5141bba5a613a17 [formerly bb698073213359bebce84ea196b30e9467f448c0]
Former-commit-id: 538927b5a6fad024eb062cf746068372c384b883
This commit is contained in:
Henrique Dias
2017-08-08 10:21:25 +01:00
parent 4bae90c80f
commit 6d853d63ed
6 changed files with 79 additions and 18 deletions

36
http.go
View File

@@ -58,6 +58,30 @@ func serveHTTP(c *RequestContext, w http.ResponseWriter, r *http.Request) (int,
return apiHandler(c, w, r)
}
// Checks if any plugin has an handler for this URL.
for p := range c.Plugins {
var h PluginHandler
for path, handler := range plugins[p].Handlers {
if strings.HasPrefix(r.URL.Path, path) {
h = handler
r.URL.Path = strings.TrimPrefix(r.URL.Path, path)
break
}
}
if h == nil {
continue
}
valid, _ := validateAuth(c, r)
if !valid {
return http.StatusForbidden, nil
}
return h(c, w, r)
}
// Any other request should show the index.html file.
w.Header().Set("x-frame-options", "SAMEORIGIN")
w.Header().Set("x-content-type", "nosniff")
@@ -108,7 +132,11 @@ func apiHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int,
}
for p := range c.Plugins {
code, err := plugins[p].Handler.Before(c, w, r)
if plugins[p].BeforeAPI == nil {
continue
}
code, err := plugins[p].BeforeAPI(c, w, r)
if code != 0 || err != nil {
return code, err
}
@@ -149,7 +177,11 @@ func apiHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int,
}
for p := range c.Plugins {
code, err := plugins[p].Handler.After(c, w, r)
if plugins[p].AfterAPI == nil {
continue
}
code, err := plugins[p].AfterAPI(c, w, r)
if code != 0 || err != nil {
return code, err
}