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
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,29 @@ import {
UseGuards,
Request,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiProperty } from '@nestjs/swagger';
import { IsNumber, IsString, IsUUID, Min } from 'class-validator';
import { Throttle } from '@nestjs/throttler';
import { FarmVaultsService } from './farm-vaults.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';

class CreateFarmVaultDto {
@ApiProperty({ description: 'Vault name' })
@IsString()
name: string;

@ApiProperty({ description: 'Crop cycle ID' })
@IsUUID()
cropCycleId: string;

@ApiProperty({ description: 'Target amount' })
@IsNumber()
@Min(0)
targetAmount: number;
}

class FarmVaultAmountDto {
@ApiProperty({ description: 'Amount to deposit/withdraw' })
@IsNumber()
@Min(0.01)
amount: number;
Expand Down
23 changes: 14 additions & 9 deletions harvest-finance/backend/src/health/health.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ import {
HealthCheck,
HealthCheckService,
TypeOrmHealthIndicator,
HealthCheckError,
HealthIndicator,
HealthIndicatorResult,
} from '@nestjs/terminus';
import { SkipThrottle } from '@nestjs/throttler';
import {
ApiTags,
ApiOperation,
ApiResponse,
} from '@nestjs/swagger';
import { StellarClientService } from '../stellar/services/stellar-client.service';
import { RedisHealthIndicator } from './redis.health';

@SkipThrottle()
@Controller('health')
Expand All @@ -16,23 +22,22 @@ export class HealthController {
private health: HealthCheckService,
private db: TypeOrmHealthIndicator,
private stellarClient: StellarClientService,
private redis: RedisHealthIndicator,
) {}

@Get()
@HealthCheck()
check() {
return this.health.check([
() => this.db.pingCheck('database', { timeout: 1500 }),
() => this.redis.isHealthy('redis', 3000),
async (): Promise<HealthIndicatorResult> => {
const streamHealth = this.stellarClient.getStreamHealth();
if (!streamHealth.isConnected) {
throw new HealthCheckError('Stellar stream is disconnected', {
'stellar-payment-stream': streamHealth,
});
try {
await this.stellarClient.checkHorizon(3000);
return { 'stellar-horizon': { status: 'up' } };
} catch {
return { 'stellar-horizon': { status: 'down' } };
}
return {
'stellar-payment-stream': streamHealth,
};
},
]);
}
Expand Down
21 changes: 21 additions & 0 deletions harvest-finance/backend/src/health/redis.health.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable, Inject } from '@nestjs/common';
import { HealthIndicator, HealthIndicatorResult, HealthCheckError } from '@nestjs/terminus';
import Redis from 'ioredis';

@Injectable()
export class RedisHealthIndicator extends HealthIndicator {
constructor(@Inject('REDIS_CLIENT') private readonly client: Redis) {}

async isHealthy(key = 'redis', timeout = 3000): Promise<HealthIndicatorResult> {
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error('Redis timeout')), timeout),
);
const pingPromise = this.client.ping();
try {
await Promise.race([pingPromise, timeoutPromise]);
return this.getStatus(key, true);
} catch {
throw new HealthCheckError('Redis is unavailable', { [key]: { status: 'down' } });
}
}
}
13 changes: 13 additions & 0 deletions harvest-finance/backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,21 @@ async function bootstrap() {
customSiteTitle: 'Harvest Finance API Docs',
});

// Serve raw OpenAPI JSON at /api/docs-json
app.use('/api/docs-json', (req, res) => {
res.json(document);
});

const configService = app.get(ConfigService);
const port = configService.get<number>('PORT') || 5000;
const isProduction = configService.get<string>('NODE_ENV') === 'production';

// Serve raw OpenAPI JSON and Swagger UI in non-production environments
if (!isProduction) {
app.getHttp().getRouter().get('/api/docs-json', (req, res) => {
res.json(document);
});
}

const server = await app.listen(port);
console.log(`Application is running on: http://localhost:${port}`);
Expand Down