Add CORS credentials, transactional endpoints, and container deployment
CORS
cors.go never set Access-Control-Allow-Credentials, so the
cookie-authenticated API was unreadable from any cross-origin frontend:
the server answered correctly and the browser blocked the page from
reading it. Set for allowlisted origins on both the preflight and the
actual response. Three tests added.
HTTP_COOKIE_SAMESITE (lax|none|strict, default lax) is new. CORS is only
half of what a cross-origin browser call needs; SameSite is judged on
registrable domain, so a frontend on an unrelated domain gets perfect CORS
headers and still no cookie. "none" is the only value that survives that,
and validate() refuses it without the Secure flag.
The "*" rejection now explains itself: browsers refuse Allow-Origin "*"
together with credentials, so it would break every authenticated call
rather than loosen anything.
Transactional endpoints (api-contract.md 12.1)
POST /api/v1/job-applications/{id}/hire
POST /api/v1/job-postings/{id}/assignments
Replaces two client-side loops that wrote several records with no
transaction and no rollback. Each is now one endpoint and one transaction,
built over repo.Repo so org scoping, derived columns, type casts and error
translation are not re-derived. Authorization reuses the existing policy
table rather than adding a parallel one: a workflow is exactly as
privileged as the writes it performs. 13 tests, including both rollback
paths.
Bug fix in the repository layer
repo.bindValue handled int64/int/float64/string but not int32, which is
what pgx returns for a PostgreSQL `int` column. Nothing previously read a
record and wrote one of its fields elsewhere, so it never surfaced; the
hire flow does exactly that and failed with "ai_score must be a number".
Both KindInt and KindFloat now accept the widths pgx actually produces.
Deployment
infrastructure/Dockerfile.api multi-stage, cross-compiling (BUILDPLATFORM
+ GOARCH) so linux/amd64 builds from arm64 are compiled rather than
emulated. Alpine runtime, non-root uid 10001, 22.1 MB. Ships api, seed,
setpassword and migrate, plus the migrations, so a Kubernetes
initContainer can apply the schema from the same image and tag as the
API. HEALTHCHECK keys on status code, not body, so a "degraded" instance
is not pulled from rotation during a migration window.
infrastructure/docker-compose.yml migrations run to completion before the
API starts. Assumes a managed PostgreSQL; the local-db overlay adds one
with TLS enabled so APP_ENV=production is met rather than dodged.
scripts/drop_public_tables.go the one-off used to clear an unrelated
schema from krowdb on 2026-08-24, kept for the record. Build-tagged
ignore and gated on CONFIRM_DROP=yes.
Verified against PostgreSQL: 16/16 new tests pass, and the image was built,
run and exercised end to end (login, CORS preflight, authenticated reads,
transaction rollback).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CmQiGq73Uyfq7J4yR8Vxxw
This commit is contained in:
@@ -572,6 +572,13 @@ func bindValue(c domain.Column, v any) (any, error) {
|
||||
return raw, nil
|
||||
|
||||
case domain.KindInt:
|
||||
// The int8/16/32 cases are not JSON shapes — encoding/json only ever
|
||||
// produces float64. They are the widths pgx hands back when a value was
|
||||
// READ from the database and is being written somewhere else: an `int`
|
||||
// column arrives as int32, and a service that copies a field from one
|
||||
// record to another (see service.Hire, which carries ai_score from an
|
||||
// application onto the staff row) would otherwise be told its own
|
||||
// database's value "must be a number".
|
||||
switch t := v.(type) {
|
||||
case float64:
|
||||
if t != float64(int64(t)) {
|
||||
@@ -592,19 +599,45 @@ func bindValue(c domain.Column, v any) (any, error) {
|
||||
return t, nil
|
||||
case int:
|
||||
return int64(t), nil
|
||||
case int32:
|
||||
return int64(t), nil
|
||||
case int16:
|
||||
return int64(t), nil
|
||||
case int8:
|
||||
return int64(t), nil
|
||||
case float32:
|
||||
if t != float32(int64(t)) {
|
||||
return nil, domain.Validation(
|
||||
fmt.Sprintf("%s must be a whole number", c.Name),
|
||||
map[string]string{c.Name: "expected an integer"})
|
||||
}
|
||||
return int64(t), nil
|
||||
}
|
||||
return nil, domain.Validation(fmt.Sprintf("%s must be a number", c.Name), nil)
|
||||
|
||||
case domain.KindFloat:
|
||||
// float32 and the integer widths for the same reason as above: a
|
||||
// numeric column is projected as float8 and returns float64, but an
|
||||
// integer read from elsewhere may legitimately be written into one.
|
||||
switch t := v.(type) {
|
||||
case float64:
|
||||
return t, nil
|
||||
case float32:
|
||||
return float64(t), nil
|
||||
case string:
|
||||
f, err := strconv.ParseFloat(t, 64)
|
||||
if err != nil {
|
||||
return nil, domain.Validation(fmt.Sprintf("%s must be a number", c.Name), nil)
|
||||
}
|
||||
return f, nil
|
||||
case int64:
|
||||
return float64(t), nil
|
||||
case int:
|
||||
return float64(t), nil
|
||||
case int32:
|
||||
return float64(t), nil
|
||||
case int16:
|
||||
return float64(t), nil
|
||||
}
|
||||
return nil, domain.Validation(fmt.Sprintf("%s must be a number", c.Name), nil)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user