/
route.go
64 lines (50 loc) · 1.46 KB
/
route.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
package api
import (
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/ismdeep/mirror-data/app/mirror/conf"
"github.com/ismdeep/mirror-data/app/mirror/internal/mirror"
)
var eng *gin.Engine
func init() {
gin.SetMode(conf.ROOT.Server.Mode)
eng = gin.New()
eng.GET("/*path", func(c *gin.Context) {
path := c.Param("path")
// if is a file
if fileInfo := mirror.M.GetFileInfo(path); fileInfo != nil {
ProxyFile(c, fileInfo)
return
}
// if is folder
ShowFolder(c, path)
})
eng.HEAD("/*path", func(c *gin.Context) {
path := c.Param("path")
fileInfo := mirror.M.GetFileInfo(path)
if fileInfo == nil {
c.Status(http.StatusNotFound)
return
}
c.Header("Content-Length", fmt.Sprintf("%v", fileInfo.ContentLength))
c.Header("Content-Type", fileInfo.ContentType)
c.Header("Accept-Ranges", "bytes")
c.Header("Last-Modified", fileInfo.LastModified.Format(time.RFC1123))
})
}
// Run http server
func Run() error {
fmt.Printf(`
# # ### ###### ###### ####### ######
## ## # # # # # # # # #
# # # # # # # # # # # # #
# # # # ###### ###### # # ######
# # # # # # # # # # #
# # # # # # # # # # #
# # ### # # # # ####### # #
`)
fmt.Println("API SERVER STARTED:", conf.ROOT.Server.Bind)
return eng.Run(conf.ROOT.Server.Bind)
}