You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.5 KiB
Go

package main
import (
"flag"
"fmt"
"goqs/controllers"
"goqs/dbsql"
"goqs/device"
"goqs/env"
"goqs/monkey"
"net/http"
_ "goqs/docs"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
var (
ENV string
)
func init() {
flag.StringVar(&ENV, "env", "test", "测试环境")
}
func main() {
env.InitDB()
log.SetLevel(log.DebugLevel)
log.SetReportCaller(true)
flag.Parse()
if ENV == "test" {
log.Info("[MODE] dev-test config")
fmt.Println("")
dbsql.DSN = dbsql.DSN_local
} else if ENV == "prod" {
log.Info("[MODE] Prod config")
fmt.Println("")
dbsql.DSN = dbsql.DSN_local
} else {
log.Error("param.env error:", ENV)
}
port := ":6364"
// 1.创建路由
r := gin.Default()
r.Use(Cors())
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "hello World!")
})
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
r.GET("/test", controllers.Test)
// monkey
r.POST("/api/monkey/v1/create_task", monkey.CreateTask)
r.POST("/api/monkey/v1/task/status", monkey.UpdateTaskStatus)
r.POST("/api/monkey/v1/task/result", monkey.UploadMonkeyResult)
r.POST("/api/monkey/v1/task/stop", monkey.StopMonkeyTask)
r.POST("/api/monkey/v1/task/pid", monkey.UpdatePids)
r.POST("/api/monkey/v1/task/update_devices", monkey.UpdateDevices)
r.GET("/api/monkey/v1/task/result", monkey.GetMonkeyResult)
r.POST("/api/monkey/v1/task/activity", monkey.UpdataActivity)
r.GET("/api/monkey/v1/task/activity", monkey.GetMonkeyActivities)
r.GET("/api/monkey/v1/tasks", monkey.GetTasks)
r.GET("/api/monkey/v1/task", monkey.GetTaskById)
r.GET("/api/monkey/v1/task/devices", monkey.GetDevicesByTaskId)
r.GET("/api/monkey/v1/devices", monkey.GetDevices)
r.POST("/api/monkey/v1/task/crash_count", monkey.UpdateTaskCrashCount)
r.PUT("/webdav/monkey/task/:id/:filename", monkey.TaskReport)
// device
r.GET("/api/device/v1/list", device.GetDevices)
r.GET("/api/device/v1/get_device", device.GetDeviceByUdid)
r.POST("/api/device/v1/create", device.CreateDevice)
r.POST("/api/device/v1/update", device.UpdateDevice)
r.DELETE("/api/device/v1/delete/:id", device.DeleteDevice)
r.Run(port)
}
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method
origin := c.Request.Header.Get("Origin") //请求头部
if origin != "" {
//接收客户端发送的origin (重要!)
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
//服务器支持的所有跨域请求的方法
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE")
//允许跨域设置可以返回其他子段,可以自定义字段
c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session, Origin, X-Requested-With, Content-Type, Accept")
//允许浏览器(客户端)可以解析的头部 (重要)
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
//设置缓存时间
c.Header("Access-Control-Max-Age", "172800")
//允许客户端传递校验信息比如 cookie (重要)
c.Header("Access-Control-Allow-Credentials", "true")
}
//允许类型校验
if method == "OPTIONS" {
c.JSON(http.StatusOK, "ok!")
}
defer func() {
if err := recover(); err != nil {
log.Error("Panic info is: %v", err)
}
}()
c.Next()
}
}