feat(infra): reworked Dockerfile

- changed from npm to pnpm in Dockerfile => improved install time
    Fixed errors from change in .ts files.
This commit is contained in:
Maieul BOYER 2025-08-03 14:40:48 +02:00
parent 704ce37909
commit 5f9fd5629c
7 changed files with 51 additions and 35 deletions

View file

@ -6,7 +6,7 @@
// By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ // // By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ //
// +#+#+#+#+#+ +#+ // // +#+#+#+#+#+ +#+ //
// Created: 2025/07/28 17:36:22 by maiboyer #+# #+# // // Created: 2025/07/28 17:36:22 by maiboyer #+# #+# //
// Updated: 2025/07/30 21:19:05 by maiboyer ### ########.fr // // Updated: 2025/08/03 13:36:25 by maiboyer ### ########.fr //
// // // //
// ************************************************************************** // // ************************************************************************** //
@ -66,20 +66,6 @@ export class Database {
this.st.set(query, st); this.st.set(query, st);
return st; return st;
} }
public createUser(user: string): Result<UUIDv7, DBUserExists> {
const st = this.prepare('INSERT INTO users VALUES (?, ?) RETURNING id');
try {
st.get(newUUIDv7(), user)
}
catch (e: any) {
console.log(e)
console.log(typeof e)
}
return Result.ok(newUUIDv7());
}
} }
// When using .decorate you have to specify added properties for Typescript // When using .decorate you have to specify added properties for Typescript
@ -96,8 +82,7 @@ export type DatabaseOption = {
export const uDatabase = fp<DatabaseOption>(async function( export const uDatabase = fp<DatabaseOption>(async function(
_fastify: FastifyInstance, _fastify: FastifyInstance,
_options: DatabaseOption) { _options: DatabaseOption) {
console.log("Database has been hooked up to fastify ?!");
}); });
export default uDatabase; export default uDatabase;

View file

@ -1,29 +1,34 @@
FROM node:24-alpine as builder FROM guergeiro/pnpm:22-10-alpine as builder
RUN apk add jq;
ARG SERVICE ARG SERVICE
RUN apk add jq python3 musl-dev gcc g++ make;
WORKDIR /build WORKDIR /build
COPY ./@shared/package.json /build/@shared/package.json COPY ./@shared/package.json /build/@shared/package.json
COPY ./${SERVICE}/package.json /build/service/package.json COPY ./${SERVICE}/package.json /build/service/package.json
COPY ./${SERVICE}/package.json /build/package.json
COPY ./tsconfig.base.json /build/tsconfig.base.json COPY ./tsconfig.base.json /build/tsconfig.base.json
COPY ./pnpm-workspace.yaml /build/pnpm-workspace.yaml
RUN echo "{\"name\":\"workspace\", \"workspaces\": [\"@shared\", \"${SERVICE}\" ]}" | jq . >/build/package.json; RUN pnpm install;
RUN npm install && npm install --prefix=/build/service; RUN ls -a *;
COPY ./@shared/ /build/@shared/ COPY ./@shared/ /build/@shared/
COPY ./${SERVICE}/ /build/service/ COPY ./${SERVICE}/ /build/service/
RUN (cd /build/service && npm run build:prod) \ RUN cd /build/service && \
&& jq -s '.[0] * .[1]' /build/@shared/package.json /build/service/package.json >/dist/package.json; pnpm run build:prod && \
jq -s '.[0] * .[1]' /build/*/package.json >/dist/package.json && \
cp /build/pnpm-workspace.yaml /dist/pnpm-workspace.yaml && \
true;
FROM node:24
FROM guergeiro/pnpm:22-10-slim
WORKDIR /src WORKDIR /src
COPY --from=builder /dist /src COPY --from=builder /dist /src
RUN npm install; RUN pnpm install --prod;
CMD ["node", "/src/run.cjs"] CMD ["node", "/src/run.cjs"]

View file

@ -3,8 +3,11 @@ import fastifyFormBody from '@fastify/formbody'
import fastifyMultipart from '@fastify/multipart' import fastifyMultipart from '@fastify/multipart'
import { mkdir } from 'node:fs/promises' import { mkdir } from 'node:fs/promises'
import fp from 'fastify-plugin' import fp from 'fastify-plugin'
import * as db from '@shared/database'
// @ts-ignore: import.meta.glob is a vite thing. Typescript doesn't know this...
const plugins = import.meta.glob('./plugins/**/*.ts', { eager: true }); const plugins = import.meta.glob('./plugins/**/*.ts', { eager: true });
// @ts-ignore: import.meta.glob is a vite thing. Typescript doesn't know this...
const routes = import.meta.glob('./routes/**/*.ts', { eager: true }); const routes = import.meta.glob('./routes/**/*.ts', { eager: true });
@ -21,13 +24,13 @@ const app: FastifyPluginAsync = async (
): Promise<void> => { ): Promise<void> => {
// Place here your custom code! // Place here your custom code!
for (const plugin of Object.values(plugins)) { for (const plugin of Object.values(plugins)) {
void fastify.register(plugin, {}); void fastify.register(plugin as any, {});
} }
for (const route of Object.values(routes)) { for (const route of Object.values(routes)) {
void fastify.register(route, {}); void fastify.register(route as any, {});
} }
//void fastify.register(MyPlugin, {}) void fastify.register(db.uDatabase as any, {})
void fastify.register(fastifyFormBody, {}) void fastify.register(fastifyFormBody, {})
void fastify.register(fastifyMultipart, {}) void fastify.register(fastifyMultipart, {})

View file

@ -2,7 +2,6 @@ import { FastifyPluginAsync } from 'fastify'
import { join } from 'node:path' import { join } from 'node:path'
import { open } from 'node:fs/promises' import { open } from 'node:fs/promises'
import sharp from 'sharp' import sharp from 'sharp'
import { newUUIDv7 } from '@shared/uuid'
import rawBody from 'raw-body' import rawBody from 'raw-body'
const route: FastifyPluginAsync = async (fastify, opts): Promise<void> => { const route: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
@ -12,7 +11,7 @@ const route: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
// we register a route handler for: `/<USERID_HERE>` // we register a route handler for: `/<USERID_HERE>`
// it sets some configuration options, and set the actual function that will handle the request // it sets some configuration options, and set the actual function that will handle the request
fastify.addContentTypeParser('*', function(request, payload, done) { fastify.addContentTypeParser('*', function(request, payload, done: any) {
done() done()
}); });

View file

@ -2,12 +2,32 @@ import { defineConfig } from 'vite'
import tsconfigPaths from 'vite-tsconfig-paths' import tsconfigPaths from 'vite-tsconfig-paths'
import nodeExternals from 'rollup-plugin-node-externals' import nodeExternals from 'rollup-plugin-node-externals'
import path from 'node:path' import path from 'node:path'
import fs from 'node:fs'
function collectDeps(...pkgJsonPaths) {
const allDeps = new Set();
for (const pkgPath of pkgJsonPaths) {
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
for (const dep of Object.keys(pkg.dependencies || {})) {
allDeps.add(dep);
}
for (const peer of Object.keys(pkg.peerDependencies || {})) {
allDeps.add(peer);
}
}
return Array.from(allDeps);
}
const externals = collectDeps(
'./package.json',
'../@shared/package.json'
);
export default defineConfig({ export default defineConfig({
root: __dirname, // service root root: __dirname, // service root
plugins: [tsconfigPaths(), nodeExternals()], plugins: [tsconfigPaths(), nodeExternals()],
build: { build: {
ssr: true, ssr: true,
outDir: 'dist', outDir: 'dist',
emptyOutDir: true, emptyOutDir: true,
@ -17,9 +37,9 @@ export default defineConfig({
fileName: () => 'index.js', fileName: () => 'index.js',
}, },
rollupOptions: { rollupOptions: {
external: [], external: externals,
}, },
target: 'node24', // or whatever Node version you use target: 'node22', // or whatever Node version you use
sourcemap: true, sourcemap: true,
minify: false, // for easier debugging minify: false, // for easier debugging
} }

4
src/pnpm-workspace.yaml Normal file
View file

@ -0,0 +1,4 @@
pacakges:
- ./*
nodeLinker: hoisted

View file

@ -2,7 +2,7 @@
"$schema": "https://json.schemastore.org/tsconfig", "$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": { "compilerOptions": {
"target": "ES2023", "target": "ES2023",
"module": "NodeNext", "module": "esnext",
"moduleResolution": "node", "moduleResolution": "node",
"newLine": "lf", "newLine": "lf",