blob: c0ba6a5d321c1842325e6d0e4c83d9d380d98c48 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
package main
import (
"net/http"
"fmt"
"path/filepath"
"github.com/gin-gonic/gin"
)
func index(c *gin.Context){
}
func main(){
router := gin.Default()
router.POST("/", func(c *gin.Context) {
// single file
file, err := c.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
fmt.Println(file.Filename)
dst := filepath.Join("./files/", filepath.Base(file.Filename))
c.SaveUploadedFile(file, dst)
c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!\n", file.Filename))
})
router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, fmt.Sprintf("HTTP WORKS TOO!"))
})
var safeFS http.FileSystem = http.Dir("./files")
router.GET("/:name", func(c *gin.Context) {
c.FileFromFS(c.Param("name"), safeFS)
})
router.Run(":8080")
}
|