-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
70 lines (63 loc) · 1.66 KB
/
http.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
package date_agent
import (
"fmt"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"k8s.io/klog/v2"
"net/http"
)
func InitHttp(addr string, hub *Hub) *http.Server {
router := gin.New()
//router.Use(gin.LoggerWithConfig(gin.LoggerConfig{Output: writer}), gin.RecoveryWithWriter(writer))
router.Use(cors.Default())
router.LoadHTMLGlob("templates/*")
router.StaticFS("/statics", http.Dir("./statics"))
router.GET("/hello", func(c *gin.Context) {
// todo handle request and return data by hub
for index, value := range hub.tasks {
fmt.Println("taskId", hub.taskId)
fmt.Printf("%+v\n", index)
fmt.Printf("%+v\n", value)
}
fmt.Println("hub.ret", hub.ret)
c.JSON(http.StatusOK, hub.ret)
})
router.GET("/getJobs", func(c *gin.Context) {
fmt.Printf("%+v\n", hub)
c.JSON(http.StatusOK, hub.ret)
})
router.POST("/changeTime", func(c *gin.Context) {
//name := c.PostForm("hostname")
command := c.PostForm("command")
go func() {
//<-time.After(time.Second * 10)
klog.Info("new task")
hub.NewTask([]string{command})
}()
c.JSON(http.StatusOK, hub.ret)
})
router.POST("/getHub", func(c *gin.Context) {
var l int
if len(hub.tasks) > 5 {
l = len(hub.tasks) - 5
}
c.JSONP(http.StatusOK, gin.H{"tasks": hub.tasks[l:], "ret": hub.ret})
})
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{})
})
server := &http.Server{
Addr: addr,
Handler: router,
}
go func() {
if err := server.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
klog.Info("Server closed under request")
} else {
klog.V(2).Info("Server closed unexpected err:", err)
}
}
}()
return server
}