Example #1
0
func (r *RouteCollection) compileRoute(route *Route) *Route {
	compiledRoute := &Route{
		Handler:     route.Handler,
		Matchers:    route.Matchers,
		Middlewares: route.Middlewares,
		Meta: &RouteMeta{Name: route.GetMeta().Name,
			Pattern:      route.GetMeta().Pattern,
			Prefix:       r.Prefix,
			Methods:      route.GetMeta().Methods,
			URLVARPrefix: r.UrlVarPrefix},
	}
	compiledRoute.Matchers = append(
		append(
			matcher.Matchers{},
			matcher.Pattern(compiledRoute.GetMeta().Pattern, r.Prefix, r.UrlVarPrefix),
			matcher.Method(route.GetMeta().Methods...),
		),
		compiledRoute.Matchers...)
	if route.GetMeta().Name == "" {
		route.Meta.Name = strings.Trim(regexp.MustCompile(`[^a-z A-Z 0-9]`).ReplaceAllString(route.GetMeta().GetPath(), "_"), "_")
		if route.Meta.Name == "" {
			route.Meta.Name = "_"
		}
	}
	compiledRoute.Matchers = append(append([]matcher.Matcher{}, r.matchers...), compiledRoute.Matchers...)
	compiledRoute.Middlewares = append(append([]Middleware{}, r.middlewares...), compiledRoute.Middlewares...)
	return compiledRoute

}
Example #2
0
func ExamplePattern() {
	matcher := r.Pattern("/:foo/:bar", "/root/")
	fmt.Println(matcher.Regexp.String())
	r, err := http.NewRequest("GET", "https://acme.com/root/users/22a39b6", nil)
	if err != nil {
		fmt.Println(err)
	} else {
		match := matcher.Match(r)
		fmt.Println(match, r.URL.Query().Get(":foo"), r.URL.Query().Get(":bar"))
	}
	// Output:
	// ^/root/(?P<foo>[^\s /]+)/(?P<bar>[^\s /]+)/?$
	// true users 22a39b6

}
Example #3
0
func ExamplePattern_Second() {
	matcher := r.Pattern("/:foo/:*bar", "/root")
	fmt.Println(matcher.Regexp.String())

	r, err := http.NewRequest("GET", "http://example.com/root/static-assets/some/path/to/file/image.jpg", nil)
	if err != nil {
		fmt.Println(err)
	} else {
		isMatched := matcher.Match(r)
		fmt.Println(isMatched, r.URL.Query().Get(":foo"), r.URL.Query().Get(":bar"))
	}
	// Output:
	// ^/root/(?P<foo>[^\s /]+)/(?P<bar>[^\s]+)/?$
	// true static-assets some/path/to/file/image.jpg

}