Ship Novya PWA with backend for deployable production use.
Fix production TypeScript builds and add DEPLOY.md so the app can be hosted from this repo. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
import { existsSync, readFileSync } from 'node:fs';
|
||||
import { resolve } from 'node:path';
|
||||
import { z } from 'zod';
|
||||
|
||||
function loadDotEnvFile(): void {
|
||||
const envPath = resolve(process.cwd(), '.env');
|
||||
|
||||
if (!existsSync(envPath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const contents = readFileSync(envPath, 'utf8');
|
||||
|
||||
for (const line of contents.split(/\r?\n/)) {
|
||||
const trimmed = line.trim();
|
||||
|
||||
if (!trimmed || trimmed.startsWith('#')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const separatorIndex = trimmed.indexOf('=');
|
||||
|
||||
if (separatorIndex <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const key = trimmed.slice(0, separatorIndex).trim();
|
||||
let value = trimmed.slice(separatorIndex + 1).trim();
|
||||
|
||||
if (
|
||||
(value.startsWith('"') && value.endsWith('"')) ||
|
||||
(value.startsWith("'") && value.endsWith("'"))
|
||||
) {
|
||||
value = value.slice(1, -1);
|
||||
}
|
||||
|
||||
if (process.env[key] === undefined) {
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
loadDotEnvFile();
|
||||
|
||||
const envSchema = z.object({
|
||||
NODE_ENV: z.enum(['development', 'test', 'production']).default('development'),
|
||||
PORT: z.coerce.number().int().positive().default(3001),
|
||||
HOST: z.string().min(1).default('0.0.0.0'),
|
||||
API_VERSION: z.string().min(1).default('1.0.0'),
|
||||
BUILD_TIMESTAMP: z.string().optional().default(''),
|
||||
DATABASE_URL: z
|
||||
.string()
|
||||
.min(1, 'DATABASE_URL is required')
|
||||
.refine(
|
||||
(value) => value.startsWith('mysql://') || value.startsWith('mysql2://'),
|
||||
'DATABASE_URL must be a MySQL connection string (mysql://...)',
|
||||
),
|
||||
LOG_LEVEL: z
|
||||
.enum(['fatal', 'error', 'warn', 'info', 'debug', 'trace', 'silent'])
|
||||
.default('info'),
|
||||
CORS_ORIGIN: z.string().min(1).default('http://localhost:5173'),
|
||||
TRUST_PROXY: z
|
||||
.enum(['true', 'false'])
|
||||
.default('false')
|
||||
.transform((value) => value === 'true'),
|
||||
RATE_LIMIT_WINDOW_MS: z.coerce.number().int().positive().default(60_000),
|
||||
RATE_LIMIT_MAX: z.coerce.number().int().positive().default(100),
|
||||
AUTH_RATE_LIMIT_MAX: z.coerce.number().int().positive().default(20),
|
||||
AUTH_SESSION_TTL_HOURS: z.coerce.number().int().positive().default(168),
|
||||
OPENAI_API_KEY: z.string().optional().default(''),
|
||||
OPENAI_BASE_URL: z.string().min(1).default('https://api.gapgpt.app/v1'),
|
||||
OPENAI_MODEL: z.string().min(1).default('gpt-4o-mini'),
|
||||
OPENAI_ORG_ID: z.string().optional().default(''),
|
||||
OPENAI_PROJECT_ID: z.string().optional().default(''),
|
||||
});
|
||||
|
||||
export type Env = z.infer<typeof envSchema>;
|
||||
|
||||
export class EnvValidationError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'EnvValidationError';
|
||||
}
|
||||
}
|
||||
|
||||
function formatZodError(error: z.ZodError): string {
|
||||
return error.issues
|
||||
.map((issue) => `${issue.path.join('.') || 'env'}: ${issue.message}`)
|
||||
.join('; ');
|
||||
}
|
||||
|
||||
export function parseEnv(source: NodeJS.ProcessEnv = process.env): Env {
|
||||
const result = envSchema.safeParse(source);
|
||||
|
||||
if (!result.success) {
|
||||
throw new EnvValidationError(`Environment validation failed: ${formatZodError(result.error)}`);
|
||||
}
|
||||
|
||||
return result.data;
|
||||
}
|
||||
|
||||
export function loadEnv(source: NodeJS.ProcessEnv = process.env): Env {
|
||||
try {
|
||||
return parseEnv(source);
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Unknown environment error';
|
||||
console.error(message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
export { envSchema };
|
||||
Reference in New Issue
Block a user