Resource identified with niceId now

This commit is contained in:
Owen
2025-09-08 17:50:07 -07:00
parent a947a74194
commit fe6e3b013e
9 changed files with 44 additions and 18 deletions

View File

@@ -1,6 +1,6 @@
import { join } from "path"; import { join } from "path";
import { readFileSync } from "fs"; import { readFileSync } from "fs";
import { db } from "@server/db"; import { db, resources } from "@server/db";
import { exitNodes, sites } from "@server/db"; import { exitNodes, sites } from "@server/db";
import { eq, and } from "drizzle-orm"; import { eq, and } from "drizzle-orm";
import { __DIRNAME } from "@server/lib/consts"; import { __DIRNAME } from "@server/lib/consts";
@@ -34,6 +34,25 @@ export async function getUniqueSiteName(orgId: string): Promise<string> {
} }
} }
export async function getUniqueResourceName(orgId: string): Promise<string> {
let loops = 0;
while (true) {
if (loops > 100) {
throw new Error("Could not generate a unique name");
}
const name = generateName();
const count = await db
.select({ niceId: resources.niceId, orgId: resources.orgId })
.from(resources)
.where(and(eq(resources.niceId, name), eq(resources.orgId, orgId)));
if (count.length === 0) {
return name;
}
loops++;
}
}
export async function getUniqueExitNodeEndpointName(): Promise<string> { export async function getUniqueExitNodeEndpointName(): Promise<string> {
let loops = 0; let loops = 0;
const count = await db const count = await db

View File

@@ -21,6 +21,7 @@ import { subdomainSchema } from "@server/lib/schemas";
import config from "@server/lib/config"; import config from "@server/lib/config";
import { OpenAPITags, registry } from "@server/openApi"; import { OpenAPITags, registry } from "@server/openApi";
import { build } from "@server/build"; import { build } from "@server/build";
import { getUniqueResourceName } from "@server/db/names";
const createResourceParamsSchema = z const createResourceParamsSchema = z
.object({ .object({
@@ -283,10 +284,13 @@ async function createHttpResource(
let resource: Resource | undefined; let resource: Resource | undefined;
const niceId = await getUniqueResourceName(orgId);
await db.transaction(async (trx) => { await db.transaction(async (trx) => {
const newResource = await trx const newResource = await trx
.insert(resources) .insert(resources)
.values({ .values({
niceId,
fullDomain, fullDomain,
domainId, domainId,
orgId, orgId,
@@ -391,10 +395,13 @@ async function createRawResource(
let resource: Resource | undefined; let resource: Resource | undefined;
const niceId = await getUniqueResourceName(orgId);
await db.transaction(async (trx) => { await db.transaction(async (trx) => {
const newResource = await trx const newResource = await trx
.insert(resources) .insert(resources)
.values({ .values({
niceId,
orgId, orgId,
name, name,
http, http,

View File

@@ -18,7 +18,7 @@ import { getTranslations } from 'next-intl/server';
interface ResourceLayoutProps { interface ResourceLayoutProps {
children: React.ReactNode; children: React.ReactNode;
params: Promise<{ resourceId: number | string; orgId: string }>; params: Promise<{ niceId: string; orgId: string }>;
} }
export default async function ResourceLayout(props: ResourceLayoutProps) { export default async function ResourceLayout(props: ResourceLayoutProps) {
@@ -31,7 +31,7 @@ export default async function ResourceLayout(props: ResourceLayoutProps) {
let resource = null; let resource = null;
try { try {
const res = await internal.get<AxiosResponse<GetResourceResponse>>( const res = await internal.get<AxiosResponse<GetResourceResponse>>(
`/resource/${params.resourceId}`, `/org/${params.orgId}/resource/${params.niceId}`,
await authCookieHeader() await authCookieHeader()
); );
resource = res.data.data; resource = res.data.data;
@@ -77,22 +77,22 @@ export default async function ResourceLayout(props: ResourceLayoutProps) {
const navItems = [ const navItems = [
{ {
title: t('general'), title: t('general'),
href: `/{orgId}/settings/resources/{resourceId}/general` href: `/{orgId}/settings/resources/{niceId}/general`
}, },
{ {
title: t('proxy'), title: t('proxy'),
href: `/{orgId}/settings/resources/{resourceId}/proxy` href: `/{orgId}/settings/resources/{niceId}/proxy`
} }
]; ];
if (resource.http) { if (resource.http) {
navItems.push({ navItems.push({
title: t('authentication'), title: t('authentication'),
href: `/{orgId}/settings/resources/{resourceId}/authentication` href: `/{orgId}/settings/resources/{niceId}/authentication`
}); });
navItems.push({ navItems.push({
title: t('rules'), title: t('rules'),
href: `/{orgId}/settings/resources/{resourceId}/rules` href: `/{orgId}/settings/resources/{niceId}/rules`
}); });
} }

View File

@@ -1,10 +1,10 @@
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
export default async function ResourcePage(props: { export default async function ResourcePage(props: {
params: Promise<{ resourceId: number | string; orgId: string }>; params: Promise<{ niceId: string; orgId: string }>;
}) { }) {
const params = await props.params; const params = await props.params;
redirect( redirect(
`/${params.orgId}/settings/resources/${params.resourceId}/proxy` `/${params.orgId}/settings/resources/${params.niceId}/proxy`
); );
} }

View File

@@ -256,7 +256,7 @@ export default function ReverseProxyTargets(props: {
const fetchTargets = async () => { const fetchTargets = async () => {
try { try {
const res = await api.get<AxiosResponse<ListTargetsResponse>>( const res = await api.get<AxiosResponse<ListTargetsResponse>>(
`/resource/${params.resourceId}/targets` `/resource/${resource.resourceId}/targets`
); );
if (res.status === 200) { if (res.status === 200) {
@@ -447,7 +447,7 @@ export default function ReverseProxyTargets(props: {
if (target.new) { if (target.new) {
const res = await api.put< const res = await api.put<
AxiosResponse<CreateTargetResponse> AxiosResponse<CreateTargetResponse>
>(`/resource/${params.resourceId}/target`, data); >(`/resource/${resource.resourceId}/target`, data);
target.targetId = res.data.data.targetId; target.targetId = res.data.data.targetId;
target.new = false; target.new = false;
} else if (target.updated) { } else if (target.updated) {
@@ -475,7 +475,7 @@ export default function ReverseProxyTargets(props: {
}; };
// Single API call to update all settings // Single API call to update all settings
await api.post(`/resource/${params.resourceId}`, payload); await api.post(`/resource/${resource.resourceId}`, payload);
// Update local resource context // Update local resource context
updateResource({ updateResource({

View File

@@ -128,7 +128,7 @@ export default function ResourceRules(props: {
try { try {
const res = await api.get< const res = await api.get<
AxiosResponse<ListResourceRulesResponse> AxiosResponse<ListResourceRulesResponse>
>(`/resource/${params.resourceId}/rules`); >(`/resource/${resource.resourceId}/rules`);
if (res.status === 200) { if (res.status === 200) {
setRules(res.data.data.rules); setRules(res.data.data.rules);
} }
@@ -251,7 +251,7 @@ export default function ResourceRules(props: {
// Save rules enabled state // Save rules enabled state
const res = await api const res = await api
.post(`/resource/${params.resourceId}`, { .post(`/resource/${resource.resourceId}`, {
applyRules: rulesEnabled applyRules: rulesEnabled
}) })
.catch((err) => { .catch((err) => {
@@ -336,13 +336,13 @@ export default function ResourceRules(props: {
if (rule.new) { if (rule.new) {
const res = await api.put( const res = await api.put(
`/resource/${params.resourceId}/rule`, `/resource/${resource.resourceId}/rule`,
data data
); );
rule.ruleId = res.data.data.ruleId; rule.ruleId = res.data.data.ruleId;
} else if (rule.updated) { } else if (rule.updated) {
await api.post( await api.post(
`/resource/${params.resourceId}/rule/${rule.ruleId}`, `/resource/${resource.resourceId}/rule/${rule.ruleId}`,
data data
); );
} }
@@ -361,7 +361,7 @@ export default function ResourceRules(props: {
for (const ruleId of rulesToRemove) { for (const ruleId of rulesToRemove) {
await api.delete( await api.delete(
`/resource/${params.resourceId}/rule/${ruleId}` `/resource/${resource.resourceId}/rule/${ruleId}`
); );
setRules(rules.filter((r) => r.ruleId !== ruleId)); setRules(rules.filter((r) => r.ruleId !== ruleId));
} }

View File

@@ -481,7 +481,7 @@ export default function ResourcesTable({
</DropdownMenuContent> </DropdownMenuContent>
</DropdownMenu> </DropdownMenu>
<Link <Link
href={`/${resourceRow.orgId}/settings/resources/${resourceRow.id}`} href={`/${resourceRow.orgId}/settings/resources/${resourceRow.nice}`}
> >
<Button <Button
variant={"secondary"} variant={"secondary"}