diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..1c7f79b7 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +**/.env +**/node_modules diff --git a/apps/backend/Dockerfile b/apps/backend/Dockerfile new file mode 100644 index 00000000..ea621040 --- /dev/null +++ b/apps/backend/Dockerfile @@ -0,0 +1,32 @@ +FROM --platform=linux/amd64 oven/bun:1.0.5-slim + +ARG DATABASE_URL +ENV DATABASE_URL=$DATABASE_URL + +ENV PORT=3000 + +ENV NODE_ENV=production + +# For prisma +ARG NODE_VERSION=18 +RUN apt update \ + && apt install -y curl \ + && curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n \ + && bash n $NODE_VERSION \ + && rm n \ + && npm install -g n + +WORKDIR /app +COPY package.json package.json +COPY apps/backend/package.json apps/backend/package.json +COPY packages/types/package.json packages/types/package.json +COPY bun.lockb bun.lockb +RUN bun install +COPY . . + +WORKDIR /app/apps/backend +RUN bunx prisma generate + +EXPOSE ${PORT} +CMD ["bun", "start"] + diff --git a/apps/backend/src/app.ts b/apps/backend/src/app.ts index 3777d175..2cc04a7f 100644 --- a/apps/backend/src/app.ts +++ b/apps/backend/src/app.ts @@ -3,39 +3,19 @@ import events from './routes/events' import profiles from './routes/profiles' import { authMiddleware } from "./middlewares/auth"; import morgan from 'morgan' -import { db } from "./db"; -import { hashPassword } from "./services/password"; -import { v4 as uuid } from 'uuid'; const app = express(); -const port = 8080; +const port = process.env.PORT || 8080; app.use(morgan('tiny')) app.use(express.json()); app.use(express.json()); app.use(authMiddleware) - - app.use(events) app.use(profiles) app.get("/ping", (req, res) => res.json("pong")); + app.listen(port, () => { console.log(`Listening on port ${port}...`); -}); - -// async function main() { -// const secret = uuid() -// await db.client.create({ -// data: { -// project_id: 'eed345ae-2772-42e5-b989-e36e09c5febc', -// name: 'test', -// secret: await hashPassword(secret), -// } -// }) -// console.log('Your secret is', secret); - - -// } - -// main() \ No newline at end of file +}); \ No newline at end of file diff --git a/apps/backend/src/middlewares/auth.ts b/apps/backend/src/middlewares/auth.ts index d7517105..d4d239af 100644 --- a/apps/backend/src/middlewares/auth.ts +++ b/apps/backend/src/middlewares/auth.ts @@ -1,6 +1,5 @@ import { NextFunction, Request, Response } from "express" import { db } from "../db" -import { verifyPassword } from "../services/password" export async function authMiddleware(req: Request, res: Response, next: NextFunction) { const secret = req.headers['mixan-client-secret'] as string | undefined diff --git a/apps/backend/src/responses/errors.ts b/apps/backend/src/responses/errors.ts index 3889abec..1da9b6f7 100644 --- a/apps/backend/src/responses/errors.ts +++ b/apps/backend/src/responses/errors.ts @@ -1,4 +1,8 @@ -import { MixanIssue, MixanIssuesResponse } from "@mixan/types"; +import { + MixanIssue, + MixanErrorResponse, + MixanIssuesResponse, +} from '@mixan/types' export function issues(arr: Array): MixanIssuesResponse { return { @@ -7,7 +11,30 @@ export function issues(arr: Array): MixanIssuesResponse { field: item.field, message: item.message, value: item.value, - }; - }) + } + }), } -} \ No newline at end of file +} + +export function makeError(error: unknown): MixanErrorResponse { + if (error instanceof Error) { + return { + code: 'Error', + message: error.message, + } + } + + // @ts-ignore + if ('message' in error) { + return { + code: 'UnknownError', + // @ts-ignore + message: error.message, + } + } + + return { + code: 'UnknownError', + message: 'Unknown error', + } +} diff --git a/apps/backend/src/routes/events.ts b/apps/backend/src/routes/events.ts index 4d22a977..3e010740 100644 --- a/apps/backend/src/routes/events.ts +++ b/apps/backend/src/routes/events.ts @@ -4,14 +4,19 @@ import { MixanRequest } from '../types/express'; import { EventPayload } from '@mixan/types'; import { getEvents, getProfileIdFromEvents } from '../services/event'; import { success } from '../responses/success'; +import { makeError } from '../responses/errors'; const router = Router(); type PostRequest = MixanRequest> router.get('/events', async (req, res) => { - const events = await getEvents(req.client.project_id) - res.json(success(events)) + try { + const events = await getEvents(req.client.project_id) + res.json(success(events)) + } catch (error) { + res.json(makeError(error)) + } }) router.post('/events', async (req: PostRequest, res) => { diff --git a/bun.lockb b/bun.lockb index 9accf375..8d6721b7 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index e5872eca..518e0929 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,6 @@ "name": "@mixan/root", "version": "1.0.0", "workspaces": ["apps/*", "packages/*"], - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, "keywords": [], "author": "", "license": "ISC",