add docker

This commit is contained in:
Carl-Gerhard Lindesvärd
2023-10-11 21:34:53 +02:00
parent 903fd155c3
commit 8510d7889d
8 changed files with 75 additions and 33 deletions

2
.dockerignore Normal file
View File

@@ -0,0 +1,2 @@
**/.env
**/node_modules

32
apps/backend/Dockerfile Normal file
View File

@@ -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"]

View File

@@ -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()
});

View File

@@ -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

View File

@@ -1,4 +1,8 @@
import { MixanIssue, MixanIssuesResponse } from "@mixan/types";
import {
MixanIssue,
MixanErrorResponse,
MixanIssuesResponse,
} from '@mixan/types'
export function issues(arr: Array<MixanIssue>): MixanIssuesResponse {
return {
@@ -7,7 +11,30 @@ export function issues(arr: Array<MixanIssue>): MixanIssuesResponse {
field: item.field,
message: item.message,
value: item.value,
};
})
}
}),
}
}
}
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',
}
}

View File

@@ -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<Array<EventPayload>>
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) => {

BIN
bun.lockb

Binary file not shown.

View File

@@ -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",