-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathstore.go
More file actions
55 lines (45 loc) · 2.17 KB
/
store.go
File metadata and controls
55 lines (45 loc) · 2.17 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
package model
import (
"context"
"io"
"net/http"
"github.com/gorilla/mux"
)
type ctxKey string
const ctxKeyStore = ctxKey("model.Store")
//go:generate go run go.uber.org/mock/mockgen -destination mocks/store.go -package mocks . Store
type Store interface {
// DeactivateOverride deactivates the override for the flag, returning the updated version of the override.
// ErrNotFound is returned if there isn't an override for the flag.
DeactivateOverride(ctx context.Context, projectKey, flagKey string) (int, error)
GetDevProjectKeys(ctx context.Context) ([]string, error)
// GetDevProject fetches the project based on the projectKey. If it doesn't exist, ErrNotFound is returned
GetDevProject(ctx context.Context, projectKey string) (*Project, error)
UpdateProject(ctx context.Context, project Project) (bool, error)
DeleteDevProject(ctx context.Context, projectKey string) (bool, error)
// InsertProject inserts the project. If it already exists, ErrAlreadyExists is returned
InsertProject(ctx context.Context, project Project) error
UpsertOverride(ctx context.Context, override Override) (Override, error)
GetOverridesForProject(ctx context.Context, projectKey string) (Overrides, error)
GetAvailableVariationsForProject(ctx context.Context, projectKey string) (map[string][]Variation, error)
// IncrementProjectPayloadVersion atomically increments the payload version for the project and returns the new version.
IncrementProjectPayloadVersion(ctx context.Context, projectKey string) (int, error)
CreateBackup(ctx context.Context) (io.ReadCloser, int64, error)
RestoreBackup(ctx context.Context, stream io.Reader) (string, error)
}
func ContextWithStore(ctx context.Context, store Store) context.Context {
return context.WithValue(ctx, ctxKeyStore, store)
}
func StoreFromContext(ctx context.Context) Store {
return ctx.Value(ctxKeyStore).(Store)
}
func StoreMiddleware(store Store) mux.MiddlewareFunc {
return func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
ctx := request.Context()
ctx = ContextWithStore(ctx, store)
request = request.WithContext(ctx)
handler.ServeHTTP(writer, request)
})
}
}