67 lines
1.6 KiB
JavaScript
67 lines
1.6 KiB
JavaScript
import Fastify from 'fastify';
|
|
import cors from '@fastify/cors';
|
|
import jwt from '@fastify/jwt';
|
|
import multipart from '@fastify/multipart';
|
|
import fastifyStatic from '@fastify/static';
|
|
import { join } from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
import { dirname } from 'path';
|
|
import dotenv from 'dotenv';
|
|
import authRoutes from './routes/auth.js';
|
|
import postRoutes from './routes/posts.js';
|
|
import imageRoutes from './routes/images.js';
|
|
|
|
// Load environment variables
|
|
dotenv.config();
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = dirname(__filename);
|
|
|
|
const app = Fastify({
|
|
logger: true
|
|
});
|
|
|
|
// Register plugins
|
|
await app.register(cors, {
|
|
origin: process.env.FRONTEND_URL || 'http://localhost:3000'
|
|
});
|
|
|
|
await app.register(jwt, {
|
|
secret: process.env.JWT_SECRET || 'your-super-secret-key-change-this-in-production'
|
|
});
|
|
|
|
await app.register(multipart, {
|
|
limits: {
|
|
fileSize: 5 * 1024 * 1024 // 5MB
|
|
}
|
|
});
|
|
|
|
await app.register(fastifyStatic, {
|
|
root: join(__dirname, '../uploads'),
|
|
prefix: '/uploads/'
|
|
});
|
|
|
|
// Create uploads directory if it doesn't exist
|
|
import { mkdirSync } from 'fs';
|
|
mkdirSync(join(__dirname, '../uploads'), { recursive: true });
|
|
|
|
// Register routes
|
|
await app.register(authRoutes);
|
|
await app.register(postRoutes);
|
|
await app.register(imageRoutes);
|
|
|
|
// Start the server
|
|
const start = async () => {
|
|
try {
|
|
await app.listen({
|
|
port: process.env.PORT || 3001,
|
|
host: process.env.HOST || 'localhost'
|
|
});
|
|
console.log(`Server listening on ${app.server.address().port}`);
|
|
} catch (err) {
|
|
app.log.error(err);
|
|
process.exit(1);
|
|
}
|
|
};
|
|
|
|
start(); |