-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookie_persistence_store.go
More file actions
141 lines (115 loc) · 3.19 KB
/
cookie_persistence_store.go
File metadata and controls
141 lines (115 loc) · 3.19 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package swole
import (
"encoding/json"
"errors"
"net/http"
)
const cookieName = "swole"
type CookiePersistenceStore struct {
MaxAge int
}
func NewCookiePersistenceStore() *CookiePersistenceStore {
return &CookiePersistenceStore{
MaxAge: 60 * 60 * 24, // one day,
}
}
// generateCookie creates a cookie based on a value
func (s *CookiePersistenceStore) generateCookie(value string) http.Cookie {
return http.Cookie{
Name: cookieName,
Value: value,
Path: "/",
MaxAge: s.MaxAge,
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
}
}
func (s *CookiePersistenceStore) writeCookie(w http.ResponseWriter, value string) error {
cookie := s.generateCookie(value)
return writeCookie(w, cookie)
}
func (s *CookiePersistenceStore) ExperimentExists(key string, w http.ResponseWriter, r *http.Request) (bool, string, error) {
cookie, err := readCookie(r, cookieName)
// we didn't find cookie therefore experiment does not exist
if errors.Is(err, http.ErrNoCookie) {
return false, "", nil
}
if err != nil {
return false, "", err
}
// we need to check cookie to see if our experiment is in there
// {"experiment_name": "control", "experiment_name:finished": "true"}
var parsedCookieValue map[string]string
err = json.Unmarshal([]byte(cookie.Value), &parsedCookieValue)
if err != nil {
return false, "", err
}
alternative, found := parsedCookieValue[key]
if found {
return true, alternative, nil
}
return false, "", nil
}
func (s *CookiePersistenceStore) PersistExperiment(key, alternative string, w http.ResponseWriter, r *http.Request) (err error) {
cookieExists := true
cookie, err := readCookie(r, cookieName)
// we didn't find cookie therefore experiment does not exist
if errors.Is(err, http.ErrNoCookie) {
cookieExists = false
} else if err != nil {
return err
}
parsedCookieValue := make(map[string]string)
if cookieExists {
err = json.Unmarshal([]byte(cookie.Value), &parsedCookieValue)
if err != nil {
return err
}
}
parsedCookieValue[key] = alternative
newValue, err := json.Marshal(&parsedCookieValue)
if err != nil {
return err
}
err = s.writeCookie(w, string(newValue))
if err != nil {
return nil
}
return nil
}
func (s *CookiePersistenceStore) RefreshTtl(w http.ResponseWriter, r *http.Request) error {
cookie, err := readCookie(r, cookieName)
if err != nil {
return err
}
err = s.writeCookie(w, cookie.Value)
if err != nil {
return err
}
return nil
}
func (s *CookiePersistenceStore) ExperimentFinish(key string, w http.ResponseWriter, r *http.Request) (finishFirstTime bool, err error) {
cookie, err := readCookie(r, cookieName)
if err != nil {
return false, err
}
var parsedCookieValue map[string]string
err = json.Unmarshal([]byte(cookie.Value), &parsedCookieValue)
if err != nil {
return false, err
}
finishedKey := key + ":finished"
_, found := parsedCookieValue[finishedKey]
parsedCookieValue[finishedKey] = "true"
newValue, err := json.Marshal(&parsedCookieValue)
if err != nil {
return false, err
}
err = s.writeCookie(w, string(newValue))
if err != nil {
return false, err
}
// if the finished key was not found this means it is the first time we're finishing it
return !found, nil
}