I am using Golang(gin-gonic framework) for my backend. I have developed below rest API to receive images or files from the frontend applications. I am saving files in this path "/home/user/Desktop/files/"+dt.String()+"_"+filepath.Base(file.Filename). Till now everything fine, I can see the saved files.
Now I am planning to return these file paths to frontend via another rest API (Eg: API: /getImageUrl, Response: https://via.placeholder.com/600/771796) so that users can download files. But I am not sure how to return these file path URLs to frontend. Can somebody guide me on how to develop this rest API?
requests.POST("/upload", func(c *gin.Context) {
// Source
file, err := c.FormFile("file")
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
return
}
dt := time.Now()
filename := "/home/user/Desktop/files/"+dt.String()+"_"+filepath.Base(file.Filename)
if err := c.SaveUploadedFile(file, filename); err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
return
}
c.JSON(200, gin.H{"message": "Files Uploaded Successfully"})
})