diff --git a/.gitignore b/.gitignore index 7fdae351..69f4f292 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,5 @@ migrations package-lock.json tsconfig.tsbuildinfo config.yml +dist +.dist \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 4c521460..cd801929 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,7 +22,8 @@ RUN npm install --omit=dev COPY --from=builder /app/.next ./.next COPY --from=builder /app/dist ./dist -COPY ./config/config.example.yml ./ -COPY server/db/names.json /app/dist/names.json + +COPY ./config/config.example.yml ./dist/config.example.yml +COPY ./server/db/names.json ./dist/names.json CMD ["npm", "start"] diff --git a/server/config.ts b/server/config.ts index ac2d3e2a..0642110c 100644 --- a/server/config.ts +++ b/server/config.ts @@ -3,6 +3,10 @@ import { fromError } from "zod-validation-error"; import path from "path"; import fs from "fs"; import yaml from "js-yaml"; +import { fileURLToPath } from "url"; + +export const __FILENAME = fileURLToPath(import.meta.url); +export const __DIRNAME = path.dirname(__FILENAME); export const APP_PATH = path.join("config"); @@ -37,6 +41,12 @@ const environmentSchema = z.object({ no_reply: z.string().email().optional(), }) .optional(), + flags: z + .object({ + allow_org_subdomain_changing: z.boolean().optional(), + require_email_verification: z.boolean().optional(), + }) + .optional(), }); const loadConfig = (configPath: string) => { @@ -64,7 +74,7 @@ if (fs.existsSync(configFilePath1)) { environment = loadConfig(configFilePath2); } if (!environment) { - const exampleConfigPath = path.join("config.example.yml"); + const exampleConfigPath = path.join(__DIRNAME, "config.example.yml"); if (fs.existsSync(exampleConfigPath)) { try { const exampleConfigContent = fs.readFileSync( diff --git a/server/db/names.ts b/server/db/names.ts index f5e253bc..cd6d8908 100644 --- a/server/db/names.ts +++ b/server/db/names.ts @@ -1,27 +1,26 @@ -import { fileURLToPath } from 'url'; -import { dirname, join } from 'path'; -import { readFileSync } from 'fs'; -import { db } from '@server/db'; -import { sites } from './schema'; -import { eq, and } from 'drizzle-orm'; - -// Get the directory name of the current module -const __filename = fileURLToPath(import.meta.url); -const __dirname = dirname(__filename); +import { join } from "path"; +import { readFileSync } from "fs"; +import { db } from "@server/db"; +import { sites } from "./schema"; +import { eq, and } from "drizzle-orm"; +import { __DIRNAME } from "@server/config"; // Load the names from the names.json file -const file = join(__dirname, 'names.json'); -export const names = JSON.parse(readFileSync(file, 'utf-8')); +const file = join(__DIRNAME, "names.json"); +export const names = JSON.parse(readFileSync(file, "utf-8")); export async function getUniqueName(orgId: string): Promise { let loops = 0; while (true) { if (loops > 100) { - throw new Error('Could not generate a unique name'); + throw new Error("Could not generate a unique name"); } const name = generateName(); - const count = await db.select({ niceId: sites.niceId, orgId: sites.orgId }).from(sites).where(and(eq(sites.niceId, name), eq(sites.orgId, orgId))); + const count = await db + .select({ niceId: sites.niceId, orgId: sites.orgId }) + .from(sites) + .where(and(eq(sites.niceId, name), eq(sites.orgId, orgId))); if (count.length === 0) { return name; } @@ -31,7 +30,12 @@ export async function getUniqueName(orgId: string): Promise { export function generateName(): string { return ( - names.descriptors[Math.floor(Math.random() * names.descriptors.length)] + "-" + + names.descriptors[ + Math.floor(Math.random() * names.descriptors.length) + ] + + "-" + names.animals[Math.floor(Math.random() * names.animals.length)] - ).toLowerCase().replace(/\s/g, '-'); -} \ No newline at end of file + ) + .toLowerCase() + .replace(/\s/g, "-"); +} diff --git a/server/middlewares/formatError.ts b/server/middlewares/formatError.ts index e6b9454f..0f62520d 100644 --- a/server/middlewares/formatError.ts +++ b/server/middlewares/formatError.ts @@ -8,10 +8,10 @@ export const errorHandlerMiddleware: ErrorRequestHandler = ( error, req, res: Response, - next: NextFunction, + next: NextFunction ) => { const statusCode = error.statusCode || HttpCode.INTERNAL_SERVER_ERROR; - if (config.app.environment !== "prod") { + if (process.env.ENVIRONMENT !== "prod") { logger.error(error); } res?.status(statusCode).send({ @@ -20,6 +20,6 @@ export const errorHandlerMiddleware: ErrorRequestHandler = ( error: true, message: error.message || "Internal Server Error", status: statusCode, - stack: config.app.environment === "prod" ? null : error.stack, + stack: process.env.ENVIRONMENT === "prod" ? null : error.stack, }); }; diff --git a/src/api/index.ts b/src/api/index.ts index e45e8abf..792778f6 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,7 +1,7 @@ import axios from "axios"; export const api = axios.create({ - baseURL: "https://fossorial.io/api/v1", + baseURL: process.env.NEXT_PUBLIC_EXTERNAL_API_BASE_URL, timeout: 10000, headers: { "Content-Type": "application/json", @@ -9,7 +9,7 @@ export const api = axios.create({ }); export const internal = axios.create({ - baseURL: "http://pangolin:3000/api/v1", + baseURL: process.env.NEXT_PUBLIC_INTERNAL_API_BASE_URL, timeout: 10000, headers: { "Content-Type": "application/json",