-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
70 lines (59 loc) · 1.33 KB
/
main.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"fmt"
"net/http"
"path/filepath"
"github.com/gin-gonic/gin"
"github.com/kulics/go-webkit"
)
func main() {
err := webkit.NewWebServerDefault("localhost:8080").
HandleFuncGet("ping", func(ctx webkit.Context) {
ctx.String(http.StatusOK, "pong")
}).
HandleStruct("api", testRouter{}).
HandleStruct("file", fileRouter{}).
Run()
if err != nil {
fmt.Println(err)
return
}
}
type testRouter struct {
Item testItem
}
func (testRouter) GET(ctx webkit.Context) {
ctx.String(http.StatusOK, "get")
}
func (testRouter) POST(ctx webkit.Context) {
ctx.String(http.StatusOK, "post")
}
type testItem struct{}
func (testItem) GET(ctx webkit.Context) {
ctx.String(http.StatusOK, "get")
}
type fileRouter struct{}
func (fileRouter) GET(ctx webkit.Context) {
fPath := ctx.Query("path")
ctx.File(filepath.Clean(fPath))
}
func (fileRouter) POST(ctx webkit.Context) {
filePath := filepath.Clean(ctx.PostForm("path"))
file, err := ctx.FormFile("file")
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"message": "upload file err:" + err.Error(),
})
return
}
if err := ctx.SaveUploadedFile(file, `./`+
filePath); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"message": "upload file err:" + err.Error(),
})
return
}
ctx.JSON(http.StatusCreated, gin.H{
"message": "success",
})
}