Documentation
¶
Overview ¶
Package synchroapi provides a standalone HTTP adapter for the synchro PostgreSQL extension. It is a thin passthrough layer: parse HTTP request, call SELECT synchro.synchro_*() via database/sql, forward the JSONB response.
This package has zero dependency on the synchro engine module. Users bring their own *sql.DB (pgx/v5 stdlib recommended) and auth integration.
Usage:
handler := synchroapi.Routes(synchroapi.Config{
DB: db,
JWTSecret: []byte("my-secret"),
MinClientVersion: "1.0.0",
})
Or, when the main API router already authenticated the request:
handler := synchroapi.Routes(synchroapi.Config{
DB: db,
UserIDResolver: synchroapi.RequestContextUserIDResolver,
})
http.ListenAndServe(":8080", handler)
Index ¶
- Constants
- Variables
- func RequestContextUserIDResolver(r *http.Request) (string, error)
- func RequireCompatibleExtension(ctx context.Context, db *sql.DB) error
- func Routes(cfg Config) http.Handler
- func UserIDFromContext(ctx context.Context) string
- func WithUserID(ctx context.Context, userID string) context.Context
- type Config
- type ConnectRequest
- type ExtensionContractInfo
- type Handler
- type PullRequest
- type PushRequest
- type RebuildRequest
- type UserIDResolver
Constants ¶
const ( ExpectedProtocolVersion = 3 ExpectedSQLContractVersion = 1 ExpectedExtensionSchema = "synchro" )
const ( // DefaultDatabaseQueryTimeout bounds one canonical extension call. It remains // below the standalone server write timeout. DefaultDatabaseQueryTimeout = 25 * time.Second )
Variables ¶
var ErrAuthRequired = errors.New("auth required")
ErrAuthRequired indicates the request does not carry an authenticated user.
Functions ¶
func RequestContextUserIDResolver ¶
RequestContextUserIDResolver resolves a user ID previously stored in the request context with WithUserID.
func Routes ¶
Routes returns an http.Handler with all sync endpoints mounted. The returned handler applies version check and one auth mode:
- trusted upstream auth via UserIDResolver - JWT validation via JWTSecret or JWKSURL
Endpoints:
POST /sync/connect (auth required) POST /sync/pull (auth required) POST /sync/push (auth required) POST /sync/rebuild (auth required) GET /sync/schema (no auth) GET /sync/tables (no auth) GET /ready (no auth)
func UserIDFromContext ¶
UserIDFromContext extracts the user ID set by the JWT middleware.
Types ¶
type Config ¶
type Config struct {
// DB is the database connection pool (required). Must be connected to a
// PostgreSQL database with the synchro_pg extension installed.
DB *sql.DB
// UserIDResolver resolves the canonical internal user ID from a trusted
// upstream-authenticated request. Mutually exclusive with JWTSecret and
// JWKSURL.
UserIDResolver UserIDResolver
// JWTSecret is the HS256 shared secret for token validation.
// Mutually exclusive with JWKSURL and UserIDResolver.
JWTSecret []byte
// JWKSURL is the RS256/ES256 JWKS endpoint for token validation.
// Mutually exclusive with JWTSecret and UserIDResolver.
JWKSURL string
// JWKSContext controls the JWKS refresh lifecycle. It is required when
// JWKSURL is set. Cancel it when the handler is no longer in use.
JWKSContext context.Context
// JWTUserClaim is the JWT claim containing the user ID (default: "sub").
JWTUserClaim string
// MinClientVersion is the minimum client version allowed (semver).
// When set, authenticated sync requests must send exactly one
// X-Client-Version or X-App-Version header at or above this version.
// Leave empty to skip version checking.
MinClientVersion string
// DatabaseQueryTimeout bounds one canonical extension call. A zero value uses
// DefaultDatabaseQueryTimeout. A negative value is invalid.
DatabaseQueryTimeout time.Duration
}
Config configures the sync HTTP handler.
type ConnectRequest ¶
type ConnectRequest struct {
ClientID string `json:"client_id"`
}
ConnectRequest is the request body for POST /sync/connect.
type ExtensionContractInfo ¶
type ExtensionContractInfo struct {
ExtensionVersion string `json:"extension_version"`
SQLContractVersion int `json:"sql_contract_version"`
ProtocolVersion int `json:"protocol_version"`
LibraryBuildFingerprint string `json:"library_build_fingerprint"`
InstalledBuildFingerprint string `json:"installed_build_fingerprint"`
ExtensionObjectsCurrent bool `json:"extension_objects_current"`
}
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler holds the database connection and serves sync endpoints.
type PullRequest ¶
type PullRequest struct {
ClientID string `json:"client_id"`
}
PullRequest is the minimal validated request envelope for pull.
type PushRequest ¶
type PushRequest struct {
ClientID string `json:"client_id"`
}
PushRequest is the minimal validated request envelope for push.
type RebuildRequest ¶
RebuildRequest is the minimal validated request envelope for rebuild.
type UserIDResolver ¶
UserIDResolver resolves the canonical internal user ID for a request. It is intended for trusted upstream auth integration, for example when the main API router has already validated WorkOS or another identity provider.
Return ErrAuthRequired when the request is unauthenticated. Return any other error only for internal resolver failures.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
synchro-seed
command
|
|
|
synchro-version
command
|
|
|
synchrod-pg
command
synchrod-pg is the sync HTTP server backed by the synchro_pg PostgreSQL extension.
|
synchrod-pg is the sync HTTP server backed by the synchro_pg PostgreSQL extension. |
|
internal
|
|
|
Package operator coordinates projection bootstrap operations.
|
Package operator coordinates projection bootstrap operations. |