Skip to content

Commit

Permalink
minor health improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Dec 5, 2024
1 parent e31319e commit 1cbb2ea
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,50 @@ All compact operations require a valid session ID in the `x-session-id` header.
- `"0"` if `allocatedBalance` >= `allocatableBalance`
- `allocatableBalance - allocatedBalance` otherwise

## API Endpoints

1. **Health Check**

```http
GET /health
```

Example response:

```json
{
"status": "healthy",
"allocatorAddress": "0x1234567890123456789012345678901234567890",
"signingAddress": "0x9876543210987654321098765432109876543210",
"timestamp": "2024-03-07T12:00:00.000Z"
}
```

2. **Get Session Payload**

```http
GET /session/:address
```

Returns an EIP-4361 payload for signing. Example response:

```json
{
"payload": {
"domain": "localhost:3000",
"address": "0x...",
"uri": "http://localhost:3000",
"statement": "Sign in to Smallocator",
"version": "1",
"chainId": 1,
"nonce": "unique_nonce",
"issuedAt": "2024-12-03T12:00:00Z",
"expirationTime": "2024-12-03T13:00:00Z",
"resources": ["http://localhost:3000/resources"]
}
}
```

## Development

### Prerequisites
Expand Down
38 changes: 33 additions & 5 deletions src/__tests__/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,45 @@ describe('API Routes', () => {
});

describe('GET /health', () => {
it('should return server health status', async () => {
it('should return health status and addresses', async () => {
const response = await server.inject({
method: 'GET',
url: '/health',
});

expect(response.statusCode).toBe(200);
const result = JSON.parse(response.payload);
expect(result.status).toBe('healthy');
expect(result.signingAddress).toBe(process.env.SIGNING_ADDRESS);
expect(result.allocatorAddress).toBe(process.env.ALLOCATOR_ADDRESS);
const body = JSON.parse(response.payload);

expect(body).toHaveProperty('status', 'healthy');
expect(body).toHaveProperty('allocatorAddress');
expect(body).toHaveProperty('signingAddress');
expect(body).toHaveProperty('timestamp');

// Verify timestamp is a valid ISO 8601 date
expect(new Date(body.timestamp).toISOString()).toBe(body.timestamp);
});

it('should fail if environment variables are not set', async () => {
// Store original env vars
const originalAllocator = process.env.ALLOCATOR_ADDRESS;
const originalSigning = process.env.SIGNING_ADDRESS;

// Unset env vars
delete process.env.ALLOCATOR_ADDRESS;
delete process.env.SIGNING_ADDRESS;

try {
const response = await server.inject({
method: 'GET',
url: '/health',
});

expect(response.statusCode).toBe(500);
} finally {
// Restore env vars
process.env.ALLOCATOR_ADDRESS = originalAllocator;
process.env.SIGNING_ADDRESS = originalSigning;
}
});
});

Expand Down
2 changes: 2 additions & 0 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export async function setupRoutes(server: FastifyInstance): Promise<void> {
status: string;
allocatorAddress: string;
signingAddress: string;
timestamp: string;
}> => {
if (!process.env.ALLOCATOR_ADDRESS || !process.env.SIGNING_ADDRESS) {
throw new Error('Required environment variables are not set');
Expand All @@ -109,6 +110,7 @@ export async function setupRoutes(server: FastifyInstance): Promise<void> {
status: 'healthy',
allocatorAddress: process.env.ALLOCATOR_ADDRESS,
signingAddress: process.env.SIGNING_ADDRESS,
timestamp: new Date().toISOString(),
};
}
);
Expand Down

0 comments on commit 1cbb2ea

Please sign in to comment.