store

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package store provides database initialization and multi-tenant support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultPostgresDSN

func DefaultPostgresDSN(host string, port int, user, password, dbname string) string

DefaultPostgresDSN returns a default PostgreSQL connection string.

func DefaultSQLiteDSN

func DefaultSQLiteDSN(path string) string

DefaultSQLiteDSN returns a default SQLite connection string.

func OrgFromContext

func OrgFromContext(ctx context.Context) (int, bool)

OrgFromContext extracts the org ID from context.

Types

type Config

type Config struct {
	// Driver is the database driver: "sqlite3" or "postgres"
	Driver string

	// DSN is the data source name (connection string)
	// SQLite: "file:omniproxy.db?cache=shared&_fk=1"
	// PostgreSQL: "postgres://user:pass@host:5432/dbname?sslmode=disable"
	DSN string

	// EnableRLS enables PostgreSQL Row-Level Security for multi-tenancy
	// Only applies when Driver is "postgres"
	EnableRLS bool

	// Debug enables Ent debug logging
	Debug bool
}

Config holds database configuration.

type RLSHelper

type RLSHelper struct {
	// contains filtered or unexported fields
}

RLSHelper manages PostgreSQL Row-Level Security for multi-tenancy.

func NewRLSHelper

func NewRLSHelper(db *sql.DB) *RLSHelper

NewRLSHelper creates a new RLS helper.

func (*RLSHelper) ClearTenantContext

func (r *RLSHelper) ClearTenantContext(ctx context.Context) error

ClearTenantContext clears the tenant context.

func (*RLSHelper) CreateBypassRole

func (r *RLSHelper) CreateBypassRole(ctx context.Context, roleName string) error

CreateBypassRole creates a role that bypasses RLS (for admin/system operations).

func (*RLSHelper) SetTenantContext

func (r *RLSHelper) SetTenantContext(ctx context.Context, orgID int) error

SetTenantContext sets the current tenant for RLS policies. This must be called at the start of each request/transaction.

func (*RLSHelper) SetTenantContextTx

func (r *RLSHelper) SetTenantContextTx(ctx context.Context, tx *sql.Tx, orgID int) error

SetTenantContextTx sets the current tenant within a transaction.

func (*RLSHelper) SetupRLS

func (r *RLSHelper) SetupRLS(ctx context.Context) error

SetupRLS creates RLS policies for all tenant-scoped tables. This should be called after schema migration.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store wraps the Ent client with multi-tenant support.

func New

func New(cfg Config) (*Store, error)

New creates a new Store with the given configuration.

func (*Store) Client

func (s *Store) Client() *ent.Client

Client returns the Ent client.

func (*Store) Close

func (s *Store) Close() error

Close closes the database connection.

func (*Store) DB

func (s *Store) DB() *sql.DB

DB returns the underlying sql.DB connection.

func (*Store) Migrate

func (s *Store) Migrate(ctx context.Context) error

Migrate runs database migrations.

func (*Store) TenantTx

func (s *Store) TenantTx(ctx context.Context, orgID int) (*ent.Tx, error)

TenantTx starts a transaction with RLS tenant context set. For PostgreSQL with RLS enabled, this sets the app.current_org_id variable. For SQLite, it returns a normal transaction (filtering must be done in queries).

func (*Store) WithOrg

func (s *Store) WithOrg(ctx context.Context, orgID int) context.Context

WithOrg returns a new context with the org ID set for RLS. This should be called at the start of each request to set tenant context.

type TrafficHandler

type TrafficHandler struct {
	// contains filtered or unexported fields
}

TrafficHandler handles storing captured traffic in the database.

func NewTrafficHandler

func NewTrafficHandler(client *ent.Client, proxyID int) *TrafficHandler

NewTrafficHandler creates a new traffic handler for a specific proxy.

func (*TrafficHandler) BatchStore

func (h *TrafficHandler) BatchStore(ctx context.Context, records []*capture.Record) error

BatchStore saves multiple records in a single transaction.

func (*TrafficHandler) Handle

func (h *TrafficHandler) Handle(rec *capture.Record)

Handle stores a captured traffic record in the database. This implements the capture.Handler interface.

func (*TrafficHandler) Store

func (h *TrafficHandler) Store(ctx context.Context, rec *capture.Record) error

Store saves a capture record to the database.

type TrafficQuery

type TrafficQuery struct {
	// contains filtered or unexported fields
}

TrafficQuery provides query helpers for traffic data.

func NewTrafficQuery

func NewTrafficQuery(client *ent.Client, proxyID int) *TrafficQuery

NewTrafficQuery creates a new traffic query helper.

func (*TrafficQuery) ByHost

func (q *TrafficQuery) ByHost(ctx context.Context, host string, limit int) ([]*ent.Traffic, error)

ByHost returns traffic records for a specific host.

func (*TrafficQuery) ByTimeRange

func (q *TrafficQuery) ByTimeRange(ctx context.Context, start, end time.Time, limit int) ([]*ent.Traffic, error)

ByTimeRange returns traffic records within a time range.

func (*TrafficQuery) GetStats

func (q *TrafficQuery) GetStats(ctx context.Context) (*TrafficStats, error)

GetStats returns traffic statistics for the proxy.

func (*TrafficQuery) Recent

func (q *TrafficQuery) Recent(ctx context.Context, limit int) ([]*ent.Traffic, error)

Recent returns the most recent traffic records.

type TrafficStats

type TrafficStats struct {
	TotalRequests    int64
	TotalErrors      int64
	AvgDurationMs    float64
	UniqueHosts      int64
	RequestsByMethod map[string]int64
	RequestsByStatus map[int]int64
}

Stats returns aggregate statistics for traffic.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL