-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
122 lines (114 loc) · 2.78 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2020 The GoSchedule Authors. All rights reserved.
// Use of this source code is governed by BSD
// license that can be found in the LICENSE file.
package main
import (
"time"
"github.com/gin-gonic/gin"
"github.com/jasonjoo2010/goschedule-console/app"
"github.com/jasonjoo2010/goschedule-console/controller"
"github.com/jasonjoo2010/goschedule-console/controller/config"
"github.com/jasonjoo2010/goschedule-console/controller/scheduler"
"github.com/jasonjoo2010/goschedule-console/controller/strategy"
"github.com/jasonjoo2010/goschedule-console/controller/task"
)
func add(numbers ...interface{}) interface{} {
if len(numbers) < 1 {
return 0
}
if len(numbers) < 2 {
return numbers[0]
}
var total int64
var totalf float64
float_result := false
for _, num := range numbers {
switch n := num.(type) {
case int8:
total += int64(n)
totalf += float64(n)
case int:
total += int64(n)
totalf += float64(n)
case int16:
total += int64(n)
totalf += float64(n)
case int32:
total += int64(n)
totalf += float64(n)
case int64:
total += n
totalf += float64(n)
case uint:
total += int64(n)
totalf += float64(n)
case uint8:
total += int64(n)
totalf += float64(n)
case uint16:
total += int64(n)
totalf += float64(n)
case uint32:
total += int64(n)
totalf += float64(n)
case uint64:
total += int64(n)
totalf += float64(n)
case float32:
float_result = true
totalf += float64(n)
case float64:
float_result = true
totalf += n
}
}
if float_result {
return totalf
}
return total
}
func timestampMillis(millis int64) string {
tm := time.Unix(millis/1000, (millis%1000)*time.Hour.Milliseconds())
return tm.Format("2006-01-02 15:04:05")
}
func timestamp(ts int64) string {
tm := time.Unix(ts, 0)
return tm.Format("2006-01-02 15:04:05")
}
func storageChecker(c *gin.Context) {
switch c.FullPath() {
case controller.GetBasePath() + "config/modify", controller.GetBasePath() + "config/save":
return
default:
}
store := app.Instance().Store
if store == nil {
c.Redirect(302, app.Instance().Conf.Base+"config/modify")
c.Abort()
return
}
}
func InitEngine() *gin.Engine {
engine := gin.New()
engine.FuncMap["add"] = add
engine.FuncMap["timestamp"] = timestamp
engine.FuncMap["timestampMillis"] = timestampMillis
engine.LoadHTMLGlob("templates/**/*")
rootGroup := engine.Group(controller.GetBasePath())
rootGroup.Static("/css", "static/css")
rootGroup.Static("/js", "static/js")
rootGroup.Use(gin.Recovery())
rootGroup.Use(storageChecker)
rootGroup.Use(func(c *gin.Context) {
if gin.Mode() == gin.ReleaseMode {
c.Set("ENV", "production")
} else {
c.Set("ENV", "development")
}
})
config.Init(rootGroup)
strategy.Init(rootGroup)
scheduler.Init(rootGroup)
task.Init(rootGroup)
return engine
}