initial implementation
This commit is contained in:
parent
f8b0abc517
commit
cb373e637b
16 changed files with 777 additions and 1 deletions
71
pkg/router/pattern.go
Normal file
71
pkg/router/pattern.go
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
package router
|
||||
|
||||
import "strings"
|
||||
|
||||
func splitPattern(pattern string) (method, host, path string) {
|
||||
rest := pattern
|
||||
if i := strings.Index(pattern, " "); i >= 0 {
|
||||
method = pattern[:i]
|
||||
rest = strings.TrimLeft(pattern[i+1:], " ")
|
||||
}
|
||||
if strings.HasPrefix(rest, "/") {
|
||||
return method, "", rest
|
||||
}
|
||||
if i := strings.Index(rest, "/"); i >= 0 {
|
||||
return method, rest[:i], rest[i:]
|
||||
}
|
||||
return method, rest, ""
|
||||
}
|
||||
|
||||
func validateGroupPrefix(p string) {
|
||||
if p == "" {
|
||||
return
|
||||
}
|
||||
if strings.ContainsAny(p, " \t") {
|
||||
panic("lightmux: group prefix must not contain whitespace (no method or host allowed): " + p)
|
||||
}
|
||||
if !strings.HasPrefix(p, "/") {
|
||||
panic("lightmux: group prefix must start with '/': " + p)
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeGroupPrefix(p string) string {
|
||||
if p == "" {
|
||||
return ""
|
||||
}
|
||||
if len(p) > 1 && strings.HasSuffix(p, "/") {
|
||||
return p[:len(p)-1]
|
||||
}
|
||||
if p == "/" {
|
||||
return ""
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func joinPath(prefix, sub string) string {
|
||||
if prefix == "" {
|
||||
return sub
|
||||
}
|
||||
if sub == "" {
|
||||
return prefix
|
||||
}
|
||||
if sub == "/" {
|
||||
return prefix + "/"
|
||||
}
|
||||
if !strings.HasPrefix(sub, "/") {
|
||||
panic("lightmux: route path must start with '/': " + sub)
|
||||
}
|
||||
return prefix + sub
|
||||
}
|
||||
|
||||
func buildPattern(method, prefix, pattern string) string {
|
||||
m, host, path := splitPattern(pattern)
|
||||
if method != "" {
|
||||
m = method
|
||||
}
|
||||
full := host + joinPath(prefix, path)
|
||||
if m != "" {
|
||||
return m + " " + full
|
||||
}
|
||||
return full
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue