Skip to content
Draft
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 health-check-server/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PORT=3000
NODE_ENV=development
API_URL=https://api.internxt.com
AUTH_TOKEN=your-jwt-token-here
CLIENT_NAME=health-check-server
CLIENT_VERSION=1.0.0
6 changes: 6 additions & 0 deletions health-check-server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
dist/
.env
*.log
.DS_Store

24 changes: 24 additions & 0 deletions health-check-server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@internxt/health-check-server",
"version": "1.0.0",
"description": "Health check server for monitoring Internxt API endpoints",
"private": true,
"main": "dist/index.js",
"scripts": {
"dev": "tsx watch src/index.ts",
"build": "tsc",
"start": "node dist/index.js",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"fastify": "^4.28.1",
"@fastify/env": "^4.4.0",
"dotenv": "^16.4.5"
},
"devDependencies": {
"@types/node": "^20.14.10",
"pino-pretty": "^11.2.1",
"tsx": "^4.16.2",
"typescript": "^5.9.3"
}
}
28 changes: 28 additions & 0 deletions health-check-server/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { config as loadEnv } from 'dotenv';
import { Config } from './types';

loadEnv();

function loadConfig(): Config {
const requiredVars = ['API_URL', 'AUTH_TOKEN', 'CLIENT_NAME', 'CLIENT_VERSION'];

const missing = requiredVars.filter((varName) => !process.env[varName]);

if (missing.length > 0) {
throw new Error(
`Missing required environment variables: ${missing.join(', ')}\n` +
'Please ensure all required variables are set in your .env file',
);
}

return {
port: Number.parseInt(process.env.PORT ?? '7001'),
apiUrl: process.env.API_URL!,
authToken: process.env.AUTH_TOKEN!,
clientName: process.env.CLIENT_NAME!,
clientVersion: process.env.CLIENT_VERSION!,
nodeEnv: process.env.NODE_ENV || 'development',
};
}

export const config = loadConfig();
64 changes: 64 additions & 0 deletions health-check-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import Fastify from 'fastify';
import { config } from './config';
import { loggingPlugin } from './plugins/logging';
import { errorHandlerPlugin } from './plugins/errorHandler';
import { driveRoutes } from './routes/drive';

async function start() {
const fastify = Fastify({
logger: {
level: config.nodeEnv === 'development' ? 'info' : 'warn',
transport:
config.nodeEnv === 'development'
? {
target: 'pino-pretty',
options: {
translateTime: 'HH:MM:ss Z',
ignore: 'pid,hostname',
},
}
: undefined,
},
});

try {
await fastify.register(loggingPlugin);
await fastify.register(errorHandlerPlugin);

await fastify.register(driveRoutes);

fastify.get('/health', async () => {
return {
status: 'healthy',
service: 'health-check-server',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
};
});

await fastify.listen({
port: config.port,
host: '0.0.0.0',
});

fastify.log.info('Health check server started successfully');
fastify.log.info(`Listening on port ${config.port}`);
fastify.log.info(`API URL: ${config.apiUrl}`);
fastify.log.info(`Environment: ${config.nodeEnv}`);
} catch (error) {
fastify.log.error(error);
process.exit(1);
}
}

process.on('SIGINT', () => {
process.stdout.write('\nShutting down gracefully...\n');
process.exit(0);
});

process.on('SIGTERM', () => {
process.stdout.write('\nShutting down gracefully...\n');
process.exit(0);
});

start();
30 changes: 30 additions & 0 deletions health-check-server/src/plugins/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { FastifyInstance, FastifyError, FastifyRequest, FastifyReply } from 'fastify';

/**
* Error handler plugin for Fastify
* Transforms errors into standardized health check responses
*/
export async function errorHandlerPlugin(fastify: FastifyInstance) {
fastify.setErrorHandler(async (error: FastifyError, request: FastifyRequest, reply: FastifyReply) => {
request.log.error(
{
error: error.message,
stack: error.stack,
url: request.url,
method: request.method,
},
'Error occurred during request',
);

const statusCode = error.statusCode ?? 503;

return reply.status(statusCode).send({
status: 'unhealthy',
endpoint: request.url,
timestamp: new Date().toISOString(),
error: error.message,
});
});

fastify.log.info('Error handler plugin registered');
}
32 changes: 32 additions & 0 deletions health-check-server/src/plugins/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';

/**
* Logging plugin for Fastify
* Logs all incoming requests and their responses
*/
export async function loggingPlugin(fastify: FastifyInstance) {
fastify.addHook('onRequest', async (request: FastifyRequest) => {
request.log.info(
{
method: request.method,
url: request.url,
userAgent: request.headers['user-agent'],
},
'Incoming request',
);
});

fastify.addHook('onResponse', async (request: FastifyRequest, reply: FastifyReply) => {
request.log.info(
{
method: request.method,
url: request.url,
statusCode: reply.statusCode,
responseTime: reply.elapsedTime,
},
'Request completed',
);
});

fastify.log.info('Logging plugin registered');
}
16 changes: 16 additions & 0 deletions health-check-server/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export interface HealthCheckResponse {
status: 'healthy' | 'unhealthy';
endpoint: string;
timestamp: string;
responseTime?: number;
error?: string;
}

export interface Config {
port: number;
apiUrl: string;
authToken: string;
clientName: string;
clientVersion: string;
nodeEnv: string;
}
19 changes: 19 additions & 0 deletions health-check-server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"module": "commonjs",
"target": "ES2022",
"lib": ["ES2022"],
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true
},
"include": ["src/**/*", "../src/**/*"],
"exclude": ["node_modules", "dist"]
}
Loading
Loading