-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.go
169 lines (135 loc) · 3.89 KB
/
admin.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
// "fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"strings"
)
type Dir struct {
Title string
Body []os.FileInfo
}
// TODO need a way to lock this to current hugo-site dir
func loadDir(title string) (*Dir, error) {
dir := title
body, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
return &Dir{Title: title, Body: body}, nil
}
// Might need a better name ; is a location pair where
// Root = site root (ie /admin/)
// Dir = relative file path (ie ./admin)
type Location struct {
Root string
Dir string
}
func serveAdmin(root string, dir string) {
// TODO some way to standardize roots / dirs (path is wonky)
// TODO templates should read adminroot (instead of hardcoding /admin/...)
admin := Location{root, dir}
// serve admin
http.HandleFunc(admin.Root, func(w http.ResponseWriter, r *http.Request) {
adminHandler(w, r, admin)
})
// serve assets
assetroot := admin.Root + "assets/"
assetdir := admin.Dir + "/assets"
http.Handle(assetroot, http.StripPrefix(assetroot, http.FileServer(http.Dir(assetdir))))
// API
http.HandleFunc(admin.Root+"edit/", func(w http.ResponseWriter, r *http.Request) {
editHandler(w, r, admin)
})
http.HandleFunc(admin.Root+"save/", func(w http.ResponseWriter, r *http.Request) {
saveHandler(w, r, admin)
})
http.HandleFunc(admin.Root+"login/", func(w http.ResponseWriter, r *http.Request) {
loginHandler(w, r, admin)
})
http.HandleFunc(admin.Root+"logout/", func(w http.ResponseWriter, r *http.Request) {
logoutHandler(w, r, admin)
})
http.HandleFunc(admin.Root+"manage-accounts/", func(w http.ResponseWriter, r *http.Request) {
adminUsersHandler(w, r, admin)
})
}
func adminHandler(w http.ResponseWriter, r *http.Request, admin Location) {
isAuth(w, r, admin, "user")
title := r.URL.Path[len(admin.Root):]
if title == "" {
title = "./"
}
p, err := loadDir(title)
if err != nil {
p = &Dir{Title: title}
}
renderTemplateDir(w, "view.html", p)
}
func editHandler(w http.ResponseWriter, r *http.Request, admin Location) {
// TODO : pass in
contentDir := getConfig("contentdir")
isAuth(w, r, admin, "user")
path := r.URL.Path[len(admin.Root+"edit/"):]
if strings.HasPrefix(path, contentDir) {
c, err := new(Page).contentRead(path)
if err != nil {
c = &Content{Path: path}
}
renderTemplateContent(w, "content-edit.html", c)
} else {
p, err := new(Page).Read(path)
if err != nil {
p = &Page{Path: path}
}
renderTemplatePage(w, "edit.html", p)
}
}
func saveHandler(w http.ResponseWriter, r *http.Request, admin Location) {
contentDir := getConfig("contentdir")
isAuth(w, r, admin, "user")
path := r.URL.Path[len(admin.Root+"save/"):]
if strings.HasPrefix(path, contentDir) {
var page Page
r.ParseForm()
fm := make(map[string]interface{})
for key, values := range r.Form {
for _, value := range values {
if strings.HasPrefix(key, "fm.") {
fm[key[3:]] = value
}
}
}
page.contentUpdate(path, fm, []byte(r.FormValue("body")))
} else {
page := &Page{
Path: path,
Body: r.FormValue("body"),
}
page.Update(path, []byte(r.FormValue("body")))
}
git(path) //this should probably only run if there have been changes
buildSite()
http.Redirect(w, r, admin.Root, http.StatusFound)
}
var templates = template.Must(template.ParseGlob("admin/templates/*"))
func renderTemplatePage(w http.ResponseWriter, tmpl string, p *Page) {
err := templates.ExecuteTemplate(w, tmpl, p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func renderTemplateContent(w http.ResponseWriter, tmpl string, c *Content) {
err := templates.ExecuteTemplate(w, tmpl, c)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func renderTemplateDir(w http.ResponseWriter, tmpl string, p *Dir) {
err := templates.ExecuteTemplate(w, tmpl, p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}