-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
116 lines (95 loc) · 2.75 KB
/
main.go
File metadata and controls
116 lines (95 loc) · 2.75 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
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
package main
import (
"embed"
"html/template"
"io/fs"
"net/http"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/netty-community/pingx/config"
"github.com/netty-community/pingx/pkg/probe"
)
type PingRequest struct {
Hosts []string `json:"hosts" binding:"required"`
}
var pingManager *probe.PingManager
//go:embed templates/*
var templatesFS embed.FS
//go:embed static/*
var staticFS embed.FS
func main() {
// Initialize default configuration
config.Init()
// Initialize ping manager
pingManager = probe.NewPingManager()
router := gin.Default()
// Enable CORS
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST", "DELETE"},
AllowHeaders: []string{"Origin", "Content-Type"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
// Serve static files
tmpl := template.Must(template.New("").ParseFS(templatesFS, "templates/*"))
router.SetHTMLTemplate(tmpl)
static, err := fs.Sub(staticFS, "static")
if err != nil {
panic(err)
}
router.StaticFS("/static", http.FS(static))
// Serve the main page
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"title": "PingX - Real-time Batch Ping Tool",
})
})
// Configuration endpoints
router.POST("/api/ping/config", func(c *gin.Context) {
if err := c.ShouldBindJSON(&config.Config); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, config.Config)
})
router.GET("/api/ping/config", func(c *gin.Context) {
c.JSON(http.StatusOK, config.Config)
})
// Ping endpoints
router.POST("/api/ping/start", func(c *gin.Context) {
var req PingRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
pingManager.StartPinging(req.Hosts)
c.JSON(http.StatusOK, gin.H{"message": "Ping started"})
})
router.POST("/api/ping/stop", func(c *gin.Context) {
pingManager.StopPinging()
c.JSON(http.StatusOK, gin.H{"message": "Ping stopped"})
})
router.POST("/api/ping/clear", func(c *gin.Context) {
pingManager.StopPinging()
pingManager.ClearResults()
c.JSON(http.StatusOK, gin.H{"message": "Ping stopped and history cleared"})
})
router.GET("/api/ping/results", func(c *gin.Context) {
results := pingManager.GetResults()
c.JSON(http.StatusOK, results)
})
router.GET("/api/ping/history/:hostname", func(c *gin.Context) {
hostname := c.Param("hostname")
result := pingManager.GetHostHistory(hostname)
if result == nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Host not found"})
return
}
c.JSON(http.StatusOK, result)
})
// Start the server
router.Run(":8080")
}