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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
package admin
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/nilslice/cms/content"
"github.com/nilslice/cms/management/editor"
"github.com/nilslice/cms/management/manager"
"github.com/nilslice/cms/system/db"
)
func init() {
http.HandleFunc("/admin", func(res http.ResponseWriter, req *http.Request) {
adminView, err := Admin(nil)
if err != nil {
fmt.Println(err)
res.WriteHeader(http.StatusInternalServerError)
return
}
res.Header().Set("Content-Type", "text/html")
res.Write(adminView)
})
http.HandleFunc("/admin/static/", func(res http.ResponseWriter, req *http.Request) {
path := req.URL.Path
pwd, err := os.Getwd()
if err != nil {
log.Fatal("Coudln't get current directory to set static asset source.")
}
http.ServeFile(res, req, filepath.Join(pwd, "system", path))
})
http.HandleFunc("/admin/configure", func(res http.ResponseWriter, req *http.Request) {
adminView, err := Admin(nil)
if err != nil {
fmt.Println(err)
res.WriteHeader(http.StatusInternalServerError)
return
}
res.Header().Set("Content-Type", "text/html")
res.Write(adminView)
})
http.HandleFunc("/admin/posts", func(res http.ResponseWriter, req *http.Request) {
q := req.URL.Query()
t := q.Get("type")
if t == "" {
res.WriteHeader(http.StatusBadRequest)
}
posts := db.GetAll(t)
b := &bytes.Buffer{}
p := content.Types[t]().(editor.Editable)
html := `<div class="col s9">
<div class="card">
<ul class="card-content collection posts">
<div class="card-title">` + t + ` Items</div>`
for i := range posts {
json.Unmarshal(posts[i], &p)
post := `<div class="row collection-item"><li class="col s12 collection-item"><a href="/admin/edit?type=` +
t + `&id=` + fmt.Sprintf("%d", p.ContentID()) +
`">` + p.ContentName() + `</a></li></div>`
b.Write([]byte(post))
}
b.Write([]byte(`</ul></div></div>`))
btn := `<div class="col s3"><a href="/admin/edit?type=` + t + `" class="btn new-post waves-effect waves-light">New ` + t + `</a></div></div>`
html = html + b.String() + btn
adminView, err := Admin([]byte(html))
if err != nil {
fmt.Println(err)
res.WriteHeader(http.StatusInternalServerError)
return
}
res.Header().Set("Content-Type", "text/html")
res.Write(adminView)
})
http.HandleFunc("/admin/edit", func(res http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodGet:
q := req.URL.Query()
i := q.Get("id")
t := q.Get("type")
contentType, ok := content.Types[t]
if !ok {
fmt.Fprintf(res, content.ErrTypeNotRegistered, t)
return
}
post := contentType()
if i != "" {
data, err := db.Get(t + ":" + i)
if err != nil {
fmt.Println(err)
res.WriteHeader(http.StatusInternalServerError)
return
}
err = json.Unmarshal(data, post)
if err != nil {
fmt.Println(err)
res.WriteHeader(http.StatusInternalServerError)
return
}
} else {
post.(editor.Editable).SetContentID(-1)
}
m, err := manager.Manage(post.(editor.Editable), t)
if err != nil {
fmt.Println(err)
res.WriteHeader(http.StatusInternalServerError)
return
}
adminView, err := Admin(m)
if err != nil {
fmt.Println(err)
res.WriteHeader(http.StatusInternalServerError)
return
}
res.Header().Set("Content-Type", "text/html")
res.Write(adminView)
case http.MethodPost:
err := req.ParseForm()
if err != nil {
fmt.Println(err)
res.WriteHeader(http.StatusBadRequest)
return
}
cid := req.FormValue("id")
t := req.FormValue("type")
// check for any multi-value fields (ex. checkbox fields)
// and correctly format for db storage. Essentially, we need
// fieldX.0: value1, fieldX.1: value2 => fieldX: []string{value1, value2}
var discardKeys []string
for k, v := range req.PostForm {
if strings.Contains(k, ".") {
key := strings.Split(k, ".")[0]
if req.PostForm.Get(key) == "" {
req.PostForm.Set(key, v[0])
discardKeys = append(discardKeys, k)
} else {
req.PostForm.Add(key, v[0])
}
}
}
for _, discardKey := range discardKeys {
req.PostForm.Del(discardKey)
}
id, err := db.Set(t+":"+cid, req.PostForm)
if err != nil {
fmt.Println(err)
res.WriteHeader(http.StatusInternalServerError)
return
}
scheme := req.URL.Scheme
host := req.URL.Host
path := req.URL.Path
sid := fmt.Sprintf("%d", id)
desURL := scheme + host + path + "?type=" + t + "&id=" + sid
http.Redirect(res, req, desURL, http.StatusFound)
}
})
}
// Run starts the Admin system on the port provided
func Run(port string) {
http.ListenAndServe(":"+port, nil)
}
|