Search code examples
gogo-gin

Go gin: how to read multiple route segments at all?


I add a PUT /:folder route to create a folder

I need a PUT /:folder/:path/to/final/file, to be used when user post a new file. So I have the root in the first paramer, but I don't know how to create a route handling 'n' routes segments and the read as a single string

For example callling

PUT /cats

will create folder named cats. This is already working

I need

PUT /cats/milady/first-year/32312.jpg 

to identify 'cats' as first parameter, it's my user first-level-folder and then get mylady/first-year as nested subfolders to check and create if needed

and the 32312.jpg a filename

I ask you kindly how to setup route using gin PUT


Solution

  • I found an answer.

    I can create group, so under a group I can use the asterisk for 'undefined level of path'

    superGroup := router.Group("/:folder")
    {
            
        // Create a folder
        superGroup.PUT("", createFolder)
    
        // Save file into folder
        superGroup.PUT("/*full-path", uploadFile)
    }
    

    Then I can read the full-path and the folder

    func uploadFile(c *gin.Context) {
    
        folder:= c.Param("folder")
        fullPath := c.Param("full-path")
    
        ....
    
        c.Status(http.StatusOK)
    }