-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin.go
558 lines (460 loc) · 14.5 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package crud
import (
"context"
"database/sql"
"embed"
"fmt"
"html/template"
"net/http"
"path"
"sort"
"strings"
"time"
"github.com/go-chi/chi/v5"
_ "github.com/lib/pq"
)
//go:embed all:templates
var templates embed.FS
//go:embed all:assets
var assets embed.FS
// handler represents a http handler.
type handler func(w http.ResponseWriter, r *http.Request)
// Formatter represents a column formatter.
type Formatter func(in any) string
// Entity represents a database entity.
type Entity struct {
// Entity table name.
TableName string
// Primary key column name. default is "id".
PrimaryKey string
// Entity title plural. default is the entity table name.
TitlePlural string
// Entity title singular. default is the entity table name.
TitleSingular string
// FavIcon represents the entity fav icon. default is empty.
FavIcon string
// Menu order. default is 0.
Order int
// Entity description. default is empty.
Description string
// SelectColumns columns. if nil, all columns will be selected.
SelectColumns []string
// EditColumns columns. if nil, all columns will be selected.
EditColumns []string
// NewColumns columns. if nil, edit columns or all columns will be selected.
NewColumns []string
// ColumnNameFormatter represents the column name formatter. if provided, the formatter will be used to format the column name.
ColumnNameFormatter map[string]Formatter
// ValueFormatters for each column. if provided, the formatter will be used to format the column value.
ValueFormatters map[string]Formatter
}
// Admin represents the admin module.
type Admin struct {
// DatabaseURI represents the database uri. default is "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable".
DatabaseURI string
// DatabaseEngine represents the database engine. default is "postgres".
databaseEngine string
// DatabaseConn represents the database connection.
db *DB
// Entities represents the entities of the admin module.
Entities map[string]Entity
// BaseURL represents the base url for the admin module. default is "/admin".
BaseURL string
// DefaultFormatters represents the default formatters for each column. if provided, the formatter will be used to format the column value.
// if a column has a formatter, the formatter will be used instead of the default formatter.
DefaultFormatters map[string]Formatter
// TemplateFuncs represents the template funcs for the admin module.
TemplateFuncs template.FuncMap
// Templates represents the templates for the admin module. will override the default templates.
Templates map[string]*template.Template
// PermissionChecker represents the permission checker for the admin module.
PermissionChecker func(r *http.Request, userID, entityName, action string) bool
// UserIdentifier represents the function that returns the user identifier from the request.
UserIdentifier func(r *http.Request) string
// SearchHandler represents the search handler for the admin module.
SearchHandler func(r *http.Request, query string) ([]SearchResult, error)
}
// New returns a new admin module.
func New(opts ...Option) (*Admin, error) {
a := &Admin{
Entities: make(map[string]Entity),
}
for _, opt := range opts {
if err := opt(a); err != nil {
return nil, err
}
}
if a.DatabaseURI == "" {
a.DatabaseURI = "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable"
}
if a.databaseEngine == "" {
a.databaseEngine = "postgres"
}
if a.BaseURL == "" {
a.BaseURL = "/admin"
}
a.db = &DB{
URI: a.DatabaseURI,
Engine: a.databaseEngine,
}
if _, err := a.db.Open(context.Background()); err != nil {
return nil, err
}
return a, nil
}
// GetMux prepares the admin module handlers.
func (a *Admin) GetMux() http.Handler {
r := chi.NewRouter()
r.Route(path.Join(a.BaseURL, "/"), func(r chi.Router) {
fileServer(r, "/", http.FS(assets))
r.With(a.checkUserPermission).Get("/", a.dashboard)
r.With(a.checkUserPermission).Get("/search", a.searchView)
r.With(a.checkUserPermission).Get("/entity/{entity}", a.getEntityList)
r.With(a.checkUserPermission).Get("/entity/{entity}/new", a.getEntityNew)
r.With(a.checkUserPermission).Post("/entity/{entity}/new", a.createEntity)
r.With(a.checkUserPermission).Get("/entity/{entity}/{entityID}", a.getEntityEdit)
r.With(a.checkUserPermission).Post("/entity/{entity}/{entityID}", a.updateEntity)
r.With(a.checkUserPermission).Get("/entity/{entity}/{entityID}/delete", a.deleteEntity)
})
return r
}
func (a *Admin) searchView(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query().Get("q")
data := SearchData{
BaseContextData: a.getBaseContextData(),
Query: query,
ResultCount: 2,
}
if a.SearchHandler != nil {
res, err := a.SearchHandler(r, query)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data.Results = res
}
if err := a.executeTemplate(w, "search", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (a *Admin) createEntity(w http.ResponseWriter, r *http.Request) {
entityName := chi.URLParam(r, "entity")
entity, ok := a.Entities[entityName]
if !ok {
a.renderNotFoundPage(w, r)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
columns := make([]Column, 0)
for column, value := range r.Form {
if column == entity.PrimaryKey {
continue
}
columns = append(columns, Column{
Name: column,
Value: value[0],
})
}
if err := a.db.CreateEntity(r.Context(), entity.TableName, entity.PrimaryKey, columns); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, path.Join(a.BaseURL, "/entity/", entity.TableName), http.StatusFound)
}
func (a *Admin) updateEntity(w http.ResponseWriter, r *http.Request) {
entityName := chi.URLParam(r, "entity")
entityID := chi.URLParam(r, "entityID")
entity, ok := a.Entities[entityName]
if !ok {
a.renderNotFoundPage(w, r)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
columns := make([]Column, 0)
for column, value := range r.Form {
if column == entity.PrimaryKey {
continue
}
columns = append(columns, Column{
Name: column,
Value: value[0],
})
}
if err := a.db.UpdateEntity(r.Context(), entity.TableName, entity.PrimaryKey, entityID, columns); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, path.Join(a.BaseURL, "/entity/", entity.TableName, entityID), http.StatusFound)
}
func (a *Admin) getEntityList(w http.ResponseWriter, r *http.Request) {
entityName := chi.URLParam(r, "entity")
entity, ok := a.Entities[entityName]
if !ok {
a.renderNotFoundPage(w, r)
return
}
rows, columens, err := a.db.GetTableColumenRows(r.Context(), entity.TableName, entity.PrimaryKey, entity.getSelectColumns())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
data := ListData{
Title: entity.TitlePlural,
EntityName: entity.TableName,
Description: entity.Description,
Columns: columens,
Rows: rows,
BaseContextData: a.getBaseContextData(),
}
if err := a.executeTemplate(w, "list", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (a *Admin) getEntityEdit(w http.ResponseWriter, r *http.Request) {
// get entity name from url and call list with that name
entityName := chi.URLParam(r, "entity")
entityID := chi.URLParam(r, "entityID")
entity, ok := a.Entities[entityName]
if !ok {
a.renderNotFoundPage(w, r)
return
}
row, err := a.db.GetEntityByID(r.Context(), entity.TableName, entity.PrimaryKey, entity.getEditColumns(), entityID)
if err != nil && err != sql.ErrNoRows {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err == sql.ErrNoRows {
a.renderNotFoundPage(w, r)
return
}
data := EditData{
Title: entity.TitleSingular,
Description: entity.Description,
EntityName: entityName,
EntityID: entityID,
Row: *row,
IsEdit: true,
BaseContextData: a.getBaseContextData(),
}
if err := a.executeTemplate(w, "edit", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (a *Admin) getEntityNew(w http.ResponseWriter, r *http.Request) {
// get entity name from url and call list with that name
entityName := chi.URLParam(r, "entity")
entity, ok := a.Entities[entityName]
if !ok {
a.renderNotFoundPage(w, r)
return
}
row, err := a.db.GetTableRow(r.Context(), entity.TableName, entity.PrimaryKey, entity.getNewColumns())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := EditData{
EntityName: entityName,
Title: entity.TitleSingular,
Description: entity.Description,
Row: *row,
IsEdit: false,
BaseContextData: a.getBaseContextData(),
}
if err := a.executeTemplate(w, "new", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (a *Admin) deleteEntity(w http.ResponseWriter, r *http.Request) {
// get entity name from url and call list with that name
entityName := chi.URLParam(r, "entity")
entityID := chi.URLParam(r, "entityID")
entity, ok := a.Entities[entityName]
if !ok {
a.renderNotFoundPage(w, r)
return
}
if err := a.db.DeleteEntityByID(r.Context(), entity.TableName, entity.PrimaryKey, entityID); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, path.Join(a.BaseURL, "/entity/", entity.TableName), http.StatusFound)
}
func (a *Admin) dashboard(w http.ResponseWriter, r *http.Request) {
data := a.getBaseContextData()
if err := a.executeTemplate(w, "dashboard", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (a *Admin) renderNotFoundPage(w http.ResponseWriter, r *http.Request) {
data := a.getBaseContextData()
if err := a.executeTemplate(w, "not_found", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (a *Admin) renderNotAuthorised(w http.ResponseWriter, r *http.Request) {
data := a.getBaseContextData()
if err := a.executeTemplate(w, "not_authorised", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (a *Admin) executeTemplate(w http.ResponseWriter, name string, data any) error {
tmpl, err := template.New("base").Funcs(templateFuncs()).ParseFS(templates, "templates/*.html")
if err != nil {
return err
}
// add template funcs
tmpl.Funcs(a.TemplateFuncs)
// add default formatters
for column, formatter := range a.DefaultFormatters {
if _, ok := a.TemplateFuncs[column]; !ok {
a.TemplateFuncs[column] = formatter
}
}
// template overrides
if tmpl, ok := a.Templates[name]; ok {
return tmpl.Execute(w, data)
}
return tmpl.ExecuteTemplate(w, name, data)
}
func (a *Admin) getMenus() []Menu {
menus := make([]Menu, 0)
for _, entity := range a.Entities {
menus = append(menus, Menu{
Idenifier: entity.TableName,
Title: entity.TitlePlural,
URL: path.Join(a.BaseURL, "/entity/", entity.TableName),
FavIcon: entity.FavIcon,
Order: entity.Order,
})
}
// sort menus by order
sort.Slice(menus, func(i, j int) bool {
return menus[i].Order < menus[j].Order
})
return menus
}
func (a *Admin) login(w http.ResponseWriter, r *http.Request) {
if err := a.executeTemplate(w, "login", nil); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (a *Admin) register(w http.ResponseWriter, r *http.Request) {
if err := a.executeTemplate(w, "register", nil); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (a *Admin) forgetPassword(w http.ResponseWriter, r *http.Request) {
if err := a.executeTemplate(w, "forget_password", nil); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (a *Admin) checkUserPermission(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if a.UserIdentifier != nil {
userIdentifier := a.UserIdentifier(r)
if userIdentifier == "" {
http.Redirect(w, r, path.Join(a.BaseURL, "/login"), http.StatusFound)
return
}
if a.PermissionChecker != nil {
var action string
switch r.Method {
case http.MethodGet:
action = "read"
case http.MethodPost:
action = "create"
}
entiryID := chi.URLParam(r, "entityID")
if entiryID != "" && action == "create" {
action = "update"
}
if strings.Contains(r.URL.Path, "delete") {
action = "delete"
}
entityName := chi.URLParam(r, "entity")
if entityName != "" {
if !a.PermissionChecker(r, userIdentifier, entityName, action) {
a.renderNotAuthorised(w, r)
return
}
}
}
handler.ServeHTTP(w, r)
}
})
}
func (a *Admin) getBaseContextData() BaseContextData {
return BaseContextData{
BaseURL: a.BaseURL,
Menus: a.getMenus(),
ShowSearchBar: a.SearchHandler != nil,
}
}
// FileServer conveniently sets up a http.FileServer handler to serve
// static files from a http.FileSystem.
func fileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit any URL parameters.")
}
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
fs := http.StripPrefix(pathPrefix, http.FileServer(root))
fs.ServeHTTP(w, r)
})
}
func (e Entity) getSelectColumns() []string {
if len(e.SelectColumns) == 0 {
return []string{"*"}
}
return e.SelectColumns
}
func (e Entity) getEditColumns() []string {
if len(e.EditColumns) == 0 {
return []string{"*"}
}
return e.EditColumns
}
func (e Entity) getNewColumns() []string {
if len(e.NewColumns) == 0 {
if len(e.EditColumns) != 0 {
return e.EditColumns
}
return []string{"*"}
}
return e.NewColumns
}
func goTypeToHTMLType(goType string) string {
switch goType {
case "string":
return "text"
case "int":
return "number"
case "bool":
return "checkbox"
case "time.Time":
return "datetime-local"
default:
return "text"
}
}
func goValueToHTMLValue(goType string, value any) string {
switch goType {
case "time.Time":
return value.(time.Time).Format("2006-01-02T15:04")
default:
return fmt.Sprintf("%v", value)
}
}