duanmao1975 2016-03-23 18:07
浏览 31
已采纳

如何在大猩猩复用路由中获得OR模式

I'm trying to use Gorilla mux router to handle paths that respond to a limited list of strings in the URL. The service I am developing will take files from the caller and pass them through an "adapter" that send them to S3 or OneDrive, depending on the "adapter" specified in the URL. I also require a variable named "schema", which I only mention now because of the weirdness that follows. My test is as follows ({schema} will be set to "test"):

router.HandleFunc("/{adapter:(s3|onedrive)}/{schema:[a-z]+}/check",
func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte(fmt.Sprintf(`{"a":"%s","s":"%s"}`,
        mux.Vars(r)["adapter"], mux.Vars(r)["schema"])))
}).Methods("GET")

I would expect that going to /s3/test/check would yield {"a":"s3","s":"test"} just as going to /onedrive/test/check should yield {"a":"onedrive","s":"test"} ... however in these cases I am getting {"a":"s3","s":"s3"} and {"a":"onedrive","s":"onedrive"} respectively.

The (s3|onedrive) check seems to be enforced because, for example, trying to go to /dropbox/test/check correctly yields a 404.

Why is the {schema} variable getting the value of the {adapter} variable and how can I fix this?

  • 写回答

1条回答 默认 最新

  • douqipi9704 2016-03-23 20:18
    关注

    I think it's because of parenthesis which denote capturing group and yield submatch. It can interfere with gorilla matcher. Just try without parenthesis.

    router.HandleFunc("/{adapter:s3|onedrive}/{schema:[a-z]+}/check",
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题