chore: production setup

This commit is contained in:
Nicolas Meienberger
2025-09-27 14:10:15 +02:00
parent 9f3fb8a302
commit 88e310cc4f
18 changed files with 213 additions and 58 deletions

View File

@@ -4,3 +4,4 @@
# React Router
/.react-router/
/build/
/dist/

View File

@@ -8,7 +8,7 @@ import "./app.css";
import { client } from "./api-client/client.gen";
client.setConfig({
baseUrl: "http://192.168.2.42:3000/",
baseUrl: "/",
});
export const links: Route.LinksFunction = () => [

View File

@@ -51,7 +51,7 @@
"tailwindcss": "^4.1.4",
"tw-animate-css": "^1.3.6",
"typescript": "^5.8.3",
"vite": "^6.3.3",
"vite": "npm:rolldown-vite@latest",
"vite-bundle-analyzer": "^1.2.3",
"vite-tsconfig-paths": "^5.1.4"
}

View File

@@ -2,4 +2,5 @@ import type { Config } from "@react-router/dev/config";
export default {
ssr: false,
buildDirectory: "dist",
} satisfies Config;

View File

@@ -2,16 +2,30 @@ import { reactRouter } from "@react-router/dev/vite";
import tailwindcss from "@tailwindcss/vite";
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";
import { analyzer } from "vite-bundle-analyzer";
const alias = {};
const { NODE_ENV } = process.env;
if (NODE_ENV === "production") {
// @ts-expect-error
alias["react-dom/server"] = "react-dom/server.node";
}
export default defineConfig({
plugins: [tailwindcss(), reactRouter(), tsconfigPaths(), analyzer()],
plugins: [tailwindcss(), reactRouter(), tsconfigPaths()],
resolve: {
alias,
},
build: {
outDir: "dist",
// sourcemap: true,
},
server: {
host: true,
port: 3000,
port: 4097,
proxy: {
"/api": {
target: "http://localhost:8080",
target: "http://localhost:4096",
changeOrigin: true,
},
},

1
apps/server/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
dist/

13
apps/server/build.ts Normal file
View File

@@ -0,0 +1,13 @@
await Bun.build({
entrypoints: ["./src/index.ts"],
outdir: "./dist",
target: "bun",
env: "disable",
// sourcemap: "linked",
minify: {
whitespace: true,
identifiers: true,
syntax: true,
},
external: [],
});

View File

@@ -3,6 +3,7 @@
"type": "module",
"scripts": {
"dev": "bun run --watch src/index.ts",
"build": "rm -rf dist && bun build.ts",
"tsc": "tsc --noEmit",
"gen:migrations": "drizzle-kit generate"
},

View File

@@ -3,11 +3,9 @@ import "dotenv/config";
const envSchema = type({
NODE_ENV: type.enumerated("development", "production", "test").default("development"),
VOLUME_ROOT: "string",
}).pipe((s) => ({
__prod__: s.NODE_ENV === "production",
environment: s.NODE_ENV,
volumeRootHost: s.VOLUME_ROOT,
}));
const parseConfig = (env: unknown) => {

View File

@@ -1,14 +1,22 @@
import "dotenv/config";
import { Database } from "bun:sqlite";
import path from "node:path";
import { drizzle } from "drizzle-orm/bun-sqlite";
import { migrate } from "drizzle-orm/bun-sqlite/migrator";
import * as schema from "./schema";
import { DATABASE_URL } from "../core/constants";
import * as schema from "./schema";
const sqlite = new Database(DATABASE_URL);
export const db = drizzle({ client: sqlite, schema });
export const runDbMigrations = () => {
migrate(db, { migrationsFolder: "./drizzle" });
let migrationsFolder = path.join("/app", "assets", "migrations");
const { NODE_ENV } = process.env;
if (NODE_ENV !== "production") {
migrationsFolder = path.join("/app", "apps", "server", "drizzle");
}
migrate(db, { migrationsFolder });
};

View File

@@ -1,6 +1,7 @@
import * as fs from "node:fs/promises";
import { Scalar } from "@scalar/hono-api-reference";
import { Hono } from "hono";
import { serveStatic } from "hono/bun";
import { logger as honoLogger } from "hono/logger";
import { openAPISpecs } from "hono-openapi";
import { runDbMigrations } from "./db/db";
@@ -31,6 +32,7 @@ export const scalarDescriptor = Scalar({
const driver = new Hono().use(honoLogger()).route("/", driverController);
const app = new Hono()
.use(honoLogger())
.get("*", serveStatic({ root: "./assets/frontend" }))
.get("healthcheck", (c) => c.json({ status: "ok" }))
.basePath("/api/v1")
.route("/volumes", volumeController);
@@ -38,10 +40,6 @@ const app = new Hono()
app.get("/openapi.json", generalDescriptor(app));
app.get("/docs", scalarDescriptor);
app.get("/", (c) => {
return c.json({ message: "Welcome to the Ironmount API" });
});
app.onError((err, c) => {
logger.error(`${c.req.url}: ${err.message}`);
@@ -56,24 +54,21 @@ app.onError((err, c) => {
const socketPath = "/run/docker/plugins/ironmount.sock";
(async () => {
await fs.mkdir("/run/docker/plugins", { recursive: true });
await fs.mkdir("/run/docker/plugins", { recursive: true });
runDbMigrations();
runDbMigrations();
Bun.serve({
unix: socketPath,
fetch: driver.fetch,
});
Bun.serve({
unix: socketPath,
fetch: driver.fetch,
});
Bun.serve({
port: 4096,
fetch: app.fetch,
});
Bun.serve({
port: 8080,
fetch: app.fetch,
});
startup();
startup();
logger.info(`Server is running at http://localhost:8080 and unix socket at ${socketPath}`);
})();
logger.info(`Server is running at http://localhost:4096 and unix socket at ${socketPath}`);
export type AppType = typeof app;