From 524a63d7af46b329bed79f401365c9692438ca8e Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 17 Jan 2024 21:56:39 +0800 Subject: [PATCH] Refactor code to add context object and cookie function --- sui/api/guards.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/sui/api/guards.go b/sui/api/guards.go index d05324a0..c58bbe3a 100644 --- a/sui/api/guards.go +++ b/sui/api/guards.go @@ -164,6 +164,8 @@ func (r *Request) scriptGuardExec(c *gin.Context, name string, args []interface{ } defer ctx.Close() + // Should be refector after the runtime refector + // Add the context object ctx.WithFunction("SetSid", func(info *v8go.FunctionCallbackInfo) *v8go.Value { if len(info.Args()) < 1 { log.Error("SetSid no sid") @@ -242,6 +244,37 @@ func (r *Request) scriptGuardExec(c *gin.Context, name string, args []interface{ return nil }) + ctx.WithFunction("Cookie", func(info *v8go.FunctionCallbackInfo) *v8go.Value { + if len(info.Args()) < 1 { + log.Error("SetGlobal no global") + return v8go.Undefined(info.Context().Isolate()) + } + + name, err := bridge.GoValue(info.Args()[0], info.Context()) + if err != nil { + log.Error("Cookie %s", err.Error()) + return v8go.Undefined(info.Context().Isolate()) + } + + if name, ok := name.(string); ok { + value, err := c.Cookie(name) + if err != nil { + log.Error("Cookie %s", err.Error()) + return v8go.Undefined(info.Context().Isolate()) + } + + jsValue, err := bridge.JsValue(info.Context(), value) + if err != nil { + log.Error("Cookie %s", err.Error()) + return v8go.Undefined(info.Context().Isolate()) + } + return jsValue + } + + return v8go.Undefined(info.Context().Isolate()) + + }) + // This function should be refector after the next version ctx.WithFunction("SetCookie", func(info *v8go.FunctionCallbackInfo) *v8go.Value {