4436
Sl
sesame-labs-coding-assignment / files
sesame-labs-coding-assignment / api / src / index.ts
1.2 KB
1import cors from "@koa/cors"; 2import { PrismaClient } from "@prisma/client"; 3import dotenv from "dotenv"; 4import Koa from "koa"; 5import bodyParser from "koa-bodyparser"; 6import json from "koa-json"; 7import session from "koa-session"; 8import logger from "koa-logger"; 9import router from "./routes"; 10 11const prisma = new PrismaClient(); 12 13async function main() { 14 // Load .env: 15 dotenv.config(); 16 17 const app = new Koa(); 18 app.keys = [process.env.APP_KEYS || ""]; 19 20 // Expose prisma client instance to context: 21 app.context.prisma = prisma; 22 23 // Mount middleware: 24 app 25 .use(logger()) 26 .use(json()) 27 .use(bodyParser()) 28 .use(cors({ credentials: true })) 29 .use(session({ secure: false, signed: false, sameSite: "lax" }, app)) 30 .use(router.routes()) 31 .use(router.allowedMethods()); 32 33 // Start the server: 34 const port = process.env.PORT; 35 app.listen(port, () => { 36 console.log( 37 `Server listening on port ${port} and on environment "${process.env.ENVIRONMENT}"` 38 ); 39 }); 40} 41 42main() 43 .then(async () => { 44 await prisma.$disconnect(); 45 }) 46 .catch(async (e) => { 47 await prisma.$disconnect(); 48 console.error(e); 49 process.exit(1); 50 });