Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

add docker

Carl-Gerhard Lindesvärd8510d7889dd1
diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 000000000..1c7f79b73
--- /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 000000000..ea6210404
--- /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 3777d175a..2cc04a7fc 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 d75171052..d4d239afb 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 3889abecb..1da9b6f7a 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<MixanIssue>): MixanIssuesResponse {
return {
@@ -7,7 +11,30 @@ export function issues(arr: Array<MixanIssue>): 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 4d22a9771..3e0107408 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<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) => {
diff --git a/bun.lockb b/bun.lockb
index 9accf3754..8d6721b75 100755
Binary files a/bun.lockb and b/bun.lockb differ
diff --git a/package.json b/package.json
index e5872eca2..518e09290 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",