2026-03-04 20:31:18 +01:00
|
|
|
import Fastify, { FastifyRequest, FastifyReply } from "fastify";
|
2026-03-04 18:07:18 +01:00
|
|
|
import fastifyCookie from "@fastify/cookie";
|
|
|
|
|
import fastifyCors from "@fastify/cors";
|
|
|
|
|
import fastifyMultipart from "@fastify/multipart";
|
|
|
|
|
import fastifyStatic from "@fastify/static";
|
|
|
|
|
import { createYoga } from "graphql-yoga";
|
2026-03-04 20:40:22 +01:00
|
|
|
import { eq } from "drizzle-orm";
|
|
|
|
|
import { files } from "./db/schema/index";
|
2026-03-04 18:07:18 +01:00
|
|
|
import path from "path";
|
2026-03-04 18:42:58 +01:00
|
|
|
import { schema } from "./graphql/index";
|
|
|
|
|
import { buildContext } from "./graphql/context";
|
|
|
|
|
import { db } from "./db/connection";
|
|
|
|
|
import { redis } from "./lib/auth";
|
2026-03-04 18:07:18 +01:00
|
|
|
|
|
|
|
|
const PORT = parseInt(process.env.PORT || "4000");
|
|
|
|
|
const UPLOAD_DIR = process.env.UPLOAD_DIR || "/data/uploads";
|
|
|
|
|
const CORS_ORIGIN = process.env.CORS_ORIGIN || "http://localhost:3000";
|
|
|
|
|
|
2026-03-04 18:42:58 +01:00
|
|
|
async function main() {
|
|
|
|
|
const fastify = Fastify({
|
|
|
|
|
logger: {
|
|
|
|
|
level: process.env.LOG_LEVEL || "info",
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-03-04 18:07:18 +01:00
|
|
|
|
2026-03-04 18:42:58 +01:00
|
|
|
await fastify.register(fastifyCookie, {
|
|
|
|
|
secret: process.env.COOKIE_SECRET || "change-me-in-production",
|
|
|
|
|
});
|
2026-03-04 18:07:18 +01:00
|
|
|
|
2026-03-04 18:42:58 +01:00
|
|
|
await fastify.register(fastifyCors, {
|
|
|
|
|
origin: CORS_ORIGIN,
|
|
|
|
|
credentials: true,
|
|
|
|
|
methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
|
|
|
|
|
});
|
2026-03-04 18:07:18 +01:00
|
|
|
|
2026-03-04 18:42:58 +01:00
|
|
|
await fastify.register(fastifyMultipart, {
|
|
|
|
|
limits: {
|
|
|
|
|
fileSize: 5 * 1024 * 1024 * 1024, // 5 GB
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-03-04 18:07:18 +01:00
|
|
|
|
2026-03-04 20:40:22 +01:00
|
|
|
// fastify-static provides reply.sendFile(); files are stored as <UPLOAD_DIR>/<id>/<filename>
|
2026-03-04 18:42:58 +01:00
|
|
|
await fastify.register(fastifyStatic, {
|
|
|
|
|
root: path.resolve(UPLOAD_DIR),
|
|
|
|
|
prefix: "/assets/",
|
2026-03-04 20:40:22 +01:00
|
|
|
serve: false, // disable auto-serving; we use a custom route below
|
|
|
|
|
decorateReply: true,
|
2026-03-04 18:42:58 +01:00
|
|
|
});
|
2026-03-04 18:07:18 +01:00
|
|
|
|
2026-03-04 20:31:18 +01:00
|
|
|
const yoga = createYoga<{ req: FastifyRequest; reply: FastifyReply; db: typeof db; redis: typeof redis }>({
|
2026-03-04 18:42:58 +01:00
|
|
|
schema,
|
|
|
|
|
context: buildContext,
|
|
|
|
|
graphqlEndpoint: "/graphql",
|
|
|
|
|
healthCheckEndpoint: "/health",
|
|
|
|
|
logging: {
|
2026-03-04 20:31:18 +01:00
|
|
|
debug: (...args) => args.forEach((arg) => fastify.log.debug(arg)),
|
|
|
|
|
info: (...args) => args.forEach((arg) => fastify.log.info(arg)),
|
|
|
|
|
warn: (...args) => args.forEach((arg) => fastify.log.warn(arg)),
|
|
|
|
|
error: (...args) => args.forEach((arg) => fastify.log.error(arg)),
|
2026-03-04 18:42:58 +01:00
|
|
|
},
|
|
|
|
|
});
|
2026-03-04 18:07:18 +01:00
|
|
|
|
2026-03-04 18:42:58 +01:00
|
|
|
fastify.route({
|
|
|
|
|
url: "/graphql",
|
|
|
|
|
method: ["GET", "POST", "OPTIONS"],
|
2026-03-04 20:31:18 +01:00
|
|
|
handler: (req, reply) =>
|
|
|
|
|
yoga.handleNodeRequestAndResponse(req, reply, { req, reply, db, redis }),
|
2026-03-04 18:42:58 +01:00
|
|
|
});
|
2026-03-04 18:07:18 +01:00
|
|
|
|
2026-03-04 20:40:22 +01:00
|
|
|
// Serve uploaded files: GET /assets/:id
|
|
|
|
|
// Files are stored as <UPLOAD_DIR>/<id>/<filename> — look up filename in DB
|
|
|
|
|
fastify.get("/assets/:id", async (request, reply) => {
|
|
|
|
|
const { id } = request.params as { id: string };
|
|
|
|
|
|
|
|
|
|
const result = await db
|
|
|
|
|
.select({ filename: files.filename, mime_type: files.mime_type })
|
|
|
|
|
.from(files)
|
|
|
|
|
.where(eq(files.id, id))
|
|
|
|
|
.limit(1);
|
|
|
|
|
|
|
|
|
|
if (!result[0]) return reply.status(404).send({ error: "File not found" });
|
|
|
|
|
|
|
|
|
|
const { filename, mime_type } = result[0];
|
|
|
|
|
reply.header("Content-Type", mime_type);
|
|
|
|
|
reply.header("Cache-Control", "public, max-age=31536000, immutable");
|
|
|
|
|
return reply.sendFile(path.join(id, filename));
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-04 18:42:58 +01:00
|
|
|
fastify.get("/health", async (_request, reply) => {
|
|
|
|
|
return reply.send({ status: "ok", timestamp: new Date().toISOString() });
|
|
|
|
|
});
|
2026-03-04 18:07:18 +01:00
|
|
|
|
2026-03-04 18:42:58 +01:00
|
|
|
try {
|
|
|
|
|
await fastify.listen({ port: PORT, host: "0.0.0.0" });
|
|
|
|
|
fastify.log.info(`Backend running at http://0.0.0.0:${PORT}`);
|
|
|
|
|
fastify.log.info(`GraphQL at http://0.0.0.0:${PORT}/graphql`);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
fastify.log.error(err);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
2026-03-04 18:07:18 +01:00
|
|
|
}
|
2026-03-04 18:42:58 +01:00
|
|
|
|
|
|
|
|
main().catch((err) => {
|
|
|
|
|
console.error("Fatal error:", err);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
});
|