-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.go
78 lines (67 loc) · 1.56 KB
/
crud.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
package crud_module
import (
"fmt"
"net/http"
"strings"
)
const (
CREATE ReqVerb = iota
READ
UPDATE
DELETE
GET
POST
PUT
PATCH
CONNECT
TRACE
CRUD
)
var RegistredMethods = []string{
"CREATE",
"READ",
"UPDATE",
"DELETE",
"GET",
"POST",
"PUT",
"PATCH",
"CONNECT",
"TRACE",
}
func CreateMultiHandlerCRUD(r MuxRouter, rawPath string, handlers IndividualCRUDHandlers) {
path := VetPath(rawPath)
exclusions := []string{}
for item, itemFunc := range handlers {
methodSelection, err := FindMethod(item)
if err != nil {
panic(err)
}
method := methodSelection[0]
r.Router.HandleFunc(path, itemFunc).Methods(method)
exclusions = append(exclusions, method)
}
LockAllOtherMethods(r, path, exclusions)
fmt.Println("Multi handlerCRUD: Created.", path, strings.Join(exclusions, " |"))
}
func CreateSingleHandlerCRUD(r MuxRouter, rawPath string, handler HandleFunc) {
methodSelection, err := FindMethod(CRUD)
if err != nil {
panic(err)
}
path := VetPath(rawPath)
for i := 0; i < len(methodSelection); i++ {
r.Router.HandleFunc(path, handler).Methods(methodSelection[i])
}
LockAllOtherMethods(r, path, methodSelection)
fmt.Println("CRUD: Created for ", strings.Join(methodSelection, ","))
}
func LockAllOtherMethods(r MuxRouter, path string, excluded []string) {
methods := RemoveItemsFromArray(RegistredMethods, excluded)
for i := 0; i < len(methods); i++ {
r.Router.HandleFunc(path, DefaultLockedMethod).Methods(methods[i])
}
}
func DefaultLockedMethod(w http.ResponseWriter, r *http.Request) {
http.Error(w, "403", http.StatusForbidden)
}