Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
rudern
物联大师
提交
9d60cf11
物
物联大师
项目概览
rudern
/
物联大师
通知
4
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
物
物联大师
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9d60cf11
编写于
10月 22, 2020
作者:
J
Jason
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
添加接口
上级
ede0658e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
86 addition
and
16 deletion
+86
-16
web/api/curd.go
web/api/curd.go
+67
-7
web/api/router.go
web/api/router.go
+19
-9
未找到文件。
web/api/curd.go
浏览文件 @
9d60cf11
...
...
@@ -9,16 +9,76 @@ import (
type
hook
func
(
value
interface
{})
error
func
createSliceFromType
(
mod
reflect
.
Type
)
interface
{}
{
//datas := reflect.MakeSlice(reflect.SliceOf(mod), 0, 10).Interface()
//解决不可寻址的问题,参考modern-go/reflect2 safe_slice.go
val
:=
reflect
.
MakeSlice
(
reflect
.
SliceOf
(
mod
),
0
,
1
)
ptr
:=
reflect
.
New
(
val
.
Type
())
ptr
.
Elem
()
.
Set
(
val
)
return
ptr
.
Interface
()
}
func
curdApiList
(
mod
reflect
.
Type
)
gin
.
HandlerFunc
{
return
func
(
ctx
*
gin
.
Context
)
{
datas
:=
createSliceFromType
(
mod
)
var
body
paramSearch
err
:=
ctx
.
ShouldBind
(
&
body
)
if
err
!=
nil
{
replyError
(
ctx
,
err
)
return
}
op
:=
db
.
Engine
.
Limit
(
body
.
Length
,
body
.
Offset
)
for
_
,
filter
:=
range
body
.
Filters
{
if
len
(
filter
.
Values
)
>
0
{
if
len
(
filter
.
Values
)
==
1
{
op
.
And
(
filter
.
Key
+
"=?"
,
filter
.
Values
[
0
])
}
else
{
op
.
In
(
filter
.
Key
,
filter
.
Values
)
}
}
}
if
body
.
Keyword
!=
""
{
kw
:=
"%"
+
body
.
Keyword
+
"%"
op
.
And
(
"user like ? or text like ? or file like ?"
,
kw
,
kw
,
kw
)
}
if
body
.
SortKey
!=
""
{
if
body
.
SortOrder
==
"desc"
{
op
.
Desc
(
body
.
SortKey
)
}
else
{
op
.
Asc
(
body
.
SortKey
)
}
}
else
{
op
.
Desc
(
"id"
)
}
cnt
,
err
:=
op
.
FindAndCount
(
datas
)
if
err
!=
nil
{
replyError
(
ctx
,
err
)
return
}
//replyOk(ctx, cs)
ctx
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"ok"
:
true
,
"data"
:
datas
,
"total"
:
cnt
,
})
}
}
//datas := reflect.MakeSlice(reflect.SliceOf(mod), 0, 10).Interface()
func
curdApiListById
(
mod
reflect
.
Type
,
field
string
)
gin
.
HandlerFunc
{
return
func
(
ctx
*
gin
.
Context
)
{
datas
:=
createSliceFromType
(
mod
)
//解决不可寻址的问题,参考modern-go/reflect2 safe_slice.go
val
:=
reflect
.
MakeSlice
(
reflect
.
SliceOf
(
mod
),
0
,
1
)
ptr
:=
reflect
.
New
(
val
.
Type
()
)
ptr
.
Elem
()
.
Set
(
val
)
datas
:=
ptr
.
Interface
()
var
pid
paramId
if
err
:=
ctx
.
BindUri
(
&
pid
);
err
!=
nil
{
replyError
(
ctx
,
err
)
return
}
var
body
paramSearch
err
:=
ctx
.
ShouldBind
(
&
body
)
...
...
@@ -27,7 +87,7 @@ func curdApiList(mod reflect.Type) gin.HandlerFunc {
return
}
op
:=
db
.
Engine
.
Limit
(
body
.
Length
,
body
.
Offset
)
op
:=
db
.
Engine
.
Where
(
field
+
"=?"
,
pid
.
Id
)
.
Limit
(
body
.
Length
,
body
.
Offset
)
for
_
,
filter
:=
range
body
.
Filters
{
if
len
(
filter
.
Values
)
>
0
{
...
...
web/api/router.go
浏览文件 @
9d60cf11
...
...
@@ -10,8 +10,8 @@ import (
)
type
paramFilter
struct
{
Key
string
`form:"key"`
Values
[]
string
`form:"value"`
Key
string
`form:"key"`
Values
[]
interface
{}
`form:"value"`
}
type
paramSearch
struct
{
...
...
@@ -63,6 +63,7 @@ func RegisterRoutes(app *gin.RouterGroup) {
"register_enable"
,
"register_regex"
,
"register_min"
,
"register_max"
,
"heart_beat_enable"
,
"heart_beat_interval"
,
"heart_beat_content"
,
"heart_beat_is_hex"
,
"disabled"
}
app
.
POST
(
"/project/:id/tunnels"
,
curdApiListById
(
mod
,
"project_id"
))
app
.
POST
(
"/tunnels"
,
curdApiList
(
mod
))
app
.
POST
(
"/tunnel"
,
curdApiCreate
(
mod
,
nil
))
//TODO 启动
app
.
DELETE
(
"/tunnel/:id"
,
curdApiDelete
(
mod
,
nil
))
//TODO 停止
...
...
@@ -77,6 +78,7 @@ func RegisterRoutes(app *gin.RouterGroup) {
//连接管理
mod
=
reflect
.
TypeOf
(
models
.
Link
{})
fields
=
[]
string
{
"name"
}
app
.
POST
(
"/tunnel/:id/links"
,
curdApiListById
(
mod
,
"tunnel_id"
))
app
.
POST
(
"/links"
,
curdApiList
(
mod
))
app
.
DELETE
(
"/link/:id"
,
curdApiDelete
(
mod
,
nil
))
//TODO 停止
app
.
PUT
(
"/link/:id"
,
curdApiModify
(
mod
,
fields
,
nil
))
...
...
@@ -84,6 +86,7 @@ func RegisterRoutes(app *gin.RouterGroup) {
mod
=
reflect
.
TypeOf
(
models
.
Device
{})
fields
=
[]
string
{
"name"
}
app
.
POST
(
"/project/:id/devices"
,
curdApiListById
(
mod
,
"project_id"
))
app
.
POST
(
"/devices"
,
curdApiList
(
mod
))
app
.
POST
(
"/device"
,
curdApiCreate
(
mod
,
nil
))
app
.
DELETE
(
"/device/:id"
,
curdApiDelete
(
mod
,
nil
))
...
...
@@ -92,7 +95,8 @@ func RegisterRoutes(app *gin.RouterGroup) {
mod
=
reflect
.
TypeOf
(
models
.
Location
{})
fields
=
[]
string
{
"name"
}
app
.
POST
(
"/locations"
,
curdApiList
(
mod
))
app
.
POST
(
"/device/:id/locations"
,
curdApiListById
(
mod
,
"device_id"
))
//app.POST("/locations", curdApiList(mod))
//app.POST("/location", curdApiCreate(mod, nil))
app
.
DELETE
(
"/location/:id"
,
curdApiDelete
(
mod
,
nil
))
//app.PUT("/location/:id", curdApiModify(mod, fields, nil))
...
...
@@ -122,13 +126,15 @@ func RegisterRoutes(app *gin.RouterGroup) {
//app.GET("/project/:id/jobs", nop)
//app.GET("/project/:id/strategies", nop)
app
.
POST
(
"/project
/
import"
,
projectImport
)
app
.
POST
(
"/project
-
import"
,
projectImport
)
app
.
GET
(
"/project/:id/export"
,
projectExport
)
app
.
GET
(
"/project/:id/deploy"
,
projectDeploy
)
mod
=
reflect
.
TypeOf
(
models
.
ProjectElement
{})
fields
=
[]
string
{
"name"
}
app
.
POST
(
"/project-elements"
,
curdApiList
(
mod
))
app
.
POST
(
"/project/:id/elements"
,
curdApiListById
(
mod
,
"project_id"
))
//app.POST("/project-elements", curdApiList(mod))
app
.
POST
(
"/project-element"
,
curdApiCreate
(
mod
,
nil
))
app
.
DELETE
(
"/project-element/:id"
,
curdApiDelete
(
mod
,
nil
))
app
.
PUT
(
"/project-element/:id"
,
curdApiModify
(
mod
,
fields
,
nil
))
...
...
@@ -136,7 +142,8 @@ func RegisterRoutes(app *gin.RouterGroup) {
mod
=
reflect
.
TypeOf
(
models
.
ProjectJob
{})
fields
=
[]
string
{
"name"
}
app
.
POST
(
"/project-jobs"
,
curdApiList
(
mod
))
app
.
POST
(
"/project/:id/jobs"
,
curdApiListById
(
mod
,
"project_id"
))
//app.POST("/project-jobs", curdApiList(mod))
app
.
POST
(
"/project-job"
,
curdApiCreate
(
mod
,
nil
))
app
.
DELETE
(
"/project-job/:id"
,
curdApiDelete
(
mod
,
nil
))
app
.
PUT
(
"/project-job/:id"
,
curdApiModify
(
mod
,
fields
,
nil
))
...
...
@@ -144,7 +151,8 @@ func RegisterRoutes(app *gin.RouterGroup) {
mod
=
reflect
.
TypeOf
(
models
.
ProjectStrategy
{})
fields
=
[]
string
{
"name"
}
app
.
POST
(
"/project-strategies"
,
curdApiList
(
mod
))
app
.
POST
(
"/project/:id/strategies"
,
curdApiListById
(
mod
,
"project_id"
))
//app.POST("/project-strategies", curdApiList(mod))
app
.
POST
(
"/project-strategy"
,
curdApiCreate
(
mod
,
nil
))
app
.
DELETE
(
"/project-strategy/:id"
,
curdApiDelete
(
mod
,
nil
))
app
.
PUT
(
"/project-strategy/:id"
,
curdApiModify
(
mod
,
fields
,
nil
))
...
...
@@ -162,7 +170,8 @@ func RegisterRoutes(app *gin.RouterGroup) {
//元件变量
mod
=
reflect
.
TypeOf
(
models
.
ElementVariable
{})
fields
=
[]
string
{
"name"
}
app
.
POST
(
"/element-variables"
,
curdApiList
(
mod
))
app
.
POST
(
"/element/:id/variables"
,
curdApiListById
(
mod
,
"element_id"
))
//app.POST("/element-variables", curdApiList(mod))
app
.
POST
(
"/element-variable"
,
curdApiCreate
(
mod
,
nil
))
app
.
DELETE
(
"/element-variable/:id"
,
curdApiDelete
(
mod
,
nil
))
app
.
PUT
(
"/element-variable/:id"
,
curdApiModify
(
mod
,
fields
,
nil
))
...
...
@@ -171,7 +180,8 @@ func RegisterRoutes(app *gin.RouterGroup) {
//元件批量操作
mod
=
reflect
.
TypeOf
(
models
.
ElementBatch
{})
fields
=
[]
string
{
"name"
}
app
.
POST
(
"/element-batches"
,
curdApiList
(
mod
))
app
.
POST
(
"/element/:id/batches"
,
curdApiListById
(
mod
,
"element_id"
))
//app.POST("/element-batches", curdApiList(mod))
app
.
POST
(
"/element-batch"
,
curdApiCreate
(
mod
,
nil
))
app
.
DELETE
(
"/element-batch/:id"
,
curdApiDelete
(
mod
,
nil
))
app
.
PUT
(
"/element-batch/:id"
,
curdApiModify
(
mod
,
fields
,
nil
))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录