perplexity-hackathon-LawMitra / perplexity_hackathon / src / index.js
index.js
Raw
import dotenv from 'dotenv';
import express from 'express';
import cors from 'cors';
import morgan from 'morgan';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// Load environment variables
dotenv.config();

const app = express();

// Create required directories
const uploadsDir = path.join(__dirname, '../tmp/uploads');
const ttsDir = path.join(__dirname, '../tmp/tts');
fs.mkdirSync(uploadsDir, { recursive: true });
fs.mkdirSync(ttsDir, { recursive: true });

// Middleware
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(morgan('dev'));

// Routes
app.get('/', (req, res) => {
  res.json({ 
    message: 'Welcome to the API',
  });
});

// Error handling middleware
app.use((err, req, res, next) => {
  const status = err.status || 500;
  const message = err.message || 'Something went wrong';

  res.status(status).json({
    success: false,
    status,
    message,
    stack: process.env.NODE_ENV === 'development' ? err.stack : undefined,
  });
});

const port = process.env.PORT || 3000;

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
  console.log('Environment variables loaded:');
  console.log('- PERPLEXITY_API_KEY:', process.env.PERPLEXITY_API_KEY ? '✓' : '✗');
  console.log('- OPENAI_API_KEY:', process.env.OPENAI_API_KEY ? '✓' : '✗');
  console.log('- ELEVENLABS_API_KEY:', process.env.ELEVENLABS_API_KEY ? '✓' : '✗');
  console.log('- TWILIO_ACCOUNT_SID:', process.env.TWILIO_ACCOUNT_SID ? '✓' : '✗');
  console.log('- TWILIO_AUTH_TOKEN:', process.env.TWILIO_AUTH_TOKEN ? '✓' : '✗');
  console.log('- TWILIO_PHONE_NUMBER:', process.env.TWILIO_PHONE_NUMBER ? '✓' : '✗');
});