补充websocket相关代码
This commit is contained in:
parent
f13c890358
commit
222740609d
@ -44,7 +44,7 @@ func (s *ResourceServiceImpl) Save(ctx context.Context, in *resource.Resource) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 保持数据, 需要从ioc里面获取一个mongodb实例
|
// 保持数据, 需要从ioc里面获取一个mongodb实例
|
||||||
_, err := s.col.InsertOne(ctx, in)
|
_, err := s.col.UpdateOne(ctx, bson.M{"_id": in.Meta.Id}, bson.M{"$set": in}, options.Update().SetUpsert(true))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -25,3 +25,12 @@ func TestSave(t *testing.T) {
|
|||||||
}
|
}
|
||||||
t.Log(resp)
|
t.Log(resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSearch(t *testing.T) {
|
||||||
|
req := resource.NewSearchRequest()
|
||||||
|
resp, err := svc.Search(ctx, req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(resp)
|
||||||
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
restfulspec "github.com/emicklei/go-restful-openapi/v2"
|
restfulspec "github.com/emicklei/go-restful-openapi/v2"
|
||||||
"github.com/emicklei/go-restful/v3"
|
"github.com/emicklei/go-restful/v3"
|
||||||
"github.com/gin-gonic/gin/binding"
|
"github.com/gin-gonic/gin/binding"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
"github.com/infraboard/mcube/v2/exception"
|
"github.com/infraboard/mcube/v2/exception"
|
||||||
"github.com/infraboard/mcube/v2/http/restful/response"
|
"github.com/infraboard/mcube/v2/http/restful/response"
|
||||||
"github.com/infraboard/mcube/v2/ioc"
|
"github.com/infraboard/mcube/v2/ioc"
|
||||||
@ -46,6 +47,15 @@ func (r *SecretApiHandler) Init() error {
|
|||||||
Writes(secret.Secret{}).
|
Writes(secret.Secret{}).
|
||||||
Returns(200, "OK", secret.Secret{}).
|
Returns(200, "OK", secret.Secret{}).
|
||||||
Returns(404, "Not Found", exception.NewNotFound("")))
|
Returns(404, "Not Found", exception.NewNotFound("")))
|
||||||
|
|
||||||
|
// :id -> {id}
|
||||||
|
// websocket api
|
||||||
|
ws.Route(ws.GET("/{id}/sync").To(r.SyncResource).Doc("资源同步").
|
||||||
|
Param(ws.PathParameter("id", "凭证Id").DataType("string")).
|
||||||
|
Metadata(restfulspec.KeyOpenAPITags, tags).
|
||||||
|
Writes(secret.ResourceResponse{}).
|
||||||
|
Returns(200, "OK", secret.ResourceResponse{}).
|
||||||
|
Returns(404, "Not Found", exception.NewNotFound("")))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,3 +96,30 @@ func (r *SecretApiHandler) DescribeSecret(req *restful.Request, resp *restful.Re
|
|||||||
|
|
||||||
response.Success(resp, ins)
|
response.Success(resp, ins)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var upgrader = websocket.Upgrader{} // use default options
|
||||||
|
|
||||||
|
// websocket api
|
||||||
|
// http 协议(短链接)--> websocket(长链接) conn.write()
|
||||||
|
// websocket: https://github.com/gorilla/websocket/tree/main/examples/chat
|
||||||
|
// Go Websocket Client: https://github.com/gorilla/websocket/blob/main/examples/echo/client.go
|
||||||
|
// Web Browser Websocket Client: https://github.com/gorilla/websocket/blob/main/examples/chat/home.html
|
||||||
|
func (r *SecretApiHandler) SyncResource(req *restful.Request, resp *restful.Response) {
|
||||||
|
sr := secret.NewSyncResourceRequest(req.PathParameter("id"))
|
||||||
|
|
||||||
|
// websocket upgrade
|
||||||
|
conn, err := upgrader.Upgrade(resp, req.Request, nil)
|
||||||
|
if err != nil {
|
||||||
|
response.Failed(resp, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 业务逻辑
|
||||||
|
err = secret.GetService().SyncResource(req.Request.Context(), sr, func(rr secret.ResourceResponse) {
|
||||||
|
conn.WriteJSON(rr)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
response.Failed(resp, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -2,9 +2,11 @@ package impl
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/infraboard/mcube/v2/ioc/config/cache"
|
"github.com/infraboard/mcube/v2/ioc/config/cache"
|
||||||
"github.com/infraboard/mcube/v2/types"
|
"github.com/infraboard/mcube/v2/types"
|
||||||
|
"gitlab.com/go-course-project/go17/devcloud-mini/cmdb/apps/resource"
|
||||||
"gitlab.com/go-course-project/go17/devcloud-mini/cmdb/apps/secret"
|
"gitlab.com/go-course-project/go17/devcloud-mini/cmdb/apps/secret"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
@ -93,10 +95,27 @@ func (s *SecretServiceImpl) QuerySecret(ctx context.Context, in *secret.QuerySec
|
|||||||
|
|
||||||
// SyncResource implements secret.Service.
|
// SyncResource implements secret.Service.
|
||||||
func (s *SecretServiceImpl) SyncResource(ctx context.Context, in *secret.SyncResourceRequest, cb secret.SyncResourceHandleFunc) error {
|
func (s *SecretServiceImpl) SyncResource(ctx context.Context, in *secret.SyncResourceRequest, cb secret.SyncResourceHandleFunc) error {
|
||||||
secret, err := s.DescribeSecret(ctx, secret.NewDescribeSecretRequeset(in.Id))
|
ins, err := s.DescribeSecret(ctx, secret.NewDescribeSecretRequeset(in.Id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return secret.Sync(cb)
|
return ins.Sync(func(rr secret.ResourceResponse) {
|
||||||
|
// 进行必要数据的填充
|
||||||
|
rr.Resource.Meta.SyncAt = time.Now().Unix()
|
||||||
|
// 资源归属
|
||||||
|
rr.Resource.Meta.Domain = "default"
|
||||||
|
rr.Resource.Meta.Namespace = "default"
|
||||||
|
|
||||||
|
// 调用resource模块来进行 资源的保存
|
||||||
|
res, err := resource.GetService().Save(ctx, rr.Resource)
|
||||||
|
if err != nil {
|
||||||
|
rr.Success = false
|
||||||
|
rr.Message = err.Error()
|
||||||
|
} else {
|
||||||
|
rr.Success = true
|
||||||
|
rr.Resource = res
|
||||||
|
}
|
||||||
|
cb(rr)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package impl_test
|
package impl_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitlab.com/go-course-project/go17/devcloud-mini/cmdb/apps/resource"
|
"gitlab.com/go-course-project/go17/devcloud-mini/cmdb/apps/resource"
|
||||||
@ -38,8 +37,14 @@ func TestDescribeSecret(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
t.Log(ins)
|
t.Log(ins)
|
||||||
|
}
|
||||||
ins.Sync(func(rr secret.ResourceResponse) {
|
|
||||||
fmt.Println(rr)
|
func TestSyncResource(t *testing.T) {
|
||||||
})
|
req := secret.NewSyncResourceRequest("0f6836e0-a894-3f87-b031-216478a8093b")
|
||||||
|
err := svc.SyncResource(ctx, req, func(rr secret.ResourceResponse) {
|
||||||
|
t.Log(rr)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ type Service interface {
|
|||||||
// Req ---> <---- Resp: 能快速响应的同步调用
|
// Req ---> <---- Resp: 能快速响应的同步调用
|
||||||
// SyncResource(Req) Resp
|
// SyncResource(Req) Resp
|
||||||
|
|
||||||
// Stream API
|
// Stream API, websocket --> UI 当前资源同步的进度
|
||||||
SyncResource(context.Context, *SyncResourceRequest, SyncResourceHandleFunc) error
|
SyncResource(context.Context, *SyncResourceRequest, SyncResourceHandleFunc) error
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,8 +73,10 @@ type DescribeSecretRequeset struct {
|
|||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSyncResourceRequest() *SyncResourceRequest {
|
func NewSyncResourceRequest(id string) *SyncResourceRequest {
|
||||||
return &SyncResourceRequest{}
|
return &SyncResourceRequest{
|
||||||
|
Id: id,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type SyncResourceRequest struct {
|
type SyncResourceRequest struct {
|
||||||
|
@ -63,6 +63,7 @@ func (s *Secret) Sync(cb SyncResourceHandleFunc) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 云商数据结构lighthouse.Instance --> Resource
|
||||||
func TransferLighthouseToResource(ins *lighthouse.Instance) *resource.Resource {
|
func TransferLighthouseToResource(ins *lighthouse.Instance) *resource.Resource {
|
||||||
res := resource.NewResource()
|
res := resource.NewResource()
|
||||||
// 具体的转化逻辑
|
// 具体的转化逻辑
|
||||||
@ -72,7 +73,6 @@ func TransferLighthouseToResource(ins *lighthouse.Instance) *resource.Resource {
|
|||||||
res.Spec.Memory = GetValue(ins.Memory)
|
res.Spec.Memory = GetValue(ins.Memory)
|
||||||
res.Spec.Storage = GetValue(ins.SystemDisk.DiskSize)
|
res.Spec.Storage = GetValue(ins.SystemDisk.DiskSize)
|
||||||
res.Status.PrivateAddress = common.StringValues(ins.PrivateAddresses)
|
res.Status.PrivateAddress = common.StringValues(ins.PrivateAddresses)
|
||||||
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1
go.mod
1
go.mod
@ -8,6 +8,7 @@ require (
|
|||||||
github.com/gin-gonic/gin v1.10.0
|
github.com/gin-gonic/gin v1.10.0
|
||||||
github.com/go-playground/validator/v10 v10.20.0
|
github.com/go-playground/validator/v10 v10.20.0
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
|
github.com/gorilla/websocket v1.5.3
|
||||||
github.com/infraboard/mcube/v2 v2.0.51
|
github.com/infraboard/mcube/v2 v2.0.51
|
||||||
github.com/rs/zerolog v1.32.0
|
github.com/rs/zerolog v1.32.0
|
||||||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1116
|
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.1116
|
||||||
|
2
go.sum
2
go.sum
@ -94,6 +94,8 @@ github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbu
|
|||||||
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
|
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
|
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 h1:Wqo399gCIufwto+VfwCSvsnfGpF/w5E9CNxSwbpD6No=
|
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 h1:Wqo399gCIufwto+VfwCSvsnfGpF/w5E9CNxSwbpD6No=
|
||||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0/go.mod h1:qmOFXW2epJhM0qSnUUYpldc7gVz2KMQwJ/QYCDIa7XU=
|
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0/go.mod h1:qmOFXW2epJhM0qSnUUYpldc7gVz2KMQwJ/QYCDIa7XU=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
|
Loading…
x
Reference in New Issue
Block a user