英文:
How to use "slashUrls" instead of QueryString. (without ordering)
问题
我正在使用gorilla-mux来路由我的URL,但是我遇到了一个困难:
我的客户更喜欢使用斜杠而不是传统的查询字符串作为URL。我的意思是:
domain/handler/filter1/val1/filter2/val2...
而不是
domain/handler?filter1=val1&filter2=val2...
重要问题:
当使用查询字符串时,'变量'的顺序不重要,任何一个变量都可以缺失而不会导致错误的路由或NotFound。
目前,我正在编写一个排列算法来创建URL,以便使用同一个函数处理它们。
有更好的方法吗?
英文:
I'm using gorilla-mux to route my urls but I found a difficulty:
My client prefer a url with slashes instead of a traditional query string. I mean:
domain/handler/filter1/val1/filter2/val2...
Instead of
domain/handler?filter1=val1&filter2=val2...
Important Issues:
When using query string, the 'variables' order isn't important and any of them can be missing without get a wrong routing or a NotFound.
When using query string, the order of 'vars' isn't important and I can miss any of them without get a wrong routing
At this moment I'm writting a permutation algorithm that create urls for handle them with same function.
Is there a better way to do that?
答案1
得分: 0
我为自己的需求编写了一个“URL生成器”。
https://github.com/daniloanp/SlashedQueryUrls
它非常简单和朴素,但对我来说很有效。
一个很好的使用示例在我的存储库中:
package main
import (
"fmt"
"github.com/daniloanp/muxUrlGen"
"github.com/gorilla/mux"
"net/http"
)
func HandleUrlsWithFunc(rtr *mux.Router, urls []string, handler func(w http.ResponseWriter, r *http.Request)) {
for _, url := range urls {
rtr.HandleFunc(url, handler)
}
}
func echoVars(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, r.URL.String(), "\n\n")
for k, i := range mux.Vars(r) {
fmt.Fprintln(w, k, ": ", i)
}
}
func main() {
var url string
var urls []string
rtr := mux.NewRouter()
url = "/handl/v1/{v}/v2/{v'}"
rtr.HandleFunc(url, echoVars)
// The above code works by we cannot missing any var OR Even
// Using "long notation"
urls = muxUrlGen.GetUrlVarsPermutations("/handlLong/v1/{v:[0-9]+}/v2/{v'}", true)
HandleUrlsWithFunc(rtr, urls, echoVars)
// Using "long notation" and Optional vars
urls = muxUrlGen.GetUrlVarsPermutations("/handlLongOptional/v1/{v}?/v2/{v'}?", true)
HandleUrlsWithFunc(rtr, urls, echoVars)
// Using "short notation"
urls = muxUrlGen.GetUrlVarsPermutations("/handlShort/{v1}/{v2}", false)
HandleUrlsWithFunc(rtr, urls, echoVars)
// Using "short notation" and Optional vars
urls = muxUrlGen.GetUrlVarsPermutations("/handlShortOptional/{v1}?/{v2}?", false)
HandleUrlsWithFunc(rtr, urls, echoVars)
http.Handle("/", rtr)
fmt.Println("Server running at http://127.0.0.1:8080")
http.ListenAndServe(":8080", nil)
}
谢谢大家。
英文:
I wrote a "url generator" for my needs
https://github.com/daniloanp/SlashedQueryUrls
It's very simple and naive but it's working for me.
A good example of usage is in my reposiry:
package main
import (
"fmt"
"github.com/daniloanp/muxUrlGen"
"github.com/gorilla/mux"
"net/http"
)
func HandleUrlsWithFunc(rtr *mux.Router, urls []string, handler func(w http.ResponseWriter, r *http.Request)) {
for _, url := range urls {
rtr.HandleFunc(url, handler)
}
}
func echoVars(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, r.URL.String(), "\n\n")
for k, i := range mux.Vars(r) {
fmt.Fprintln(w, k, ": ", i)
}
}
func main() {
var url string
var urls []string
rtr := mux.NewRouter()
url = "/handl/v1/{v}/v2/{v'}"
rtr.HandleFunc(url, echoVars)
// The above code works by we cannot missing any var OR Even
// Using "long notation"
urls = muxUrlGen.GetUrlVarsPermutations("/handlLong/v1/{v:[0-9]+}/v2/{v'}", true)
HandleUrlsWithFunc(rtr, urls, echoVars)
// Using "long notation" and Optional vars
urls = muxUrlGen.GetUrlVarsPermutations("/handlLongOptional/v1/{v}?/v2/{v'}?", true)
HandleUrlsWithFunc(rtr, urls, echoVars)
// Using "short notation"
urls = muxUrlGen.GetUrlVarsPermutations("/handlShort/{v1}/{v2}", false)
HandleUrlsWithFunc(rtr, urls, echoVars)
// Using "short notation" and Optional vars
urls = muxUrlGen.GetUrlVarsPermutations("/handlShortOptional/{v1}?/{v2}?", false)
HandleUrlsWithFunc(rtr, urls, echoVars)
http.Handle("/", rtr)
fmt.Println("Server running at http://127.0.0.1:8080")
http.ListenAndServe(":8080", nil)
}
Thanks for everyone.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论