this repo has no description
at main 1.8 kB view raw
1import { cors } from "@elysiajs/cors"; 2import { swagger } from "@elysiajs/swagger"; 3import { Logger } from "common"; 4import { Elysia } from "elysia"; 5 6import cfg from "./config"; 7 8import { federationRoutes } from "./federation/routes"; 9import { routes } from "./routes"; 10import { apiError } from "./utils/apiError"; 11 12const appLogger = new Logger("app"); 13 14const app = new Elysia({ 15 cookie: { 16 secrets: [cfg.cookie.secret], 17 path: cfg.cookie.path, 18 httpOnly: cfg.cookie.httpOnly, 19 secure: cfg.server.secure, 20 sameSite: cfg.cookie.sameSite, 21 sign: ["session"], 22 }, 23}) 24 .use( 25 cors({ 26 origin: "http://localhost:5173", 27 methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], 28 allowedHeaders: ["Content-Type", "Authorization", "x-eden-request"], 29 credentials: true, 30 }), 31 ) 32 .use(swagger({ path: "/api/docs" })) 33 .onError(({ error, code }) => { 34 if (code === "NOT_FOUND") return apiError(404, "Not Found"); 35 if (code === "VALIDATION") return apiError(400, "Validation Error"); 36 if (code === "INVALID_COOKIE_SIGNATURE") 37 return apiError(403, "Invalid Cookie Signature"); 38 if (code === "INTERNAL_SERVER_ERROR") 39 return apiError(500, "Internal Server Error"); 40 if (code === "PARSE") return apiError(400, "Invalid Request"); 41 42 appLogger.error(`${code}`, error); 43 return apiError(500, "Internal Server Error"); 44 }) 45 .group("/api", (app) => { 46 return app 47 .use( 48 cors({ 49 origin: "http://localhost:5173", 50 methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], 51 allowedHeaders: ["Content-Type", "Authorization", "x-eden-request"], 52 credentials: true, 53 }), 54 ) 55 .use(routes); 56 }) 57 .use(federationRoutes); 58 59export default app;