Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/backend-governance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ jobs:
- name: Install dependencies
run: npm ci

- name: Generate Prisma client
run: npm run prisma:generate

- name: Type-check backend
run: npm run build

- name: Validate Prisma schema and migration consistency
run: npm run prisma:schema-check

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rust-wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
toolchain: stable
components: clippy
components: clippy, rustfmt
profile: minimal
override: true

Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

121 changes: 120 additions & 1 deletion backend/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,121 @@
"example": "99.5"
}
}
},
"HealthResponse": {
"type": "object",
"required": [
"status",
"timestamp",
"uptime",
"environment",
"checks"
],
"properties": {
"status": {
"type": "string",
"example": "healthy"
},
"timestamp": {
"type": "string",
"format": "date-time"
},
"uptime": {
"type": "number",
"example": 123.4
},
"environment": {
"type": "string",
"example": "production"
},
"checks": {
"type": "object",
"required": [
"api",
"cache",
"stellarRpc",
"databasePrimary",
"databaseReplica",
"prisma",
"jobs",
"indexer"
],
"properties": {
"api": {
"type": "string",
"enum": [
"up",
"down",
"degraded",
"unknown"
]
},
"cache": {
"type": "string",
"enum": [
"up",
"down",
"degraded",
"unknown"
]
},
"stellarRpc": {
"type": "string",
"enum": [
"up",
"down",
"degraded",
"unknown"
]
},
"databasePrimary": {
"type": "string",
"enum": [
"up",
"down",
"degraded",
"unknown"
]
},
"databaseReplica": {
"type": "string",
"enum": [
"up",
"down",
"degraded",
"unknown"
]
},
"prisma": {
"type": "string",
"enum": [
"up",
"down",
"degraded",
"unknown"
]
},
"jobs": {
"type": "string",
"enum": [
"up",
"down",
"degraded",
"unknown"
]
},
"indexer": {
"type": "string",
"enum": [
"up",
"down",
"degraded",
"unknown"
]
}
}
}
}
}
}
},
Expand Down Expand Up @@ -233,6 +348,9 @@
"description": "Service healthy",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HealthResponse"
},
"example": {
"status": "healthy",
"timestamp": "2024-01-01T00:00:00.000Z",
Expand All @@ -241,7 +359,8 @@
"checks": {
"api": "up",
"cache": "up",
"stellarRpc": "up"
"stellarRpc": "up",
"indexer": "up"
}
}
}
Expand Down
25 changes: 16 additions & 9 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions backend/src/__tests__/cacheInvalidation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { registerInvalidationHook, triggerCacheInvalidation } from '../middleware/cache';

describe('triggerCacheInvalidation', () => {
it('skips non-array hook results without throwing', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => undefined);
registerInvalidationHook(
(() => undefined) as unknown as (eventType: string, metadata?: Record<string, unknown>) => string[],
);
registerInvalidationHook(
(() => ['GET:/health']) as (eventType: string, metadata?: Record<string, unknown>) => string[],
);

expect(() => triggerCacheInvalidation('test.event')).not.toThrow();
expect(triggerCacheInvalidation('test.event').patternsInvalidated).toContain('GET:/health');
errorSpy.mockRestore();
});
});
19 changes: 19 additions & 0 deletions backend/src/__tests__/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ describe('OpenAPI documentation', () => {
expect(spec.info.description).toMatch(/Rate limit/i);
});

it('documents the indexer health dependency', () => {
const health = spec.paths['/health'] as {
get: {
responses: {
'200': {
content: {
'application/json': {
schema: { $ref: string };
};
};
};
};
};
};
expect(health.get.responses['200'].content['application/json'].schema.$ref).toBe(
'#/components/schemas/HealthResponse',
);
});

it('serves the spec and Swagger UI over HTTP', async () => {
const app = express();
setupSwagger(app);
Expand Down
24 changes: 22 additions & 2 deletions backend/src/middleware/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,10 @@ export function cacheMiddleware(options: CacheOptions) {

// ── Invalidation ─────────────────────────────────────────────────────────────

type InvalidationHook = (eventType: string, metadata?: Record<string, unknown>) => string[];
type InvalidationHook = (
eventType: string,
metadata?: Record<string, unknown>,
) => string[] | Promise<string[]> | Promise<void>;

const invalidationHooks: InvalidationHook[] = [];

Expand All @@ -332,7 +335,24 @@ export function triggerCacheInvalidation(
for (const hook of invalidationHooks) {
try {
const hookPatterns = hook(eventType, metadata);
patterns.push(...hookPatterns);
if (hookPatterns instanceof Promise) {
void hookPatterns.catch((err) => {
console.error(
JSON.stringify({
level: 'error',
event: 'invalidation_hook_error',
error: err instanceof Error ? err.message : String(err),
}),
);
});
continue;
}

if (!Array.isArray(hookPatterns)) {
throw new TypeError('invalidation hook must return an array of patterns');
}

patterns.push(...hookPatterns.filter((pattern): pattern is string => typeof pattern === 'string'));
} catch (err) {
console.error(
JSON.stringify({
Expand Down
27 changes: 26 additions & 1 deletion backend/src/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,30 @@ const options: swaggerJsdoc.Options = {
shares: { type: 'string', example: '99.5' },
},
},
HealthResponse: {
type: 'object',
required: ['status', 'timestamp', 'uptime', 'environment', 'checks'],
properties: {
status: { type: 'string', example: 'healthy' },
timestamp: { type: 'string', format: 'date-time' },
uptime: { type: 'number', example: 123.4 },
environment: { type: 'string', example: 'production' },
checks: {
type: 'object',
required: ['api', 'cache', 'stellarRpc', 'databasePrimary', 'databaseReplica', 'prisma', 'jobs', 'indexer'],
properties: {
api: { type: 'string', enum: ['up', 'down', 'degraded', 'unknown'] },
cache: { type: 'string', enum: ['up', 'down', 'degraded', 'unknown'] },
stellarRpc: { type: 'string', enum: ['up', 'down', 'degraded', 'unknown'] },
databasePrimary: { type: 'string', enum: ['up', 'down', 'degraded', 'unknown'] },
databaseReplica: { type: 'string', enum: ['up', 'down', 'degraded', 'unknown'] },
prisma: { type: 'string', enum: ['up', 'down', 'degraded', 'unknown'] },
jobs: { type: 'string', enum: ['up', 'down', 'degraded', 'unknown'] },
indexer: { type: 'string', enum: ['up', 'down', 'degraded', 'unknown'] },
},
},
},
},
},
},
tags: [
Expand All @@ -160,12 +184,13 @@ const options: swaggerJsdoc.Options = {
description: 'Service healthy',
content: {
'application/json': {
schema: { $ref: '#/components/schemas/HealthResponse' },
example: {
status: 'healthy',
timestamp: '2024-01-01T00:00:00.000Z',
uptime: 123.4,
environment: 'production',
checks: { api: 'up', cache: 'up', stellarRpc: 'up' },
checks: { api: 'up', cache: 'up', stellarRpc: 'up', indexer: 'up' },
},
},
},
Expand Down
1 change: 0 additions & 1 deletion backend/src/vaultEndpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,6 @@ router.get('/strategy/cooldown', cacheMiddleware({ ttl: 5000 }), (_req: Request,
});
});

router.post('/strategy', depositsLimiter, requireFlag('strategy-selection'), (_req: Request, res: Response) => {
router.post('/strategy', depositsLimiter, requireFlag('strategy-selection'), validate({ body: VaultStrategyBodySchema }), (req: Request, res: Response) => {
const cooldownSec = parseInt(process.env.STRATEGY_SWITCH_COOLDOWN_SEC || '0', 10);
const lastSwitchIso = process.env.LAST_STRATEGY_SWITCH_TIME || null;
Expand Down
Loading
Loading