This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
skipper.go
43 lines (39 loc) · 1.32 KB
/
skipper.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
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a MIT style license that can be found
// in the LICENSE file.
package clevergo
import "strings"
// Skipper is a function that indicates whether current request is skippable.
type Skipper func(c *Context) bool
// PathSkipper returns a skipper with the given patterns.
// Pattern has two forms, one is that contains a certain path, another contains a wildcard,
// both of them are case-insensitive.
// Pattern Path Skippable
// "" "/" false
// "/" "/" true
// "/" "/login" false
// "/login" "/login" true
// "/login" "/Login" true
// "/login" "/LOGIN" true
// "/guest*" "/guest" true
// "/guest*" "/guest/foo" true
// "/guest*" "/guest/bar" true
func PathSkipper(patterns ...string) Skipper {
return func(c *Context) bool {
for _, pattern := range patterns {
if pattern == "" {
continue
}
if pattern[len(pattern)-1] == '*' && len(c.Request.URL.Path) >= len(pattern)-1 {
length := len(pattern) - 1
if strings.EqualFold(c.Request.URL.Path[:length], pattern[:length]) {
return true
}
}
if strings.EqualFold(pattern, c.Request.URL.Path) {
return true
}
}
return false
}
}