Help us shape the future of Go! Take the 2026 Go Developer Survey

synchroapi

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 17 Imported by: 0

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

View Source
const (
	ExpectedProtocolVersion    = 3
	ExpectedSQLContractVersion = 1
	ExpectedExtensionSchema    = "synchro"
)
View Source
const (

	// DefaultDatabaseQueryTimeout bounds one canonical extension call. It remains
	// below the standalone server write timeout.
	DefaultDatabaseQueryTimeout = 25 * time.Second
)

Variables

View Source
var ErrAuthRequired = errors.New("auth required")

ErrAuthRequired indicates the request does not carry an authenticated user.

Functions

func RequestContextUserIDResolver

func RequestContextUserIDResolver(r *http.Request) (string, error)

RequestContextUserIDResolver resolves a user ID previously stored in the request context with WithUserID.

func RequireCompatibleExtension

func RequireCompatibleExtension(ctx context.Context, db *sql.DB) error

func Routes

func Routes(cfg Config) http.Handler

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

func UserIDFromContext(ctx context.Context) string

UserIDFromContext extracts the user ID set by the JWT middleware.

func WithUserID

func WithUserID(ctx context.Context, userID string) context.Context

WithUserID returns a new context with the resolved canonical user ID set.

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

type RebuildRequest struct {
	ClientID string `json:"client_id"`
	Scope    string `json:"scope"`
}

RebuildRequest is the minimal validated request envelope for rebuild.

type UserIDResolver

type UserIDResolver func(*http.Request) (string, error)

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.

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.

Jump to

Keyboard shortcuts

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