/
middlewares.go
43 lines (39 loc) · 903 Bytes
/
middlewares.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
package fileserver
import (
"net/http"
"os"
"path"
"github.com/gin-gonic/gin"
)
func (s *Server) directoryCheck() gin.HandlerFunc {
return func(ctx *gin.Context) {
p := path.Join(s.config.BaseDirectory, ctx.Param("directory"))
if !directoryExists(p) {
if ctx.Request.Method == "POST" {
os.Mkdir(p, 0755)
} else {
ctx.String(http.StatusNotFound, "No such file or directory")
ctx.Abort()
return
}
}
ctx.Next()
}
}
func (s *Server) authMiddleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
method := ctx.Request.Method
if method == "POST" || method == "DELETE" {
token := ctx.GetHeader("Authentication")
if token != s.config.SecretKey {
ctx.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"status": "forbidden",
"message": "Forbidden access to this method",
"method": method,
})
return
}
}
ctx.Next()
}
}