hatch-surf/internal/store/db.go
CTO 90c3f6285d feat: mock endpoint — configure response per endpoint
- Add mock_status, mock_headers, mock_body columns to endpoints schema
- Add MockConfig struct and UpsertMock to Repository interface
- Implement UpsertMock in sqliteRepo (INSERT ON CONFLICT UPDATE)
- Update GetEndpoint to return mock configuration when present
- Add PUT /e/{endpoint_id}/mock handler with JSON body {status, headers, body}
- Add catch-all /{endpoint_id} handler returning mock or default 200
- Header sanitization: only allow Content-Type, Cache-Control, X-*
- Wire handler into cmd/hatch main with SQLite store
- Fix Dockerfile: COPY go.sum for reproducible builds
- Tests: store mock CRUD, handler mock config + header sanitization

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-06-22 14:32:53 +02:00

36 lines
873 B
Go

package store
import (
"database/sql"
_ "embed"
"fmt"
"os"
"path/filepath"
_ "modernc.org/sqlite"
)
//go:embed schema.sql
var schemaSQL string
func Open(dbPath string) (Repository, error) {
if dbPath == "" { dbPath = filepath.Join("data", "hatch.db") }
dir := filepath.Dir(dbPath)
if err := os.MkdirAll(dir, 0o755); err != nil {
return nil, fmt.Errorf("store: create db dir %s: %w", dir, err)
}
conn, err := sql.Open("sqlite", dbPath+"?_journal_mode=WAL&_busy_timeout=5000")
if err != nil { return nil, fmt.Errorf("store: open %s: %w", dbPath, err) }
conn.SetMaxOpenConns(1)
if err := migrate(conn); err != nil {
conn.Close()
return nil, fmt.Errorf("store: migrate: %w", err)
}
return NewSQLiteRepo(conn)
}
func migrate(db *sql.DB) error {
_, err := db.Exec(schemaSQL)
if err != nil { return fmt.Errorf("store/migrate: %w", err) }
return nil
}