generated from fluffy-bunny/fluffycore-grpc-starterkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
62 lines (57 loc) · 1.43 KB
/
base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package cookies
import (
"encoding/json"
"time"
contracts_config "github.com/fluffy-bunny/fluffycore-rage-identity/pkg/contracts/config"
fluffycore_contracts_cookies "github.com/fluffy-bunny/fluffycore/echo/contracts/cookies"
status "github.com/gogo/status"
echo "github.com/labstack/echo/v4"
codes "google.golang.org/grpc/codes"
)
type (
CustomCookieBase struct {
fluffycore_contracts_cookies.ICookies
}
)
func GetCookie[T any](c echo.Context,
cookies fluffycore_contracts_cookies.ICookies, name string, data *T) error {
getCookieResponse, err := cookies.GetCookie(c, name)
if err != nil {
return err
}
if getCookieResponse.Value == nil {
return status.Errorf(codes.NotFound, "%s not found", name)
}
bb, err := json.Marshal(getCookieResponse.Value)
if err != nil {
return err
}
err = json.Unmarshal(bb, data)
return err
}
func SetCookie[T any](c echo.Context,
config *contracts_config.CookieConfig,
cookies fluffycore_contracts_cookies.ICookies, name string, data T) error {
b, err := json.Marshal(data)
if err != nil {
return err
}
value := make(map[string]interface{})
err = json.Unmarshal(b, &value)
if err != nil {
return err
}
_, err = cookies.SetCookie(c,
&fluffycore_contracts_cookies.SetCookieRequest{
Name: name,
Value: value,
HttpOnly: false,
Expires: time.Now().Add(30 * time.Minute),
Path: "/",
Domain: config.Domain,
})
if err != nil {
return err
}
return nil
}