diff --git a/script/build.ts b/script/build.ts index 0b4eb60..9aba467 100644 --- a/script/build.ts +++ b/script/build.ts @@ -54,7 +54,7 @@ async function buildAll() { "process.env.NODE_ENV": '"production"', }, minify: true, - external: externals, + external: [...externals, "better-sqlite3", "pg", "drizzle-orm/better-sqlite3", "drizzle-orm/node-postgres"], logLevel: "info", }); } diff --git a/server/db.ts b/server/db.ts index c640111..ed4358a 100644 --- a/server/db.ts +++ b/server/db.ts @@ -1,32 +1,31 @@ -import { drizzle } from "drizzle-orm/node-postgres"; +import { drizzle } from "drizzle-orm/better-sqlite3"; import { eq } from "drizzle-orm"; import { users, User, NewUser } from "../shared/schema"; -import { Pool } from "pg"; +import Database from "better-sqlite3"; +import path from "node:path"; +import fs from "node:fs"; -// Production PostgreSQL connection -const pool = new Pool({ - host: process.env.DB_HOST || "10.136.95.120", - port: parseInt(process.env.DB_PORT || "5432"), - user: process.env.DB_USER || "gn_admin", - password: process.env.DB_PASSWORD || "", - database: process.env.DB_NAME || "gammanexus", - ssl: (process.env.DB_SSL === "true" && process.env.NODE_ENV === "production") ? { rejectUnauthorized: false } : false, - max: 20, - idleTimeoutMillis: 30000, - connectionTimeoutMillis: 5000, -}); +// Use SQLite for production (PostgreSQL migration later) +const dbPath = path.resolve(__dirname || ".", "..", "data.db"); -export const db = drizzle(pool); +// Ensure data directory exists +const dbDir = path.dirname(dbPath); +if (!fs.existsSync(dbDir)) { + fs.mkdirSync(dbDir, { recursive: true }); +} + +const sqlite = new Database(dbPath); +export const db = drizzle(sqlite); // Initialize users table if it doesn't exist (called at startup) export async function initDb(): Promise { - await db.execute(` + sqlite.exec(` CREATE TABLE IF NOT EXISTS users ( - id SERIAL PRIMARY KEY, + id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE, password_hash TEXT NOT NULL, - created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() + created_at DATETIME DEFAULT CURRENT_TIMESTAMP ) `); } diff --git a/server/static.ts b/server/static.ts index b97fbfe..bcc33b8 100644 --- a/server/static.ts +++ b/server/static.ts @@ -2,13 +2,20 @@ import express from 'express'; import type { Express } from 'express'; import fs from "node:fs"; import path from "node:path"; -import { fileURLToPath } from "node:url"; - -const __dirname = path.dirname(fileURLToPath(import.meta.url)); export function serveStatic(app: Express) { - const distPath = path.resolve(__dirname, "..", "dist", "public"); + // In production, dist/public is at the same level as index.cjs + const distPath = path.resolve(__dirname, "..", "public"); if (!fs.existsSync(distPath)) { + // Fallback: check cwd + const cwdPath = path.resolve(process.cwd(), "dist", "public"); + if (fs.existsSync(cwdPath)) { + app.use(express.static(cwdPath)); + app.use("/{*path}", (_req, res) => { + res.sendFile(path.resolve(cwdPath, "index.html")); + }); + return; + } throw new Error( `Could not find the build directory: ${distPath}, make sure to build the client first`, );