-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
52 lines (45 loc) · 1.27 KB
/
router.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
package controller
import (
"io/fs"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
"github.com/myml/web-file-manager/internal/handle"
)
type API struct {
handle.MoveF
handle.DownloadF
handle.ListF
handle.UploadF
handle.MkdirF
handle.DeleteF
handle.CopyFileF
handle.WebDavH
}
func NewEngine(uiFS fs.FS, api API) *gin.Engine {
engine := gin.Default()
if pass := os.Getenv("PASSWORD"); len(pass) > 0 {
engine.Use(gin.BasicAuth(gin.Accounts{os.Getenv("USER"): pass}))
}
r := engine.Group("api")
r.GET("/dl/:name", handle.WarpF(api.DownloadF))
r.GET("/file", handle.WarpF(api.ListF))
r.POST("/file", handle.WarpF(api.UploadF))
r.POST("/file/move", handle.WarpF(api.MoveF))
r.POST("/file/copy", handle.WarpF(api.CopyFileF))
r.POST("/file/mkdir", handle.WarpF(api.MkdirF))
r.DELETE("/file", handle.WarpF(api.DeleteF))
for _, method := range strings.Split("OPTIONS, LOCK, GET, HEAD, POST, DELETE, PROPPATCH, COPY, MOVE, UNLOCK, PROPFIND, PUT", ", ") {
engine.Handle(method, "/dav/*path", gin.HandlerFunc(api.WebDavH))
}
engine.StaticFS("app", http.FS(uiFS))
engine.NoRoute(func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/api") {
c.String(http.StatusNotFound, "no route")
return
}
c.FileFromFS("/", http.FS(uiFS))
})
return engine
}