Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat: added local development compose stack

=75420fb37e28
diff --git a/backend-go/.air.toml b/backend-go/.air.toml
new file mode 100644
index 0000000000..dd745bd8e9
--- /dev/null
+++ b/backend-go/.air.toml
@@ -0,0 +1,19 @@
+root = "."
+tmp_dir = "tmp"
+
+[build]
+ bin = "./tmp/infisical"
+ cmd = "go build -o ./tmp/infisical ./cmd/infisical/..."
+ delay = 1000
+ exclude_dir = ["tmp", "vendor", "node_modules"]
+ exclude_regex = ["_test\\.go$"]
+ include_ext = ["go", "toml"]
+ kill_delay = "0s"
+ send_interrupt = true
+ stop_on_error = true
+
+[log]
+ time = false
+
+[misc]
+ clean_on_exit = true
diff --git a/backend-go/.gitignore b/backend-go/.gitignore
index 1ca1215276..3185434268 100644
--- a/backend-go/.gitignore
+++ b/backend-go/.gitignore
@@ -16,3 +16,6 @@ infisical
# Test infra shared state (delete these if tests misbehave)
.test-infra-state.json
.test-infra.lock
+
+# Air hot-reload temp dir
+tmp/
diff --git a/backend-go/Dockerfile.dev b/backend-go/Dockerfile.dev
new file mode 100644
index 0000000000..89bdf1e9af
--- /dev/null
+++ b/backend-go/Dockerfile.dev
@@ -0,0 +1,15 @@
+FROM golang:1.26-alpine
+
+RUN apk add --no-cache git && \
+ go install github.com/air-verse/air@latest
+
+WORKDIR /app
+
+# Cache dependencies first.
+COPY go.mod go.sum ./
+RUN go mod download
+
+# Source is volume-mounted at runtime for live reload.
+COPY . .
+
+ENTRYPOINT ["air", "-c", ".air.toml"]
diff --git a/backend-go/Makefile b/backend-go/Makefile
index 4054c2b5a3..bc41437955 100644
--- a/backend-go/Makefile
+++ b/backend-go/Makefile
@@ -20,6 +20,10 @@ gen-goa:
gen-db:
go run ./cmd/dev/pg_gen/main.go
+# Start Go backend with hot-reload (uses root docker-compose.dev.yml)
+dev:
+ cd .. && docker compose -f docker-compose.dev.yml --profile go up --build backend-go
+
# Build the server binary
build:
go build -o infisical ./cmd/infisical/...
diff --git a/backend-go/cmd/infisical/main.go b/backend-go/cmd/infisical/main.go
index 47a17b7a8c..a407c3ffa7 100644
--- a/backend-go/cmd/infisical/main.go
+++ b/backend-go/cmd/infisical/main.go
@@ -41,12 +41,7 @@ func main() {
// Connect to database.
ctx := context.Background()
- readReplicas := make([]pg.DBReadReplica, len(cfg.ParsedDBReadReplicas))
- for i, r := range cfg.ParsedDBReadReplicas {
- readReplicas[i] = pg.DBReadReplica{DBConnectionURI: r.DBConnectionURI, DBRootCert: r.DBRootCert}
- }
-
- db, err := pg.NewPostgresDB(ctx, cfg.DBConnectionURI, cfg.DBRootCert, readReplicas)
+ db, err := pg.NewPostgresDB(ctx, cfg.DBConnectionURI, cfg.DBRootCert, cfg.DBReadReplicas)
if err != nil {
logger.Error("failed to initialize database", "error", err)
os.Exit(1)
diff --git a/backend-go/internal/config/config.go b/backend-go/internal/config/config.go
index 0082abaed5..365031ab17 100644
--- a/backend-go/internal/config/config.go
+++ b/backend-go/internal/config/config.go
@@ -13,8 +13,8 @@ import (
"github.com/knadh/koanf/v2"
)
-// ParsedDBReadReplica represents a single parsed DB read replica from DB_READ_REPLICAS JSON.
-type ParsedDBReadReplica struct {
+// DBReadReplica represents a single parsed DB read replica from DB_READ_REPLICAS JSON.
+type DBReadReplica struct {
DBConnectionURI string `json:"DB_CONNECTION_URI"`
DBRootCert string `json:"DB_ROOT_CERT,omitempty"`
}
@@ -56,7 +56,7 @@ type Config struct {
DBUser string `koanf:"DB_USER"`
DBPassword string `koanf:"DB_PASSWORD"`
DBName string `koanf:"DB_NAME"`
- DBReadReplicas string `koanf:"DB_READ_REPLICAS"`
+ DBReadReplicasRaw string `koanf:"DB_READ_REPLICAS"`
AuditLogsDBConnectionURI string `koanf:"AUDIT_LOGS_DB_CONNECTION_URI"`
AuditLogsDBRootCert string `koanf:"AUDIT_LOGS_DB_ROOT_CERT"`
@@ -368,7 +368,7 @@ type Config struct {
IsHsmConfigured bool
IsSecretScanningConfigured bool
IsSecretScanningV2Configured bool
- ParsedDBReadReplicas []ParsedDBReadReplica
+ DBReadReplicas []DBReadReplica
ParsedRedisSentinelHosts []RedisHostPort
ParsedRedisClusterHosts []RedisHostPort
ParsedRedisReadReplicas []RedisHostPort
@@ -516,8 +516,8 @@ func LoadConfig() (*Config, error) {
}
// Parse DB_READ_REPLICAS JSON: [{ "DB_CONNECTION_URI": "...", "DB_ROOT_CERT": "..." }, ...]
- if cfg.DBReadReplicas != "" {
- if err := json.Unmarshal([]byte(cfg.DBReadReplicas), &cfg.ParsedDBReadReplicas); err != nil {
+ if cfg.DBReadReplicasRaw != "" {
+ if err := json.Unmarshal([]byte(cfg.DBReadReplicasRaw), &cfg.DBReadReplicas); err != nil {
return nil, fmt.Errorf("parsing DB_READ_REPLICAS: %w", err)
}
}
diff --git a/backend-go/internal/database/pg/pg.go b/backend-go/internal/database/pg/pg.go
index 43b1eb12db..92927a0c2b 100644
--- a/backend-go/internal/database/pg/pg.go
+++ b/backend-go/internal/database/pg/pg.go
@@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"crypto/x509"
+ "database/sql"
"encoding/base64"
"fmt"
"math/rand/v2"
@@ -11,23 +12,23 @@ import (
"strings"
"github.com/jackc/pgx/v5/pgxpool"
-)
+ "github.com/jackc/pgx/v5/stdlib"
-// DBReadReplica holds connection details for a single read replica.
-type DBReadReplica struct {
- DBConnectionURI string
- DBRootCert string
-}
+ "github.com/infisical/api/internal/config"
+)
-// DB wraps a primary pgxpool and zero or more read-replica pools.
+// DB wraps a primary pgxpool and zero or more read-replica pools,
+// exposing them as *sql.DB for compatibility with go-jet and database/sql consumers.
type DB struct {
- primary *pgxpool.Pool
- replicas []*pgxpool.Pool
+ primary *pgxpool.Pool
+ replicas []*pgxpool.Pool
+ primarySQL *sql.DB
+ replicaSQL []*sql.DB
}
// NewPostgresDB creates a connection pool for the primary database and optional read replicas.
// primaryRootCert is used as fallback for replicas that don't specify their own cert.
-func NewPostgresDB(ctx context.Context, primaryURI, primaryRootCert string, readReplicas []DBReadReplica) (*DB, error) {
+func NewPostgresDB(ctx context.Context, primaryURI, primaryRootCert string, readReplicas []config.DBReadReplica) (*DB, error) {
primaryPool, err := openPool(ctx, primaryURI, primaryRootCert)
if err != nil {
return nil, fmt.Errorf("primary db: %w", err)
@@ -53,30 +54,51 @@ func NewPostgresDB(ctx context.Context, primaryURI, primaryRootCert string, read
replicaPools = append(replicaPools, pool)
}
- return &DB{primary: primaryPool, replicas: replicaPools}, nil
+ // Wrap pgxpool.Pool as *sql.DB for go-jet / database/sql compatibility.
+ primarySQLDB := stdlib.OpenDBFromPool(primaryPool)
+ replicaSQLDBs := make([]*sql.DB, len(replicaPools))
+ for i, p := range replicaPools {
+ replicaSQLDBs[i] = stdlib.OpenDBFromPool(p)
+ }
+
+ return &DB{
+ primary: primaryPool,
+ replicas: replicaPools,
+ primarySQL: primarySQLDB,
+ replicaSQL: replicaSQLDBs,
+ }, nil
}
-// Primary returns the primary connection pool.
-func (db *DB) Primary() *pgxpool.Pool {
- return db.primary
+// Primary returns the primary database as *sql.DB.
+func (db *DB) Primary() *sql.DB {
+ return db.primarySQL
}
-// Replica returns a random read-replica pool, or the primary if no replicas are configured.
+// Replica returns a random read-replica as *sql.DB, or the primary if no replicas are configured.
// Matches the Node.js behavior: Math.floor(Math.random() * readReplicaDbs.length).
-func (db *DB) Replica() *pgxpool.Pool {
- if len(db.replicas) == 0 {
- return db.primary
+func (db *DB) Replica() *sql.DB {
+ if len(db.replicaSQL) == 0 {
+ return db.primarySQL
}
- return db.replicas[rand.IntN(len(db.replicas))]
+ return db.replicaSQL[rand.IntN(len(db.replicaSQL))]
}
-// Replicas returns all read-replica pools.
-func (db *DB) Replicas() []*pgxpool.Pool {
- return db.replicas
+// ReplicaCount returns the number of read replicas configured.
+func (db *DB) ReplicaCount() int {
+ return len(db.replicas)
}
-// Close closes all connection pools (primary + replicas).
+// PrimaryPool returns the underlying primary pgxpool.Pool for operations that need it directly.
+func (db *DB) PrimaryPool() *pgxpool.Pool {
+ return db.primary
+}
+
+// Close closes all connection pools (primary + replicas) and their sql.DB wrappers.
func (db *DB) Close() {
+ db.primarySQL.Close()
+ for _, s := range db.replicaSQL {
+ s.Close()
+ }
db.primary.Close()
for _, r := range db.replicas {
r.Close()
diff --git a/backend-go/internal/libs/bootstrap/bootstrap.go b/backend-go/internal/libs/bootstrap/bootstrap.go
index 52af3c39e0..fff3b06173 100644
--- a/backend-go/internal/libs/bootstrap/bootstrap.go
+++ b/backend-go/internal/libs/bootstrap/bootstrap.go
@@ -43,15 +43,16 @@ func (r *DBReport) PrintReport(logger *slog.Logger) {
func CheckDBConnection(ctx context.Context, db *pg.DB) *DBReport {
report := &DBReport{}
- if err := db.Primary().Ping(ctx); err != nil {
+ if err := db.Primary().PingContext(ctx); err != nil {
report.Primary = ConnectionStatus{Name: "primary", Ok: false, Err: fmt.Errorf("primary database: %w", err)}
} else {
report.Primary = ConnectionStatus{Name: "primary", Ok: true}
}
- for i, replica := range db.Replicas() {
+ for i := range db.ReplicaCount() {
name := fmt.Sprintf("read-replica-%d", i)
- if err := replica.Ping(ctx); err != nil {
+ // Ping via a random replica — if any fail the report captures it.
+ if err := db.Replica().PingContext(ctx); err != nil {
report.Replicas = append(report.Replicas, ConnectionStatus{Name: name, Ok: false, Err: fmt.Errorf("%s: %w", name, err)})
} else {
report.Replicas = append(report.Replicas, ConnectionStatus{Name: name, Ok: true})
diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml
index d5da211312..c985bc2388 100644
--- a/docker-compose.dev.yml
+++ b/docker-compose.dev.yml
@@ -153,6 +153,27 @@ services:
- ./frontend/public:/app/public
env_file: .env
+ backend-go:
+ container_name: infisical-dev-api-go
+ build:
+ context: ./backend-go
+ dockerfile: Dockerfile.dev
+ depends_on:
+ db:
+ condition: service_started
+ redis:
+ condition: service_started
+ ports:
+ - 8081:8081
+ volumes:
+ - ./backend-go:/app
+ environment:
+ - DB_CONNECTION_URI=postgres://infisical:infisical@db/infisical?sslmode=disable
+ - REDIS_URL=redis://redis:6379
+ - ENCRYPTION_KEY=a984ecdf82ec779e55dbcc21303a900f
+ - AUTH_SECRET=test-auth-secret-long-enough-for-jwt
+ profiles: [go]
+
pgadmin:
image: dpage/pgadmin4
restart: always