Skip to content

Instantly share code, notes, and snippets.

@maietta
Last active September 18, 2024 16:45
Show Gist options
  • Save maietta/1a42e6850bf4ec255196736bf21b8d82 to your computer and use it in GitHub Desktop.
Save maietta/1a42e6850bf4ec255196736bf21b8d82 to your computer and use it in GitHub Desktop.
Run SvelteKit with Bun
ARG BUN_VERSION=1.1.27
FROM oven/bun:${BUN_VERSION} AS builder
# Install required dependencies
RUN apt-get update && \
apt-get install -y \
curl \
xz-utils \
&& rm -rf /var/lib/apt/lists/*
# Set the Node.js version
ENV NODE_VERSION=20.15.0
# Download and install Node.js
RUN curl -fsSL https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.xz -o node-v${NODE_VERSION}-linux-x64.tar.xz && \
tar -xf node-v${NODE_VERSION}-linux-x64.tar.xz && \
mv node-v${NODE_VERSION}-linux-x64 /usr/local/node && \
ln -s /usr/local/node/bin/node /usr/local/bin/node && \
ln -s /usr/local/node/bin/npm /usr/local/bin/npm && \
ln -s /usr/local/node/bin/npx /usr/local/bin/npx && \
rm node-v${NODE_VERSION}-linux-x64.tar.xz
# Verify installation
RUN node --version && npm --version
WORKDIR /app
ENV NODE_ENV=production
COPY --link bun.lockb package.json ./
RUN bun install --ci
# Generate Prisma Client
# COPY --link prisma .
# RUN bun x prisma generate
# Copy application code
COPY --link . .
# Build application
RUN bun run build
# Build the final image
FROM oven/bun:${BUN_VERSION}
# Copy Node.js binaries and curl from the builder stage
COPY --from=builder /usr/local/node /usr/local/node
COPY --from=builder /usr/bin/curl /usr/bin/curl
COPY --from=builder /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu
# Create symbolic links to Node.js binaries
RUN ln -s /usr/local/node/bin/node /usr/local/bin/node && \
ln -s /usr/local/node/bin/npm /usr/local/bin/npm && \
ln -s /usr/local/node/bin/npx /usr/local/bin/npx
# Copy the built application and necessary files from the builder stage
COPY --from=builder /app /app
# Set environment variables and expose the port
ENV PORT=3000
EXPOSE 3000/tcp
# Set user and define healthcheck
USER bun
HEALTHCHECK --interval=5s --timeout=5s --start-period=5s --retries=3 CMD curl --fail http://localhost:3000/healthcheck
CMD ["bun", "/app/build/index.js"]
import { createContext } from '$lib/tRPC/context';
import { router } from '$lib/tRPC/router';
import type { Handle } from '@sveltejs/kit';
import { createTRPCHandle } from 'trpc-sveltekit';
import { sequence } from '@sveltejs/kit/hooks';
import { market} from '$lib/pocketbase';
const pocketbase: Handle = async ({ event, resolve }: { event: any; resolve: any }) => {
event.locals.incomming = market;
event.locals.incomming.authStore.loadFromCookie(event.request.headers.get('cookie') || '');
try {
event.locals.incomming.authStore.isValid &&
(await event.locals.incomming.collection('users').authRefresh());
event.locals.user = event.locals.incomming.authStore?.model;
} catch (_) {
event.locals.incomming.authStore.clear();
}
const response = await resolve(event);
const isProd = process.env.NODE_ENV === 'production' ? true : false;
response.headers.append(
'set-cookie',
event.locals.incomming.authStore.exportToCookie({
secure: isProd,
sameSite: 'Lax',
httpOnly: false
})
);
return response;
};
const healthcheck: Handle = async ({ event, resolve }) => {
if (event.url.pathname === '/healthcheck') {
return new Response('OK', {
headers: {
'Content-Type': 'text/plain'
}
});
}
return resolve(event);
};
const tRPC: Handle = createTRPCHandle({ router, createContext });
export const handle = sequence(healthcheck, pocketbase, tRPC);
import adapter from 'svelte-adapter-bun'; // Installed with `bun add -D svelte-adapter-bun`
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
// for more information about preprocessors
preprocess: vitePreprocess(),
kit: {
// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
adapter: adapter(),
crsf: {
checkOrigin: false // This fixes CORS issues for form submissions if behind a proxy.
}
};
export default config;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment