Spaces:
Sleeping
Sleeping
Download apps/api/src/index.ts from raihan013/eduboard-api: direct link, hf CLI and curl.
- Browser
- Download file 2.92 kB
-
https://huggingface.co/spaces/raihan013/eduboard-api/resolve/main/apps/api/src/index.ts
- Command line
-
hf download hf://spaces/raihan013/eduboard-api/apps/api/src/index.ts
-
curl -L -o index.ts https://huggingface.co/spaces/raihan013/eduboard-api/resolve/main/apps/api/src/index.ts
2.92 kB
| import Fastify from "fastify"; | |
| import cors from "@fastify/cors"; | |
| import { Server } from "socket.io"; | |
| import { prisma } from "@repo/db"; | |
| import * as dotenv from "dotenv"; | |
| import * as path from "path"; | |
| import fastifyJwt from "@fastify/jwt"; | |
| import authRoutes from "./routes/auth"; | |
| import questionRoutes from "./routes/questions"; | |
| import roomRoutes from "./routes/rooms"; | |
| import { questionSetRoutes } from "./routes/questionSets"; | |
| import { handleSocketEvents } from "./socket/gameManager"; | |
| // βββ Load Environment Variables βββββββββββββββββββββββββββββββββββββββββββββββ | |
| // Priority: apps/api/.env β root .env (fallback for local dev) | |
| dotenv.config({ path: path.resolve(__dirname, "../.env") }); | |
| dotenv.config({ path: path.resolve(__dirname, "../../../.env") }); | |
| // βββ Validate Required Environment Variables ββββββββββββββββββββββββββββββββββ | |
| const jwtSecret = process.env.JWT_SECRET; | |
| if (!jwtSecret) { | |
| console.error( | |
| "[FATAL] JWT_SECRET environment variable is not set. " + | |
| "Generate one with: openssl rand -base64 32" | |
| ); | |
| process.exit(1); | |
| } | |
| const port = parseInt(process.env.PORT || "4000", 10); | |
| // CORS_ORIGIN: comma-separated list of allowed origins for production. | |
| // Example: "https://eduboard.vercel.app,https://www.eduboard.com" | |
| // In development, defaults to localhost:3000. | |
| const corsOrigins: string | string[] | boolean = process.env.CORS_ORIGIN | |
| ? process.env.CORS_ORIGIN.split(",").map((o) => o.trim()) | |
| : "http://localhost:3000"; | |
| // βββ Fastify Instance βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| const fastify = Fastify({ logger: true }); | |
| // Register Plugins | |
| fastify.register(cors, { | |
| origin: true, | |
| credentials: true, | |
| methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], | |
| }); | |
| fastify.register(fastifyJwt, { | |
| secret: jwtSecret, | |
| }); | |
| fastify.get("/health", async () => { | |
| return { status: "ok" }; | |
| }); | |
| fastify.register(authRoutes, { prefix: "/api/auth" }); | |
| fastify.register(questionRoutes, { prefix: "/api/questions" }); | |
| fastify.register(roomRoutes, { prefix: "/api/rooms" }); | |
| fastify.register(questionSetRoutes, { prefix: "/api/sets" }); | |
| const start = async () => { | |
| try { | |
| await fastify.listen({ port, host: "0.0.0.0" }); | |
| // Initialize Socket.io attached to Fastify's raw server | |
| const io = new Server(fastify.server, { | |
| cors: { | |
| origin: corsOrigins, | |
| methods: ["GET", "POST"], | |
| }, | |
| }); | |
| io.on("connection", (socket) => { | |
| handleSocketEvents(io, socket); | |
| }); | |
| console.log(`Server and Socket.io started on port ${port}`); | |
| } catch (err) { | |
| fastify.log.error(err); | |
| process.exit(1); | |
| } | |
| }; | |
| start(); | |