-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
141 lines (129 loc) · 3.67 KB
/
utils.go
File metadata and controls
141 lines (129 loc) · 3.67 KB
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
package slide
import (
"fmt"
"net/http"
"strings"
)
// ...
const (
GET = http.MethodGet
POST = http.MethodPost
PUT = http.MethodPut
DELETE = http.MethodDelete
ContentType = "Content-Type"
ContentDeposition = "Content-Disposition"
ApplicationJSON = "application/json"
Attachment = "attachment"
// cors headers
HeaderOrigin = "Origin"
HeaderVary = "Vary"
HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin"
HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods"
HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
HeaderAccessControlExposeHeaders = "Access-Control-Expose-Headers"
HeaderAccessControlRequestMethod = "Access-Control-Request-Method"
HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers"
HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers"
HeaderAccessControlMaxAge = "Access-Control-Max-Age"
routerRegexReplace = "[a-zA-Z0-9_-]*"
// routing error messages
NotFoundMessage = "Not Found, Check URL"
)
// Finds wild card in URL and replace them with a regex for,
// ex if path is /auth/:name -> /auth/[a-zA-Z0-9]*
// ex if path is /auth/name -> /auth/name
func findAndReplace(path string) string {
if !strings.Contains(path, ":") {
return fmt.Sprintf("%s%s%s", "^", path, "$")
}
result := ""
slitted := strings.Split(path, "/")
for _, v := range slitted {
if v == "" {
continue
}
if strings.Contains(v, ":") {
result = fmt.Sprintf("%s/%s", result, routerRegexReplace)
continue
}
result = fmt.Sprintf("%s/%s", result, v)
}
// replace slashes
result = strings.ReplaceAll(result, "/", "\\/")
result = fmt.Sprintf("%s%s%s", "^", result, "$")
return result
}
// routerPath /auth/:name
// requestPath /auth/madhuri
// paramName name
// returns madhuri
func extractParamFromPath(routerPath, requestPath, paramName string) string {
routerSplit := strings.Split(routerPath, "/")
requestSplit := strings.Split(requestPath, "/")
if len(routerSplit) != len(requestSplit) {
return ""
}
paramWithWildCard := fmt.Sprintf(":%s", paramName)
for k, v := range routerSplit {
if v == paramWithWildCard {
return requestSplit[k]
}
}
return ""
}
// routerPath /auth/:name/:age
// requestPath /auth/madhuri/32
// returns { name: madhuri, age: 32 }
func getParamsFromPath(routerPath, requestPath string) map[string]string {
paramsMap := map[string]string{}
routerSplit := strings.Split(routerPath, "/")
requestSplit := strings.Split(requestPath, "/")
for k, v := range routerSplit {
if strings.Contains(v, ":") {
key := strings.ReplaceAll(v, ":", "")
paramsMap[key] = requestSplit[k]
}
}
return paramsMap
}
// returns value of a single query Param
//
// route path /hello?key=test&value=bbp
//
// keyValue = GetQueryParam(key)
//
// keyValue = test
func getAllQueryParams(queryPath string) map[string]string {
queryParamsMap := map[string]string{}
params := strings.Split(queryPath, "&")
for _, v := range params {
if strings.Contains(v, "=") {
pair := strings.Split(v, "=")
queryParamsMap[pair[0]] = pair[1]
}
}
return queryParamsMap
}
// returns map of query Params
//
// route path /hello?key=test&value=bbp
//
// returns {key : test, value : bbp}
func getQueryParam(querypath string, name string) string {
params := strings.Split(querypath, "&")
for _, v := range params {
if strings.Contains(v, "=") {
pair := strings.Split(v, "=")
if pair[0] == name {
return pair[1]
}
}
}
return ""
}
func getAttachmentHeader(fileName string) string {
if fileName == "" {
return fmt.Sprintf(Attachment)
}
return fmt.Sprintf("%s; filename=%s", Attachment, fileName)
}