Event retention (#23)

Signed-off-by: link <a624669980@163.com>
This commit is contained in:
link
2022-11-30 12:16:11 +08:00
committed by GitHub
parent b41ab1ecce
commit dab1fdd6be
15 changed files with 601 additions and 35 deletions
+65
View File
@@ -0,0 +1,65 @@
package route
import (
"encoding/json"
"fmt"
"log"
"strings"
"github.com/IceWhaleTech/CasaOS-Common/external"
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
message_bus "github.com/IceWhaleTech/CasaOS-UserService/codegen/message_bus"
"github.com/IceWhaleTech/CasaOS-UserService/model"
"github.com/IceWhaleTech/CasaOS-UserService/pkg/config"
"github.com/IceWhaleTech/CasaOS-UserService/service"
"go.uber.org/zap"
"golang.org/x/net/websocket"
)
func EventListen() {
messageBusUrl, err := external.GetMessageBusAddress(config.CommonInfo.RuntimePath)
if err != nil {
logger.Error("get message bus url error", zap.Any("err", err))
return
}
wsURL := fmt.Sprintf("ws://%s/event/%s", strings.ReplaceAll(messageBusUrl, "http://", ""), "local-storage")
ws, err := websocket.Dial(wsURL, "", "http://localhost")
if err != nil {
logger.Error("connect websocket err", zap.Any("error", err))
}
defer ws.Close()
log.Println("subscribed to", wsURL)
for {
msg := make([]byte, 1024)
n, err := ws.Read(msg)
if err != nil {
log.Fatalln(err.Error())
}
var event message_bus.Event
if err := json.Unmarshal(msg[:n], &event); err != nil {
log.Println(err.Error())
}
propertiesStr, err := json.Marshal(event.Properties)
if err != nil {
continue
}
model := model.EventModel{
SourceID: event.SourceID,
Name: event.Name,
Properties: string(propertiesStr),
UUID: *event.Uuid,
}
service.MyService.Event().CreateEvemt(model)
output, err := json.MarshalIndent(event, "", " ")
if err != nil {
log.Println(err.Error())
}
log.Println(string(output))
}
}
View File
+105
View File
@@ -0,0 +1,105 @@
package route
import (
"net/http"
"net/url"
"strconv"
"strings"
"github.com/IceWhaleTech/CasaOS-Common/utils/common_err"
"github.com/IceWhaleTech/CasaOS-Common/utils/jwt"
codegen "github.com/IceWhaleTech/CasaOS-UserService/codegen/user-service"
v2 "github.com/IceWhaleTech/CasaOS-UserService/route/v2"
"github.com/deepmap/oapi-codegen/pkg/middleware"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
"github.com/labstack/echo/v4"
echo_middleware "github.com/labstack/echo/v4/middleware"
)
var (
_swagger *openapi3.T
V2APIPath string
V2DocPath string
)
func init() {
swagger, err := codegen.GetSwagger()
if err != nil {
panic(err)
}
_swagger = swagger
u, err := url.Parse(_swagger.Servers[0].URL)
if err != nil {
panic(err)
}
V2APIPath = strings.TrimRight(u.Path, "/")
V2DocPath = "/doc" + V2APIPath
}
func InitV2Router() http.Handler {
UserService := v2.NewUserService()
e := echo.New()
e.Use((echo_middleware.CORSWithConfig(echo_middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{echo.POST, echo.GET, echo.OPTIONS, echo.PUT, echo.DELETE},
AllowHeaders: []string{echo.HeaderAuthorization, echo.HeaderContentLength, echo.HeaderXCSRFToken, echo.HeaderContentType, echo.HeaderAccessControlAllowOrigin, echo.HeaderAccessControlAllowHeaders, echo.HeaderAccessControlAllowMethods, echo.HeaderConnection, echo.HeaderOrigin, echo.HeaderXRequestedWith},
ExposeHeaders: []string{echo.HeaderContentLength, echo.HeaderAccessControlAllowOrigin, echo.HeaderAccessControlAllowHeaders},
MaxAge: 172800,
AllowCredentials: true,
})))
e.Use(echo_middleware.Gzip())
e.Use(echo_middleware.Logger())
e.Use(echo_middleware.JWTWithConfig(echo_middleware.JWTConfig{
Skipper: func(c echo.Context) bool {
return c.RealIP() == "::1" || c.RealIP() == "127.0.0.1"
},
ParseTokenFunc: func(token string, c echo.Context) (interface{}, error) {
claims, code := jwt.Validate(token)
if code != common_err.SUCCESS {
return nil, echo.ErrUnauthorized
}
c.Request().Header.Set("user_id", strconv.Itoa(claims.ID))
return claims, nil
},
TokenLookupFuncs: []echo_middleware.ValuesExtractor{
func(c echo.Context) ([]string, error) {
return []string{c.Request().Header.Get(echo.HeaderAuthorization)}, nil
},
},
}))
e.Use(middleware.OapiRequestValidatorWithOptions(_swagger, &middleware.Options{Options: openapi3filter.Options{AuthenticationFunc: openapi3filter.NoopAuthenticationFunc}}))
codegen.RegisterHandlersWithBaseURL(e, UserService, V2APIPath)
return e
}
func InitV2DocRouter(docHTML string, docYAML string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == V2DocPath {
if _, err := w.Write([]byte(docHTML)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
return
}
if r.URL.Path == V2DocPath+"/openapi.yaml" {
if _, err := w.Write([]byte(docYAML)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
})
}
+20
View File
@@ -0,0 +1,20 @@
package v2
import (
"net/http"
codegen "github.com/IceWhaleTech/CasaOS-UserService/codegen/user-service"
"github.com/IceWhaleTech/CasaOS-UserService/service"
"github.com/labstack/echo/v4"
)
func (s *UserService) DeleteEvent(ctx echo.Context, params codegen.EventUuid) error {
m := service.MyService.Event().GetEventByUUID(params.String())
service.MyService.Event().DeleteEvent(params.String())
return ctx.JSON(http.StatusOK, m)
}
func (s *UserService) GetEvents(ctx echo.Context, params codegen.GetEventsParams) error {
list := service.MyService.Event().GetEvents()
return ctx.JSON(http.StatusOK, list)
}
+9
View File
@@ -0,0 +1,9 @@
package v2
import codegen "github.com/IceWhaleTech/CasaOS-UserService/codegen/user-service"
type UserService struct{}
func NewUserService() codegen.ServerInterface {
return &UserService{}
}