Skip to content
Open
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
48 changes: 24 additions & 24 deletions api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ let dailyUsageTracker;
async function initializeServices() {
try {
logger.info('Initializing services...');

// Initialize daily usage tracker
dailyUsageTracker = new DailyUsageTracker();
await dailyUsageTracker.initialize();

// Initialize documentation index
documentationIndex = new DocumentationIndex();
await documentationIndex.initialize();

// Initialize chat service with documentation context
chatService = new ChatService({
documentationIndex,
Expand All @@ -72,7 +72,7 @@ async function initializeServices() {
projectId: config.llm.projectId,
location: config.llm.location
});

logger.info('Services initialized successfully');
} catch (error) {
logger.error('Failed to initialize services:', error);
Expand All @@ -85,7 +85,7 @@ app.get('/api/health', async (req, res) => {
try {
const currentUsage = dailyUsageTracker ? await dailyUsageTracker.getCurrentUsage() : 0;
const remainingUsage = dailyUsageTracker ? await dailyUsageTracker.getRemainingUsage(config.rateLimiting.dailyLimit) : config.rateLimiting.dailyLimit;

res.json({
status: 'healthy',
timestamp: new Date().toISOString(),
Expand All @@ -102,8 +102,8 @@ app.get('/api/health', async (req, res) => {
});
} catch (error) {
logger.error('Health check error:', error);
res.json({
status: 'healthy',
res.status(503).json({
status: 'degraded',
timestamp: new Date().toISOString(),
services: {
chatService: !!chatService,
Expand All @@ -129,7 +129,7 @@ const dailyLimitMiddleware = async (req, res, next) => {
// Check if daily limit exceeded
if (await dailyUsageTracker.hasExceededLimit(config.rateLimiting.dailyLimit)) {
const currentUsage = await dailyUsageTracker.getCurrentUsage();

logger.warn('Daily chat limit exceeded', {
currentUsage,
limit: config.rateLimiting.dailyLimit,
Expand Down Expand Up @@ -184,7 +184,7 @@ app.post('/api/chat',
}

const { message, context, conversationHistory } = req.body;

// Log the request (without sensitive data)
logger.info('Chat request received', {
messageLength: message.length,
Expand Down Expand Up @@ -213,7 +213,7 @@ app.post('/api/chat',
try {
const newUsageCount = await dailyUsageTracker.incrementUsage();
const remaining = await dailyUsageTracker.getRemainingUsage(config.rateLimiting.dailyLimit);

logger.info('Daily usage incremented', {
currentUsage: newUsageCount,
remaining: remaining,
Expand All @@ -237,9 +237,9 @@ app.post('/api/chat',

} catch (error) {
logger.error('Chat endpoint error:', error);

// Don't expose internal errors to clients
const errorMessage = process.env.NODE_ENV === 'production'
const errorMessage = process.env.NODE_ENV === 'production'
? 'I\'m having trouble processing your request right now. Please try again later.'
: error.message;

Expand All @@ -262,7 +262,7 @@ app.get('/api/search',
async (req, res) => {
try {
const { query, limit = 10 } = req.query;

if (!query) {
return res.status(400).json({
error: 'Query parameter is required'
Expand Down Expand Up @@ -305,7 +305,7 @@ app.get('/api/topics', async (req, res) => {
}

const topics = await documentationIndex.getTopics();

res.json({
topics,
timestamp: new Date().toISOString()
Expand All @@ -329,7 +329,7 @@ app.post('/api/admin/rebuild-index', async (req, res) => {
}

const result = await documentationIndex.rebuildIndex();

res.json({
message: 'Documentation index rebuilt successfully',
...result,
Expand All @@ -349,7 +349,7 @@ app.post('/api/admin/rebuild-index', async (req, res) => {
app.all('/webhook/rebuild-docs', async (req, res) => {
try {
const startTime = Date.now();

if (!documentationIndex) {
return res.status(503).json({
error: 'Documentation service not available'
Expand Down Expand Up @@ -396,7 +396,7 @@ app.use((err, req, res, next) => {
logger.error('Unhandled error:', err);
res.status(500).json({
error: 'Internal server error',
message: process.env.NODE_ENV === 'production'
message: process.env.NODE_ENV === 'production'
? 'Something went wrong'
: err.message
});
Expand All @@ -413,32 +413,32 @@ app.use((req, res) => {
// Graceful shutdown
process.on('SIGINT', async () => {
logger.info('Received SIGINT, shutting down gracefully...');

// Close any open connections or cleanup
if (chatService && typeof chatService.cleanup === 'function') {
await chatService.cleanup();
}


process.exit(0);
});

process.on('SIGTERM', async () => {
logger.info('Received SIGTERM, shutting down gracefully...');

if (chatService && typeof chatService.cleanup === 'function') {
await chatService.cleanup();
}


process.exit(0);
});

// Start server
async function startServer() {
try {
await initializeServices();

app.listen(PORT, () => {
logger.info(`Krkn Chatbot API server running on port ${PORT}`);
logger.info(`Environment: ${process.env.NODE_ENV || 'development'}`);
Expand Down
Loading