【发布时间】:2020-06-14 02:31:47
【问题描述】:
有没有办法在 *http.Request 对象被解析并转发到 Gorilla mux router 处理程序之前捕获它?
例如,我们有一些路由映射及其处理程序:
r := mux.NewRouter()
r.HandleFunc("/products/{key}", ProductHandler)
r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
我计划使用动态语言前缀(2 个符号)。示例:
没有语言代码(用于默认语言选项):
https://example.com/products/1
https://example.com/articels/2
带有语言代码:
https://example.com/ru/products/1
https://example.com/ru/articels/2
有没有办法在中间件中捕获完整的 URL,提取语言(如果存在),然后在进行一些修改后将其传递给 Gorilla mux 路由器?这将有助于构建漂亮的 URL:
https://example.com/products/1 <- default language
https://example.com/ru/products/1 <- russian language (same resource but in different language)
这看起来比这个变种更有吸引力:
https://example.com/en/products/1 <- mandatory default language
https://example.com/ru/products/1 <- russian language
【问题讨论】:
-
使用
http.ServeMux并在"/"模式下注册一个处理程序,然后让处理程序做任何事情,然后执行gorilla mux的ServeHTTP方法,将w和r传递给它. -
@mkopriva,非常有趣的解决方案。你能写一个答案吗?我会接受的