From 09ae2e1d08b1644b7ee6557d37bf32a2f6ade70f Mon Sep 17 00:00:00 2001 From: Kevin Etchells Date: Thu, 14 Nov 2024 13:58:50 +0000 Subject: [PATCH] Add sveltekit_app to Docker --- docker-compose.yml | 14 ++++++++++++++ sveltekit_app/Dockerfile | 15 +++++++++++++++ sveltekit_app/src/routes/+layout.server.js | 19 +++++++++++++------ sveltekit_app/svelte.config.js | 6 ++---- 4 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 sveltekit_app/Dockerfile diff --git a/docker-compose.yml b/docker-compose.yml index 697c15489..2a95ccc06 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -35,6 +35,20 @@ services: timeout: 30s retries: 24 start_period: 60s + sveltekit-app: + image: sveltekit-app:latest + build: + context: . + dockerfile: ./sveltekit_app/Dockerfile + user: "node" + networks: + - redbox-app-network + ports: + - "3000:3000" + restart: unless-stopped + env_file: + - path: .env + required: false db: image: postgres:13 env_file: diff --git a/sveltekit_app/Dockerfile b/sveltekit_app/Dockerfile new file mode 100644 index 000000000..20ef2cb44 --- /dev/null +++ b/sveltekit_app/Dockerfile @@ -0,0 +1,15 @@ +FROM node:20 + +WORKDIR /app + +COPY sveltekit_app/package.json ./ +RUN npm install + +COPY sveltekit_app/ ./ +RUN npm run build + +USER node:node + +EXPOSE 3000 + +CMD ["node", "build/index.js"] diff --git a/sveltekit_app/src/routes/+layout.server.js b/sveltekit_app/src/routes/+layout.server.js index 6a4f1a87c..6200e2b61 100644 --- a/sveltekit_app/src/routes/+layout.server.js +++ b/sveltekit_app/src/routes/+layout.server.js @@ -4,12 +4,19 @@ export async function load({ request, cookies }) { const API_URL = "http://localhost:8091/api/v0?format=json"; - const userRequest = await fetch(API_URL, { - headers: { - cookie: `sessionid=${cookies.get("sessionid")}`, - }, - }); - const userData = userRequest.ok ? await userRequest.json() : {}; + let userData = {}; + + try { + const userRequest = await fetch(API_URL, { + headers: { + cookie: `sessionid=${cookies.get("sessionid")}`, + }, + }); + userData = userRequest.ok ? await userRequest.json() : {}; + } catch (err) { + console.log("Error connecting to Django", err); + } + const publicRoutes = [ "/", diff --git a/sveltekit_app/svelte.config.js b/sveltekit_app/svelte.config.js index 10c4eeb27..ea24ec269 100644 --- a/sveltekit_app/svelte.config.js +++ b/sveltekit_app/svelte.config.js @@ -1,11 +1,9 @@ -import adapter from '@sveltejs/adapter-auto'; +//import adapter from '@sveltejs/adapter-auto'; +import adapter from '@sveltejs/adapter-node'; /** @type {import('@sveltejs/kit').Config} */ const config = { kit: { - // adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list. - // If your environment is not supported, or you settled on a specific environment, switch out the adapter. - // See https://svelte.dev/docs/kit/adapters for more information about adapters. adapter: adapter() } };