-
Notifications
You must be signed in to change notification settings - Fork 0
/
routing.go
156 lines (144 loc) · 4.67 KB
/
routing.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
// GO Lang :: SmartGo / Web Server / Routing :: Smart.Go.Framework
// (c) 2020-2024 unix-world.org
// r.20240114.2007 :: STABLE
// Req: go 1.16 or later (embed.FS is N/A on Go 1.15 or lower)
package websrv
import (
"log"
smart "github.com/unix-world/smartgo"
)
func UrlHandlerRegisterRoute(route string, skipAuth bool, methods []string, maxTailSegments int, fxHandler HttpHandlerFunc) bool {
//--
defer smart.PanicHandler()
//--
if(handlersAreLocked == true) {
log.Println("[ERROR]", smart.CurrentFunctionName(), msgErrHandlersLocked, "Route: `" + route + "`")
return false
} //end if
//--
handlersWriteMutex.Lock()
defer handlersWriteMutex.Unlock()
//--
route = smart.StrTrimWhitespaces(route)
if(route == "") {
log.Println("[ERROR]", smart.CurrentFunctionName(), "Invalid Route: Empty")
return false
} //end if
//--
if(!webUrlRouteIsValid(route)) {
log.Println("[ERROR]", smart.CurrentFunctionName(), "Invalid Route: Contains Invalid or Unsafe Characters: `" + route + "`")
return false
} //end if
//--
if(!smart.StrStartsWith(route, "/")) {
log.Println("[ERROR]", smart.CurrentFunctionName(), "Invalid Route: Must Start with a `/` Slash: `" + route + "`")
return false
} //end if
if(route != "/") {
if(smart.StrEndsWith(route, "/")) { // {{{SYNC-PATH-FROM-SLASH-REDIRECT}}} ; this in combination
log.Println("[ERROR]", smart.CurrentFunctionName(), "Invalid Route: Must NOT End with a `/` Slash, except Web Root, which is always `/`: `" + route + "`")
return false
} //end if
} //end if
//--
if((route == webDavUrlPath()) || (smart.StrStartsWith(route, webDavUrlPath()+"/"))) { // {{{SYNC-WEBSRV-ROUTE-WEBDAV}}}
log.Println("[ERROR]", smart.CurrentFunctionName(), "Invalid Route: Disallowed (Reserved for WebDAV Service): `" + route + "`")
return false
} //end if
//--
if((route == "/lib") || smart.StrStartsWith(route, "/lib/")) { // reserved for assets
log.Println("[ERROR]", smart.CurrentFunctionName(), "Invalid Route: Disallowed (Reserved for Assets): `" + route + "`")
return false
} //end if
//--
if(fxHandler == nil) {
log.Println("[ERROR]", smart.CurrentFunctionName(), "Invalid Handler for Route: NULL: `" + route + "`")
return false
} //end if
//--
_, ok := urlHandlersMap[route]
if(ok) {
log.Println("[ERROR]", smart.CurrentFunctionName(), "Route already have set a Handler. To replace a route it must be Un-Registered first: `" + route + "`")
return false
} //end if
//--
if(methods == nil) {
methods = []string{}
} //end if
var allowedMethods []string = AllowedMethods // OPTIONS is reserved !!
var allowedSafeMethods []string = []string{}
for _, method := range methods {
method = smart.StrToUpper(smart.StrTrimWhitespaces(method))
if(!smart.InListArr(method, allowedMethods)) {
log.Println("[ERROR]", smart.CurrentFunctionName(), "Invalid Method [" + method + "] for Route: `" + route + "`")
return false
} else {
allowedSafeMethods = append(allowedSafeMethods, method)
} //end if else
} //end for
if(len(allowedSafeMethods) <= 0) {
allowedSafeMethods = append(allowedSafeMethods, "HEAD")
allowedSafeMethods = append(allowedSafeMethods, "GET")
allowedSafeMethods = append(allowedSafeMethods, "POST")
} //end if
//--
sr := smartRoute{
AuthSkip: skipAuth,
AllowedMethods: allowedSafeMethods,
MaxTailSegments: maxTailSegments,
FxHandler: fxHandler,
}
urlHandlersMap[route] = sr
//--
return true
//--
} //END FUNCTION
func UrlHandlerUnRegisterRoute(route string) bool {
//--
defer smart.PanicHandler()
//--
if(handlersAreLocked == true) {
log.Println("[ERROR]", smart.CurrentFunctionName(), msgErrHandlersLocked, "Route: `" + route + "`")
return false
} //end if
//--
handlersWriteMutex.Lock()
defer handlersWriteMutex.Unlock()
//--
route = smart.StrTrimWhitespaces(route)
if(route == "") {
log.Println("[ERROR]", smart.CurrentFunctionName(), "Invalid Route: Empty")
return false
} //end if
//--
if((urlHandlersMap == nil) || (len(urlHandlersMap) <= 0)) {
log.Println("[WARNING]", smart.CurrentFunctionName(), "No Registered URL Handlers Found")
return true
} //end if
//--
_, ok1 := urlHandlersMap[route]
if(!ok1) {
log.Println("[ERROR]", smart.CurrentFunctionName(), "Invalid Route: Not Registered: `" + route + "`")
} else {
delete(urlHandlersMap, route)
} //end if else
//--
return true
//--
} //END FUNCTION
func listAuthSkipRoutes() []string {
//--
var skipAuthRoutes []string = []string{}
//--
if((urlHandlersMap != nil) && (len(urlHandlersMap) > 0)) {
for route, sr := range urlHandlersMap {
if(sr.AuthSkip == true) {
skipAuthRoutes = append(skipAuthRoutes, route)
} //end if
} //end for
} //end if
//--
return skipAuthRoutes
//--
} //END FUNCTION
// #END