我添加 PUT /:folder 路由来创建文件夹
我需要一个 PUT /:folder/:path/to/final/file,以便在用户发布新文件时使用。所以我在第一个参数中有根,但我不知道如何创建一个处理“n”个路由段的路由并将其读取为单个字符串
例如调用
PUT /cats
将创建名为 cats 的文件夹。这已经可以工作了
我需要
PUT /cats/milady/first-year/32312.jpg
将“cats”识别为第一个参数,它是我的用户一级文件夹 然后将 mylady/firstyear 作为嵌套子文件夹进行检查并根据需要创建
和 32312.jpg 文件名
请问如何使用 gin 设置路线 放置
我找到了答案。
我可以创建组,因此在组下我可以使用星号表示“未定义的路径级别”
superGroup := router.Group("/:folder") { // Create a folder superGroup.PUT("", createFolder) // Save file into folder superGroup.PUT("/*full-path", uploadFile) }
然后我可以读取 full-path
和 folder
func uploadFile(c *gin.Context) { folder:= c.Param("folder") fullPath := c.Param("full-path") .... c.Status(http.StatusOK) }