add cors + adjusting dockerfile

This commit is contained in:
Emily
2024-09-28 13:37:10 +02:00
parent 0ba44a406d
commit 33b730e66b
10 changed files with 135 additions and 50 deletions

49
dashboard/Dockerfile Normal file
View File

@@ -0,0 +1,49 @@
# Start with a minimal Node.js base image
FROM node:21-alpine AS base
# Create a distinct build environment
FROM base AS build
# Install pnpm globally with caching to avoid reinstalling if nothing has changed
RUN npm i -g pnpm
# Set the working directory
WORKDIR /home/app
# Copy only package-related files to leverage caching
COPY --link ./dashboard/package.json ./dashboard/pnpm-lock.yaml ./dashboard/
COPY --link ./lyx-ui/package.json ./lyx-ui/pnpm-lock.yaml ./lyx-ui/
COPY --link ./shared/package.json ./shared/pnpm-lock.yaml ./shared/
# Install dependencies for each package
WORKDIR /home/app/lyx-ui
RUN pnpm install --frozen-lockfile
# WORKDIR /home/app/shared
# RUN pnpm install --frozen-lockfile
WORKDIR /home/app/dashboard
RUN pnpm install --frozen-lockfile
# Now copy the rest of the source files
WORKDIR /home/app
COPY --link ./dashboard ./dashboard
COPY --link ./lyx-ui ./lyx-ui
COPY --link ./shared ./shared
# Build the dashboard
WORKDIR /home/app/dashboard
RUN pnpm run build
# Use a smaller base image for the final production build
FROM node:21-alpine AS production
# Set the working directory for the production container
WORKDIR /home/app
# Copy the built application from the build stage
COPY --from=build /home/app/dashboard/.output /home/app/.output
# Start the application
CMD ["node", "/home/app/.output/server/index.mjs"]

View File

@@ -10,7 +10,8 @@
"postinstall": "nuxt prepare",
"test": "vitest",
"docker-build": "docker build -t litlyx-dashboard -f Dockerfile ../",
"docker-inspect": "docker run -it litlyx-dashboard sh"
"docker-inspect": "docker run -it litlyx-dashboard sh",
"docker-run": "docker run -p 3000:3000 --name litlyx-dashboard litlyx-dashboard"
},
"dependencies": {
"@getbrevo/brevo": "^2.2.0",

View File

@@ -1,9 +1,12 @@
import { checkApiKey, checkAuthorization, eventsListApi } from '~/server/services/ApiService';
import { useCors } from '~/server/utils/useCors';
export default defineEventHandler(async event => {
useCors(event);
const { rows, from, to, limit } = await readBody(event);
const token = checkAuthorization(event);

View File

@@ -1,9 +1,12 @@
import { checkApiKey, checkAuthorization, eventsListApi, } from '~/server/services/ApiService';
import { useCors } from '~/server/utils/useCors';
export default defineEventHandler(async event => {
useCors(event);
const { row, from, to, limit } = getQuery(event);
const token = checkAuthorization(event);

View File

@@ -1,10 +1,13 @@
import { checkApiKey, checkAuthorization } from '~/server/services/ApiService';
import { visitsListApi } from '../../services/ApiService';
import { useCors } from '~/server/utils/useCors';
export default defineEventHandler(async event => {
useCors(event);
const { rows, from, to, limit } = await readBody(event);
const token = checkAuthorization(event);

View File

@@ -2,9 +2,12 @@
import { ApiSettingsModel } from '@schema/ApiSettingsSchema';
import { VisitModel } from '@schema/metrics/VisitSchema';
import { checkApiKey, checkAuthorization, visitsListApi } from '~/server/services/ApiService';
import { useCors } from '~/server/utils/useCors';
export default defineEventHandler(async event => {
useCors(event);
const { row, from, to, limit } = getQuery(event);
const token = checkAuthorization(event);

View File

@@ -0,0 +1,10 @@
import type { H3Event, EventHandlerRequest } from 'h3';
export function useCors(event: H3Event<EventHandlerRequest>) {
setResponseHeader(event, 'Access-Control-Allow-Origin', '*');
setResponseHeader(event, 'Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
setResponseHeader(event, 'Access-Control-Allow-Headers', 'Content-Type, Authorization');
}