Skip to content

refactor: backend typescript rewrite #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ COPY --chown=node:node yarn.lock .
COPY --chown=node:node package.json .
COPY --chown=node:node .yarnrc.yml .
COPY --chown=node:node README.md .
COPY --chown=node:node vite.config.mjs .
COPY --chown=node:node .yarn/ .yarn/

ENTRYPOINT ["dumb-init", "--"]
Expand All @@ -25,7 +26,6 @@ FROM base as builder
ENV NODE_ENV="development"

COPY --chown=node:node tsconfig.json .
COPY --chown=node:node vite.config.ts .
COPY --chown=node:node tsup.config.ts .
COPY --chown=node:node src/ src/

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"type": "module",
"scripts": {
"start": "node .",
"dev": "yarn build && conc -n \"frontend,backend\" -c \"bgBlue,bgGreen\" \"yarn build:frontend --watch --clearScreen=false\" \"yarn build:backend --watch --silent --onSuccess 'sleep 2 && node .'\"",
"build": "conc -c \"bgBlue,bgGreen\" \"yarn:build:*\"",
"dev": "yarn build:backend --watch --onSuccess \"node . --dev\"",
"build": "concurrently -c \"bgBlue,bgGreen\" \"yarn:build:*\"",
"build:frontend": "vite build",
"build:backend": "tsup",
"clean": "rimraf dist data",
Expand All @@ -18,10 +18,10 @@
"dependencies": {
"@fastify/rate-limit": "^9.1.0",
"@fastify/sensible": "^5.5.0",
"@fastify/static": "^7.0.1",
"@fastify/swagger": "^8.14.0",
"@fastify/swagger-ui": "^3.0.0",
"@fastify/type-provider-typebox": "^4.0.0",
"@fastify/vite": "^6.0.6",
"@pnotify/core": "^5.2.0",
"@sapphire/fetch": "^3.0.2",
"@sinclair/typebox": "^0.32.15",
Expand Down
21 changes: 10 additions & 11 deletions src/backend/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ process.env.NODE_ENV ??= 'development';
import type { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
import Fastify, { type FastifyRequest } from 'fastify';
import { readFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { DocumentHandler } from './handlers/DocumentHandler.js';
import { config } from './lib/config.js';
import { rootDir } from './lib/constants.js';
Expand Down Expand Up @@ -83,6 +84,12 @@ await fastify.register(import('@fastify/swagger-ui'), {
routePrefix: '/swagger-ui'
});

await fastify.register(import('@fastify/vite'), {
root: fileURLToPath(rootDir),
dev: process.argv.includes('--dev'),
spa: true
});

// First try to match API routes
fastify.get<{ Params: SwaggerTypes.ParamsType; Reply: SwaggerTypes.RawDocumentType }>(
'/raw/:id',
Expand Down Expand Up @@ -163,22 +170,14 @@ fastify.post<{ Body: SwaggerTypes.PostBodyType; Reply: SwaggerTypes.CreatedDocum
}
);

// Otherwise, try to match static files
await fastify.register(import('@fastify/static'), {
root: new URL('dist/frontend/', rootDir),
prefix: '/',
cacheControl: true,
immutable: true,
maxAge: '3d',
wildcard: false
});

// Then we can loop back with a wildcard route, everything else should be a token,
// so route it back to sending index.html
fastify.get('*', { schema: { hide: true } }, (_, reply) => {
return reply.sendFile('index.html');
return reply.html();
});

await fastify.vite.ready();

const serverAddress = await fastify.listen({
port: config.port,
host: config.host
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["src", "vite.config.ts", "tsup.config.ts"]
"include": ["src", "vite.config.mjs", "tsup.config.ts"]
}
9 changes: 1 addition & 8 deletions vite.config.ts → vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ export default defineConfig({
build: {
outDir: fileURLToPath(new URL('dist/frontend/', import.meta.url)),
chunkSizeWarningLimit: 1_000, // 1 MB
emptyOutDir: true,
rollupOptions: {
output: {
entryFileNames: `assets/[name].js`,
chunkFileNames: `assets/[name].js`,
assetFileNames: `assets/[name].[ext]`
}
}
emptyOutDir: true
}
});
Loading