[agent-go] 开始使用GO重构Agent部分

This commit is contained in:
zeaslity
2023-03-20 14:51:38 +08:00
parent 41de603234
commit 4961a4ddc7
13 changed files with 197 additions and 159 deletions

View File

@@ -1,33 +0,0 @@
package entity
import (
"gorm.io/gorm"
)
type ServerInfo struct {
gorm.Model
ServerName string `grom:type`
TopicName string
ServerIPPublicV4 string
ServerIPInnerV4 string
ServerIPPublicV6 string
ServerIPInnerV6 string
ProxyType uint
Location string
Provider string
ManagePort string
CpuBrand string
CpuCore string
MemoryTotal string
DiskTotal string
DiskUsage string
IoSpeed string
TcpControl string
Virtualization string
OsInfo string
OsKernelInfo string
MachineId string
Comment string
Version uint
}

View File

@@ -1,11 +0,0 @@
package entity
import (
"gorm.io/gorm"
)
type GloablConfig struct {
DB *gorm.DB
}
var CONGIG = new(GloablConfig)

View File

@@ -1,38 +1,3 @@
module agent-go
go 1.18
require (
github.com/gin-gonic/gin v1.9.0
github.com/go-playground/validator/v10 v10.11.2
gorm.io/driver/mysql v1.4.7
gorm.io/gorm v1.24.6
)
require (
github.com/bytedance/sonic v1.8.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.9 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

View File

@@ -1,93 +0,0 @@
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.8.0 h1:ea0Xadu+sHlu7x5O3gKhRpQ1IKiMrSiHttPF0ybECuA=
github.com/bytedance/sonic v1.8.0/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.9.0 h1:OjyFBKICoexlu99ctXNR2gg+c5pKrKMuyjgARg9qeY8=
github.com/gin-gonic/gin v1.9.0/go.mod h1:W1Me9+hsUSyj3CePGrd1/QrKJMSJ1Tu/0hFEH89961k=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU=
github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA=
github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.9 h1:rmenucSohSTiyL09Y+l2OCk+FrMxGMzho2+tjr5ticU=
github.com/ugorji/go/codec v1.2.9/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE=
golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.4.7 h1:rY46lkCspzGHn7+IYsNpSfEv9tA+SU4SkkB+GFX125Y=
gorm.io/driver/mysql v1.4.7/go.mod h1:SxzItlnT1cb6e1e4ZRpgJN2VYtcqJgqnHxWr4wsP8oc=
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.24.6 h1:wy98aq9oFEetsc4CAbKD2SoBCdMzsbSIvSUUFJuHi5s=
gorm.io/gorm v1.24.6/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

View File

@@ -1,125 +1,5 @@
package main
import (
"agent-go/router"
"agent-go/service"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
"net/http"
)
func myValidate(fl validator.FieldLevel) bool {
fmt.Println(fl.Field().Interface().(int))
return true
}
func main() {
// init the db connection
service.InitDB()
// all the route
engine := router.Routers()
// Get方法
engine.GET("/ping", func(c *gin.Context) {
statusOK := http.StatusOK
c.JSON(
statusOK,
gin.H{})
})
// Post方法接收request body的形式
engine.POST("/jsonPost", func(c *gin.Context) {
// 自定义 请求规则验证器
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
v.RegisterValidation("myValidation", myValidate)
}
var p string
err := c.ShouldBindJSON(&p)
if err != nil {
return
}
})
// 上传文件的操作 前端上传文件使用的是form-data的形式
// 单个文件上传
engine.POST("/upload", func(c *gin.Context) {
file, err := c.FormFile("file") // 获取上传的文件
if err != nil {
c.String(400, fmt.Sprintf("get form err: %s", err.Error()))
return
}
// 如果有其他的表单信息
c.DefaultPostForm("name", "default-file-name")
// 打印文件信息
fmt.Printf("File Name: %s\n", file.Filename)
fmt.Printf("File Size: %d bytes\n", file.Size)
fmt.Printf("MIME Type: %s\n", file.Header.Get("Content-Type"))
// 保存文件到本地
err = c.SaveUploadedFile(file, "./"+file.Filename)
if err != nil {
c.String(500, fmt.Sprintf("upload file err: %s", err.Error()))
return
}
c.String(200, fmt.Sprintf("%s uploaded successfully!", file.Filename))
})
// 多个文件上传
engine.POST("/multi-upload", func(c *gin.Context) {
form, err := c.MultipartForm() // 获取多个文件上传表单
if err != nil {
c.String(400, fmt.Sprintf("get form err: %s", err.Error()))
return
}
// 遍历所有上传的文件
files := form.File["files"]
for _, file := range files {
// 打印文件信息
fmt.Printf("File Name: %s\n", file.Filename)
fmt.Printf("File Size: %d bytes\n", file.Size)
fmt.Printf("MIME Type: %s\n", file.Header.Get("Content-Type"))
// 保存文件到本地
err = c.SaveUploadedFile(file, "./"+file.Filename)
if err != nil {
c.String(500, fmt.Sprintf("upload file err: %s", err.Error()))
return
}
}
c.String(200, fmt.Sprintf("%d files uploaded successfully!", len(files)))
})
// 返回静态文件
engine.GET("/file", func(c *gin.Context) {
c.File("./example.txt") // 返回指定路径的文件
})
// 返回动态生成的文件
engine.GET("/download", func(c *gin.Context) {
content := "Hello, world!"
fileName := "example.txt"
// 设置响应头,使浏览器可以下载文件
c.Header("Content-Disposition", "attachment; filename="+fileName)
c.Header("Content-Type", "application/octet-stream")
c.Header("Content-Transfer-Encoding", "binary")
c.Header("Cache-Control", "no-cache")
// 将内容写入响应体中
c.String(http.StatusOK, content)
})
engine.Run(":9090")
}

31
agent-go/readme.md Normal file
View File

@@ -0,0 +1,31 @@
使用GO语言重构Octopus-Agent项目
---
## Message
1. Agent注册与离线机制
2. 从环境变量中获取信息
## Executor
1. 利用协程池
1. 实现任务超时控制-可控,可不控
2. 任务异步非阻塞式执行
2. 执行Shell命令
1. 简单的命令 -- 单行命令
2. 管道命令 -- 管道式命令-复杂单行命令
3. 完整体命令 -- 多行命令
4. shell脚本 -- 执行shell脚本
3. 执行结果上报
1. 执行日志合并收集,统一上报
2. 执行结果上报
## Status
1. 版本信息汇报
2. 收集Agent自身的服务器信息
3. 上报Agent的版本信息
##

View File

@@ -1,29 +0,0 @@
package router
import (
"agent-go/service"
"github.com/gin-gonic/gin"
)
type ServerRouter struct {
}
var ServerRouterAPP = new(ServerRouter)
func (s *ServerRouter) InitServerRouter(Router *gin.RouterGroup) {
serverRouter := Router.Group("/server")
serverAPIService := service.ServerAPIService
{
serverRouter.GET("/info/fake", serverAPIService.FakeAPI)
// 服务器 CRUD接口及方法
serverRouter.GET("/info/all", serverAPIService.QueryAPI)
serverRouter.POST("/create/one", serverAPIService.CreateAPI)
serverRouter.POST("/update/one", serverAPIService.UpdateAPI)
serverRouter.POST("/delete/one", serverAPIService.DeleteAPI)
}
}

View File

@@ -1,18 +0,0 @@
package router
import (
_ "agent-go/service"
"github.com/gin-gonic/gin"
)
func Routers() *gin.Engine {
Router := gin.Default()
ServerGroup := Router.Group("/core")
// register server app
ServerRouterAPP.InitServerRouter(ServerGroup)
return Router
}

View File

@@ -1,27 +0,0 @@
package service
import (
"agent-go/entity"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func InitDB() *gorm.DB {
// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
dsn := "wmm:Superwmm.23@tcp(42.192.52.227:21306)/gormdb?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic(err)
}
// init the table
db.AutoMigrate(&entity.ServerInfo{})
// cache this db to global
entity.CONGIG.DB = db
return db
}

View File

@@ -1,91 +0,0 @@
package service
import (
"agent-go/entity"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
type ServerAPI struct {
}
var ServerAPIService = new(ServerAPI)
var db = entity.CONGIG.DB
func (s *ServerAPI) QueryAPI(c *gin.Context) {
fmt.Println("ServerAPI -> QueryAPI")
}
func (s *ServerAPI) CreateAPI(c *gin.Context) {
var serverInfo entity.ServerInfo
// get the body
err := c.ShouldBindJSON(&serverInfo)
if err != nil {
fmt.Println("server info error! pls check !")
}
fmt.Sprintf("server info is => %s", serverInfo)
create := db.Create(&serverInfo)
//user.ID // 返回插入数据的主键
//result.Error // 返回 error
//result.RowsAffected // 返回插入记录的条数
fmt.Sprintf(
"inter id is %s , resul error is %s, RowsAffected is %d ", serverInfo.ID, create.Error, create.RowsAffected)
}
func (s *ServerAPI) FakeAPI(c *gin.Context) {
c.JSON(
http.StatusOK,
new(entity.ServerInfo))
}
// UpdateAPI 根据前端传递的参数id进行更新
func (s *ServerAPI) UpdateAPI(c *gin.Context) {
var jsonServerInfo entity.ServerInfo
err := c.ShouldBindJSON(&jsonServerInfo)
if err != nil {
return
}
id := jsonServerInfo.ID
// 查询要更新的记录
var dbProduct entity.ServerInfo
if err := db.First(&dbProduct, id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Record not found"})
return
}
// 绑定模型并更新属性
db.Model(&dbProduct).Updates(jsonServerInfo)
// 保存更改并提交事务
if err := db.Save(&dbProduct).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update record"})
return
}
c.JSON(http.StatusOK, dbProduct)
}
// DeleteAPI 跟俊前端传递的id进行删除
func (s *ServerAPI) DeleteAPI(c *gin.Context) {
// 获取前端传递的值
id := c.Param("id")
// 根据ID删除 是否是软删除
db.Delete(&entity.ServerInfo{}, id)
// 软删除
}

View File

@@ -1,37 +0,0 @@
package main
import "fmt"
func sum(start, end uint64, result chan uint64) {
var sum uint64 = 0
for i := start; i <= end; i++ {
sum += i
}
result <- sum
}
func main() {
const n uint64 = 5000000000
var numCPU int = 4
result := make(chan uint64, numCPU)
var chunkSize uint64 = n / uint64(numCPU)
var start uint64 = 1
var end uint64 = chunkSize
for i := 0; i < numCPU; i++ {
go sum(start, end, result)
start = end + 1
end += chunkSize
}
var total uint64 = 0
for i := 0; i < numCPU; i++ {
partialSum := <-result
total += partialSum
}
fmt.Println(total)
}