add docker
This commit is contained in:
2
.dockerignore
Normal file
2
.dockerignore
Normal file
@@ -0,0 +1,2 @@
|
||||
**/.env
|
||||
**/node_modules
|
||||
32
apps/backend/Dockerfile
Normal file
32
apps/backend/Dockerfile
Normal 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"]
|
||||
|
||||
@@ -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()
|
||||
});
|
||||
@@ -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
|
||||
|
||||
@@ -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',
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user