Files
rpk/src/services.go

75 lines
1.3 KiB
Go

package main
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
type Service struct {
Port int
}
func (s Service) Run() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
// 重定向到 /ping
c.Redirect(http.StatusMovedPermanently, "/ping")
})
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": gin.H{
"message": "pong",
},
})
})
r.GET("/list", func(c *gin.Context) {
name := c.Query("name")
ps := ListProcess(name)
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": ps,
})
})
r.GET("/listps", func(c *gin.Context) {
name := c.Query("name")
ps := ListProcessesUsingPowerShell(name)
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": ps,
})
})
r.POST("/stop", func(c *gin.Context) {
var p Process
if err := c.ShouldBindJSON(&p); err == nil {
ps, err := StopProcess(p.Pid)
if err == nil {
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": gin.H{
"message": "A termination signal has been sent",
"process": ps,
},
})
} else {
c.JSON(http.StatusBadRequest, gin.H{
"code": 1,
"data": gin.H{
"message": "An unknown error occurred, see error for details",
"error": err.Error(),
},
})
}
}
})
r.Run(":" + strconv.Itoa(s.Port))
}