-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmain.go
More file actions
87 lines (76 loc) · 1.86 KB
/
main.go
File metadata and controls
87 lines (76 loc) · 1.86 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"docker-pull/src/controller"
"docker-pull/src/util"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/robfig/cron"
)
func initClearDist() {
c := cron.New()
timer, err := strconv.Atoi(util.AllConf["clearDistTime"])
if err != nil {
timer = 6
}
c.AddFunc("0 */1 * * *", func() {
files, errDir := os.ReadDir(util.DistPath)
if errDir != nil {
return
}
for _, file := range files {
if !file.IsDir() {
fileName := file.Name()
filePath := path.Join(util.DistPath, fileName)
fileinfo, err := os.Stat(filePath)
if err == nil && filepath.Ext(filePath) == ".tar" {
modTime := fileinfo.ModTime()
duration := time.Since(modTime)
if duration.Hours() > float64(timer) {
fmt.Println("删除过期文件:", filePath)
os.Remove(filePath)
}
}
}
}
})
c.Start()
}
func main() {
if util.AllConf["registry"] == "" {
panic("registry is empty")
}
if _, err := os.Stat(util.DistPath); os.IsNotExist(err) {
os.Mkdir(util.DistPath, 0755)
}
gin.SetMode(gin.ReleaseMode)
// 禁用控制台颜色,将日志写入文件时不需要控制台颜色。
gin.DisableConsoleColor()
// 记录到文件。
f, _ := os.Create(path.Join(util.LogPath, "gin.log"))
gin.DefaultWriter = io.MultiWriter(f)
router := gin.Default()
staticFile := path.Join(util.StaticPath)
router.GET("/", func(c *gin.Context) {
c.File(staticFile + "/" + "index.html")
})
router.GET("/:fileName", func(c *gin.Context) {
fileName := c.Param("fileName")
if fileName == "" {
fileName = "index.html"
}
c.File(staticFile + "/" + fileName)
})
router.GET("/api/pull", func(c *gin.Context) {
controller.Pull(c)
})
router.Static("/dist/down", util.DistPath)
fmt.Println("registry: ", util.AllConf["registry"])
initClearDist()
router.Run(":" + util.AllConf["port"])
}