feat: upload route
This commit is contained in:
parent
f2e666874d
commit
aad953f0b5
5 changed files with 79 additions and 15 deletions
|
@ -8,5 +8,5 @@ import (
|
|||
func Register(cfg *types.StereoConfig) {
|
||||
api := cfg.Router.Group("/api")
|
||||
|
||||
routes.RegisterPingRoutes(api)
|
||||
routes.RegisterUploadRoutes(cfg, api)
|
||||
}
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
package routes
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterPingRoutes(api *gin.RouterGroup) {
|
||||
api.GET("/ping", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "Pong!")
|
||||
})
|
||||
}
|
24
internal/api/routes/upload.go
Normal file
24
internal/api/routes/upload.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package routes
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"stereo.cat/backend/internal/types"
|
||||
)
|
||||
|
||||
func RegisterUploadRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
|
||||
api.POST("/upload", func(c *gin.Context) {
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{"error": "file is required"})
|
||||
return
|
||||
}
|
||||
|
||||
filePath := filepath.Join(cfg.ImagePath, file.Filename)
|
||||
if err := c.SaveUploadedFile(file, filePath); err != nil {
|
||||
c.JSON(500, gin.H{"error": "failed to save file"})
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue