mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-21 12:33:59 +08:00
change: server
This commit is contained in:
111
server/main.go
111
server/main.go
@@ -2,7 +2,9 @@ package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog/log"
|
||||
@@ -20,31 +22,45 @@ type Router struct {
|
||||
|
||||
func (r *Router) Init() {
|
||||
r.Engine = gin.Default()
|
||||
r.Engine.Use(teardown())
|
||||
r.Engine.GET("/ping", pingHandler)
|
||||
r.Engine.GET("/", pingHandler)
|
||||
r.Engine.POST("/", pingHandler)
|
||||
r.Engine.GET("/api/v1/devices", listDeviceHandler)
|
||||
|
||||
apiV1Platform := r.Engine.Group("/api/v1").Group("/:platform")
|
||||
apiV1Platform.GET("/devices", listDeviceHandler)
|
||||
apiV1PlatformSerial := r.Group("/api/v1").Group("/:platform").Group("/:serial")
|
||||
|
||||
apiV1PlatformSerial := apiV1Platform.Group("/:serial")
|
||||
// UI operations
|
||||
apiV1PlatformSerial.POST("/ui/tap", r.HandleDeviceContext(), tapHandler)
|
||||
apiV1PlatformSerial.POST("/ui/drag", r.HandleDeviceContext(), dragHandler)
|
||||
apiV1PlatformSerial.POST("/ui/input", r.HandleDeviceContext(), inputHandler)
|
||||
apiV1PlatformSerial.POST("/ui/tap", tapHandler)
|
||||
apiV1PlatformSerial.POST("/ui/double_tap", doubleTapHandler)
|
||||
apiV1PlatformSerial.POST("/ui/drag", dragHandler)
|
||||
apiV1PlatformSerial.POST("/ui/input", inputHandler)
|
||||
apiV1PlatformSerial.POST("/ui/home", homeHandler)
|
||||
|
||||
// Key operations
|
||||
apiV1PlatformSerial.POST("/key/unlock", r.HandleDeviceContext(), unlockHandler)
|
||||
apiV1PlatformSerial.POST("/key/home", r.HandleDeviceContext(), homeHandler)
|
||||
// App operations
|
||||
apiV1PlatformSerial.GET("/app/foreground", r.HandleDeviceContext(), foregroundAppHandler)
|
||||
apiV1PlatformSerial.POST("/app/clear", r.HandleDeviceContext(), clearAppHandler)
|
||||
apiV1PlatformSerial.POST("/app/launch", r.HandleDeviceContext(), launchAppHandler)
|
||||
apiV1PlatformSerial.POST("/app/terminal", r.HandleDeviceContext(), terminalAppHandler)
|
||||
// get screen info
|
||||
apiV1PlatformSerial.GET("/screenshot", r.HandleDeviceContext(), screenshotHandler)
|
||||
apiV1PlatformSerial.POST("/screenresult", r.HandleDeviceContext(), screenResultHandler)
|
||||
apiV1PlatformSerial.GET("/adb/source", r.HandleDeviceContext(), adbSourceHandler)
|
||||
// run uixt actions
|
||||
apiV1PlatformSerial.POST("/uixt/action", r.HandleDeviceContext(), uixtActionHandler)
|
||||
apiV1PlatformSerial.POST("/uixt/actions", r.HandleDeviceContext(), uixtActionsHandler)
|
||||
apiV1PlatformSerial.POST("/key/unlock", unlockHandler)
|
||||
apiV1PlatformSerial.POST("/key/home", homeHandler)
|
||||
apiV1PlatformSerial.POST("/key/backspace", backspaceHandler)
|
||||
apiV1PlatformSerial.POST("/key", keycodeHandler)
|
||||
|
||||
// APP operations
|
||||
apiV1PlatformSerial.GET("/app/foreground", foregroundAppHandler)
|
||||
apiV1PlatformSerial.GET("/app/appInfo", appInfoHandler)
|
||||
apiV1PlatformSerial.POST("/app/clear", clearAppHandler)
|
||||
apiV1PlatformSerial.POST("/app/launch", launchAppHandler)
|
||||
apiV1PlatformSerial.POST("/app/terminal", terminalAppHandler)
|
||||
apiV1PlatformSerial.POST("/app/uninstall", uninstallAppHandler)
|
||||
|
||||
// Device operations
|
||||
apiV1PlatformSerial.GET("/screenshot", screenshotHandler)
|
||||
apiV1PlatformSerial.GET("/video", videoHandler)
|
||||
apiV1PlatformSerial.POST("/device/push_image", pushImageHandler)
|
||||
apiV1PlatformSerial.POST("/device/clear_image", clearImageHandler)
|
||||
apiV1PlatformSerial.GET("/adb/source", adbSourceHandler)
|
||||
|
||||
// uixt operations
|
||||
apiV1PlatformSerial.POST("/uixt/action", uixtActionHandler)
|
||||
apiV1PlatformSerial.POST("/uixt/actions", uixtActionsHandler)
|
||||
}
|
||||
|
||||
func (r *Router) Run(port int) error {
|
||||
@@ -57,5 +73,56 @@ func (r *Router) Run(port int) error {
|
||||
}
|
||||
|
||||
func pingHandler(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, HttpResponse{Code: 0, Message: "success"})
|
||||
RenderSuccess(c, true)
|
||||
}
|
||||
|
||||
func teardown() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
logID := c.Request.Header.Get("x-tt-logid")
|
||||
startTime := time.Now()
|
||||
// 结束处理后打印日志
|
||||
fmt.Printf("[GIN] %s | %s | %-7s \"%s\"\n",
|
||||
startTime.Format("2006/01/02 - 15:04:05"),
|
||||
logID,
|
||||
c.Request.Method,
|
||||
c.Request.URL.Path,
|
||||
)
|
||||
// 执行请求处理器
|
||||
c.Next()
|
||||
|
||||
driverObj, exists := c.Get("driver")
|
||||
if exists {
|
||||
if driver, ok := driverObj.(*uixt.XTDriver); ok {
|
||||
_ = driver.TearDown()
|
||||
}
|
||||
}
|
||||
|
||||
deviceObj, exists := c.Get("device")
|
||||
if exists {
|
||||
if device, ok := deviceObj.(*uixt.IOSDevice); ok {
|
||||
err := device.Teardown()
|
||||
if err != nil {
|
||||
log.Error().Err(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理请求后获取结束时间
|
||||
endTime := time.Now()
|
||||
latency := endTime.Sub(startTime)
|
||||
|
||||
// 获取请求的状态码、客户端IP等信息
|
||||
statusCode := c.Writer.Status()
|
||||
|
||||
// 结束处理后打印日志
|
||||
fmt.Printf("[GIN] %s | %d | %v | %s | %-7s \"%s\"\n",
|
||||
endTime.Format("2006/01/02 - 15:04:05"),
|
||||
statusCode,
|
||||
latency,
|
||||
logID,
|
||||
c.Request.Method,
|
||||
c.Request.URL.Path,
|
||||
)
|
||||
c.Writer.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user