diff --git a/api/message_bus/openapi.yaml b/api/message_bus/openapi.yaml index 724ad47..5061b46 100644 --- a/api/message_bus/openapi.yaml +++ b/api/message_bus/openapi.yaml @@ -514,6 +514,10 @@ components: type: string description: event name example: "local-storage:disk:added" + uuid: + type: string + description: event uuid + example: "442e0e5b-9d3e-4fe8-b46f-9c4141fdecd7" properties: type: object description: event properties diff --git a/go.mod b/go.mod index 9517d4d..d75814b 100644 --- a/go.mod +++ b/go.mod @@ -57,7 +57,7 @@ require ( github.com/getkin/kin-openapi v0.107.0 github.com/go-openapi/jsonpointer v0.19.5 // indirect github.com/go-openapi/swag v0.21.1 // indirect - github.com/google/uuid v1.3.0 // indirect + github.com/google/uuid v1.3.0 github.com/invopop/yaml v0.1.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/labstack/echo/v4 v4.9.1 diff --git a/model/structs.go b/model/structs.go index 6979f7a..0f94685 100644 --- a/model/structs.go +++ b/model/structs.go @@ -18,6 +18,7 @@ type Event struct { Name string `gorm:"index"` Properties map[string]string `gorm:"foreignKey:Id"` Timestamp int64 `gorm:"autoCreateTime:milli"` + Uuid string `json:"uuid,omitempty"` } type ActionType struct { diff --git a/route/adapter/in/event.go b/route/adapter/in/event.go index 764b2a7..97457f0 100644 --- a/route/adapter/in/event.go +++ b/route/adapter/in/event.go @@ -20,6 +20,8 @@ func EventAdapter(event codegen.Event) model.Event { SourceID: event.SourceID, Name: event.Name, Properties: event.Properties, + Uuid: *event.Uuid, + Timestamp: timestamp, } } diff --git a/route/adapter/out/event.go b/route/adapter/out/event.go index f519e5b..f715748 100644 --- a/route/adapter/out/event.go +++ b/route/adapter/out/event.go @@ -19,5 +19,6 @@ func EventAdapter(event model.Event) codegen.Event { Name: event.Name, Properties: event.Properties, Timestamp: utils.Ptr(time.Unix(event.Timestamp, 0)), + Uuid: &event.Uuid, } } diff --git a/route/api_route_event.go b/route/api_route_event.go index 2a8a00b..5b11309 100644 --- a/route/api_route_event.go +++ b/route/api_route_event.go @@ -16,6 +16,7 @@ import ( "github.com/IceWhaleTech/CasaOS-MessageBus/route/adapter/out" "github.com/gobwas/ws" "github.com/gobwas/ws/wsutil" + "github.com/google/uuid" "github.com/labstack/echo/v4" "go.uber.org/zap" ) @@ -87,10 +88,9 @@ func (r *APIRoute) PublishEvent(ctx echo.Context, sourceID codegen.SourceID, nam return ctx.JSON(http.StatusNotFound, codegen.ResponseNotFound{Message: utils.Ptr("not found")}) } - var properties map[string]string - body, err := ioutil.ReadAll(ctx.Request().Body) + properties := make(map[string]string) - if err != nil { + if err := ctx.Bind(&properties); err != nil { message := err.Error() return ctx.JSON(http.StatusBadRequest, codegen.ResponseBadRequest{Message: &message}) @@ -101,12 +101,13 @@ func (r *APIRoute) PublishEvent(ctx echo.Context, sourceID codegen.SourceID, nam return ctx.JSON(http.StatusBadRequest, codegen.ResponseBadRequest{Message: &message}) } } - + uuidStr := uuid.New().String() event := codegen.Event{ SourceID: sourceID, Name: name, Properties: properties, Timestamp: utils.Ptr(time.Now()), + Uuid: &uuidStr, } result, err := r.services.EventService.Publish(in.EventAdapter(event))