forked from gofiber/fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer.go
49 lines (44 loc) · 1.37 KB
/
layer.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
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofibel.io
package fiber
import "strings"
// Layer is a struct that holds all metadata for each registered handler
type Layer struct {
// Internal fields
use bool // USE matches path prefixes
star bool // Path equals '*' or '/*'
root bool // Path equals '/'
parsed parsedParams // parsed contains parsed params segments
// External fields for ctx.Route() method
Path string // Registered route path
Method string // HTTP method
Params []string // Slice containing the params names
Handler func(*Ctx) // Ctx handler
}
func (l *Layer) match(path string) (match bool, values []string) {
if l.use {
if l.root || strings.HasPrefix(path, l.Path) {
return true, values
}
// Check for a simple path match
} else if len(l.Path) == len(path) && l.Path == path {
return true, values
// Middleware routes allow prefix matches
} else if l.root && path == "/" {
return true, values
}
// '*' wildcard matches any path
if l.star {
return true, []string{path}
}
// Does this route have parameters
if len(l.Params) > 0 {
// Match params
if values, match = l.parsed.getMatch(path, l.use); match {
return
}
}
// No match
return false, values
}