Documentation
¶
Overview ¶
Package gae provides a Google Cloud Datastore session store for scs (alexedwards/scs/v2). It stores session data in Datastore with automatic expiration cleanup.
Usage ¶
client, _ := datastore.NewClient(ctx, projectID) sessionStore := gae.NewGCDSessionStore(client, "", time.Hour) // cleanup every hour session := scs.New() session.Store = sessionStore
Datastore Kind ¶
Sessions are stored with the kind "AuthSession" and the following properties:
- Token: string (used as key name)
- ExpiresAt: time.Time (indexed for cleanup queries)
- Data: []byte (session data, noindex)
Namespacing ¶
The store supports Datastore namespaces for multi-tenant applications:
sessionStore := gae.NewGCDSessionStore(client, "tenant-123", time.Hour)
Package gae provides a Google Cloud Datastore implementation of the UsersService. It is designed for deployment on Google Cloud Platform and supports multi-tenancy through Datastore namespaces.
Datastore Kind ¶
Users are stored with the kind "User" and the following properties:
- id: string (used as key name, not stored as property)
- created_at, updated_at: timestamps
- name, description, email, image_url: strings
- tags: []string (noindex)
- extras: Struct (noindex, for app-specific data)
Namespacing ¶
The service supports Datastore namespaces for multi-tenant applications. Pass a namespace to NewUsersService to isolate data between tenants:
userService := gae.NewUsersService(client, "tenant-123")
Generated Code ¶
This package uses protoc-gen-dal-datastore generated code:
- UserDatastore: Datastore entity model
- UserDatastoreToUser/UserToUserDatastore: conversion functions
- UserDatastoreDAL: data access layer with CRUD operations
Usage ¶
client, _ := datastore.NewClient(ctx, projectID)
userService := gae.NewUsersService(client, "") // default namespace
resp, err := userService.CreateUser(ctx, &v1.CreateUserRequest{
User: &v1.User{Name: "John Doe", Email: "john@example.com"},
})
Index ¶
- type AuthSession
- type GCDSessionStore
- func (s *GCDSessionStore) AllCtx(ctx context.Context) (map[string][]byte, error)
- func (s *GCDSessionStore) Commit(token string, data []byte, expiry time.Time) error
- func (s *GCDSessionStore) CommitCtx(ctx context.Context, token string, data []byte, expiry time.Time) error
- func (s *GCDSessionStore) Delete(token string) error
- func (s *GCDSessionStore) DeleteCtx(ctx context.Context, token string) error
- func (s *GCDSessionStore) Find(token string) ([]byte, bool, error)
- func (s *GCDSessionStore) FindCtx(ctx context.Context, token string) ([]byte, bool, error)
- func (s *GCDSessionStore) StopCleanup()
- type UsersService
- func (s *UsersService) CreateUser(ctx context.Context, req *v1.CreateUserRequest) (*v1.CreateUserResponse, error)
- func (s *UsersService) DeleteFromStorage(ctx context.Context, id string) error
- func (s *UsersService) ListAllUsers(ctx context.Context) ([]*v1.User, error)
- func (s *UsersService) LoadUser(ctx context.Context, id string) (*v1.User, error)
- func (s *UsersService) NamespacedKey(kind, name string) *datastore.Key
- func (s *UsersService) SaveUser(ctx context.Context, id string, user *v1.User) error
- func (s *UsersService) UpdateUser(ctx context.Context, req *v1.UpdateUserRequest) (*v1.UpdateUserResponse, error)
- func (s *UsersService) UserExists(ctx context.Context, id string) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuthSession ¶
type AuthSession struct {
Token string `datastore:"-"` // Key name, not stored as property
ExpiresAt time.Time `datastore:"expiresAt"`
Data []byte `datastore:"data,noindex"`
}
AuthSession represents a session entity in Datastore.
type GCDSessionStore ¶
type GCDSessionStore struct {
// contains filtered or unexported fields
}
GCDSessionStore implements the scs.Store interface using Google Cloud Datastore.
func NewGCDSessionStore ¶
func NewGCDSessionStore(client *datastore.Client, namespace string, cleanupInterval time.Duration) *GCDSessionStore
NewGCDSessionStore creates a new Datastore-backed session store. The cleanupInterval parameter controls how frequently expired session data is removed. Setting it to 0 disables the background cleanup goroutine.
func (*GCDSessionStore) AllCtx ¶
AllCtx returns a map containing the token and data for all active (non-expired) sessions.
func (*GCDSessionStore) CommitCtx ¶
func (s *GCDSessionStore) CommitCtx(ctx context.Context, token string, data []byte, expiry time.Time) error
CommitCtx adds a session token and data to Datastore with the given expiry time. If the session token already exists, the data and expiry time are updated.
func (*GCDSessionStore) Delete ¶
func (s *GCDSessionStore) Delete(token string) error
func (*GCDSessionStore) DeleteCtx ¶
func (s *GCDSessionStore) DeleteCtx(ctx context.Context, token string) error
DeleteCtx removes a session token and corresponding data from Datastore.
func (*GCDSessionStore) FindCtx ¶
FindCtx returns the data for a given session token. If the session token is not found or is expired, the returned exists flag will be false.
func (*GCDSessionStore) StopCleanup ¶
func (s *GCDSessionStore) StopCleanup()
StopCleanup terminates the background cleanup goroutine.
type UsersService ¶
type UsersService struct {
services.BaseUsersService
MaxPageSize int
UserDAL v1dal.UserDatastoreDAL
// contains filtered or unexported fields
}
UsersService implements services.UsersService using Google Cloud Datastore. It embeds BaseUsersService to inherit caching and shared operations.
func NewUsersService ¶
func NewUsersService(client *datastore.Client, namespace string) *UsersService
NewUsersService creates a new Datastore-backed UsersService. The namespace parameter supports multi-tenancy; pass empty string for default namespace.
func (*UsersService) CreateUser ¶
func (s *UsersService) CreateUser(ctx context.Context, req *v1.CreateUserRequest) (*v1.CreateUserResponse, error)
CreateUser creates a new user profile.
func (*UsersService) DeleteFromStorage ¶
func (s *UsersService) DeleteFromStorage(ctx context.Context, id string) error
DeleteFromStorage implements UserStorageProvider.
func (*UsersService) ListAllUsers ¶
ListAllUsers implements UserStorageProvider.
func (*UsersService) NamespacedKey ¶
func (s *UsersService) NamespacedKey(kind, name string) *datastore.Key
NamespacedKey creates a Datastore key with the configured namespace
func (*UsersService) UpdateUser ¶
func (s *UsersService) UpdateUser(ctx context.Context, req *v1.UpdateUserRequest) (*v1.UpdateUserResponse, error)
UpdateUser updates an existing user profile.
func (*UsersService) UserExists ¶
func (s *UsersService) UserExists(ctx context.Context, id string) bool
UserExists implements UserStorageProvider.