forked from filebrowser/filebrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hugo.go
86 lines (71 loc) · 2.09 KB
/
hugo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//go:generate go-bindata -pkg assets -o assets/assets.go static/css/ static/js/ templates/ static/
package hugo
import (
"mime"
"net/http"
"path/filepath"
"strings"
"github.com/hacdias/caddy-hugo/assets"
"github.com/hacdias/caddy-hugo/edit"
"github.com/hacdias/caddy-hugo/settings"
"github.com/mholt/caddy/config/setup"
"github.com/mholt/caddy/middleware"
"github.com/spf13/hugo/commands"
)
// Setup function
func Setup(c *setup.Controller) (middleware.Middleware, error) {
commands.Execute()
return func(next middleware.Handler) middleware.Handler {
return &handler{Next: next}
}, nil
}
type handler struct{ Next middleware.Handler }
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
if middleware.Path(r.URL.Path).Matches("/admin") {
page := parseComponents(r)[1]
if page == "static" {
filename := strings.Replace(r.URL.Path, "/admin/", "", 1)
file, err := assets.Asset(filename)
if err != nil {
return 404, nil
}
extension := filepath.Ext(filename)
mime := mime.TypeByExtension(extension)
header := w.Header()
header.Set("Content-Type", mime)
w.Write(file)
return 200, nil
} else if page == "content" {
w.Write([]byte("Content Page"))
return 200, nil
} else if page == "browse" {
w.Write([]byte("Show Data Folder"))
return 200, nil
} else if page == "edit" {
return edit.Execute(w, r)
} else if page == "settings" {
return settings.Execute(w, r)
}
return 404, nil
}
return h.Next.ServeHTTP(w, r)
}
// TODO: utils package
func parseComponents(r *http.Request) []string {
//The URL that the user queried.
path := r.URL.Path
path = strings.TrimSpace(path)
//Cut off the leading and trailing forward slashes, if they exist.
//This cuts off the leading forward slash.
if strings.HasPrefix(path, "/") {
path = path[1:]
}
//This cuts off the trailing forward slash.
if strings.HasSuffix(path, "/") {
cutOffLastCharLen := len(path) - 1
path = path[:cutOffLastCharLen]
}
//We need to isolate the individual components of the path.
components := strings.Split(path, "/")
return components
}