feat: Add functionality to start a Fastify server

This commit is contained in:
Gauthier Daniels 2025-04-19 16:14:12 +02:00
parent d27f265e8a
commit 40ecdc6448
5 changed files with 60 additions and 48 deletions

46
app/fastify-server.ts Normal file
View File

@ -0,0 +1,46 @@
import { fileURLToPath } from "node:url";
import { dirname } from "node:path";
import { config } from "./config";
import Fastify from "fastify";
import fs from "node:fs";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const root = __dirname;
const pagesDir = `${root}/dist/client`;
async function startServer() {
const app = Fastify();
await app.register(await import("@fastify/static"), {
root: `${root}/dist/client`,
wildcard: false,
});
app.get("/*", (request, reply) => {
const filePath = `${pagesDir}${request.url}/index.html`;
if (!fs.existsSync(filePath)) {
const stream = fs.createReadStream(`${pagesDir}/404.html`);
reply.status(404).type("text/html").send(stream);
return;
}
const stream = fs.createReadStream(filePath);
reply.type("text/html").send(stream);
});
return app;
}
const app = await startServer();
app.listen({ port: config.PORT, host: "0.0.0.0" }, (error, address) => {
if (error) {
app.log.error(error);
process.exit(1);
}
console.log(`Server listening on ${address}`);
});

View File

@ -1,8 +1,8 @@
{ {
"scripts": { "scripts": {
"dev": "bun ./fastify-entry.ts", "dev": "bun ./fastify-entry.ts",
"build": "vike build", "build": "cross-env DEBUG=vike:error,vike:log vike build",
"preview": "cross-env NODE_ENV=production bun ./fastify-entry.ts", "preview": "cross-env NODE_ENV=production bun ./fastify-server.ts",
"production": "bun run build && bun run preview", "production": "bun run build && bun run preview",
"lint": "biome lint --write .", "lint": "biome lint --write .",
"format": "biome format --write ." "format": "biome format --write ."

11
app/pages/temp/+Page.tsx Executable file
View File

@ -0,0 +1,11 @@
export default function Page() {
return (
<>
<h1 class={"font-bold text-3xl pb-4"}>Temp</h1>
This page is:
<ul>
<li>Rendered to HTML.</li>
</ul>
</>
);
}

View File

@ -1,4 +1,4 @@
import {Button} from "./Button"; import { Button } from "@/components/Button";
# TEST # TEST

View File

@ -1,45 +0,0 @@
import type { JSX } from "solid-js";
import clsx from "clsx";
const variantStyles = {
primary:
"bg-violet-300 font-semibold text-slate-900 hover:bg-violet-200 focus:outline-hidden focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-violet-300/50 active:bg-violet-500",
secondary:
"bg-slate-800 font-medium text-white hover:bg-slate-700 focus:outline-hidden focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-white/50 active:text-slate-400",
ghost:
"bg-transparent font-medium text-slate-900 dark:text-slate-400 hover:bg-slate-100 focus:outline-hidden focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-slate-300/50 active:bg-slate-200",
};
const sizeStyles = {
sm: "rounded-md py-1 px-2 text-xs",
md: "rounded-full py-2 px-4 text-sm",
lg: "rounded-full py-3 px-6 text-base",
};
type ButtonProps = {
variant?: keyof typeof variantStyles;
size?: keyof typeof sizeStyles;
className?: string;
} & (
| JSX.IntrinsicElements["button"]
| (JSX.IntrinsicElements["a"] & { href: string })
);
export function Button({
variant = "primary",
size = "md",
className,
...props
}: ButtonProps) {
className = clsx(
variantStyles[variant],
sizeStyles[size],
"cursor-pointer",
className,
);
return (
<button class={className} {...(props as JSX.IntrinsicElements["button"])} />
);
}