Add resource niceId

This commit is contained in:
Owen
2025-09-08 17:37:30 -07:00
parent 64722617c1
commit a947a74194
7 changed files with 84 additions and 29 deletions

View File

@@ -71,7 +71,7 @@ export const resources = pgTable("resources", {
onDelete: "cascade" onDelete: "cascade"
}) })
.notNull(), .notNull(),
niceId: text("niceId"), // TODO: SHOULD THIS BE NULLABLE? niceId: text("niceId").notNull(),
name: varchar("name").notNull(), name: varchar("name").notNull(),
subdomain: varchar("subdomain"), subdomain: varchar("subdomain"),
fullDomain: varchar("fullDomain"), fullDomain: varchar("fullDomain"),

View File

@@ -77,7 +77,7 @@ export const resources = sqliteTable("resources", {
onDelete: "cascade" onDelete: "cascade"
}) })
.notNull(), .notNull(),
niceId: text("niceId"), // TODO: SHOULD THIS BE NULLABLE? niceId: text("niceId").notNull(),
name: text("name").notNull(), name: text("name").notNull(),
subdomain: text("subdomain"), subdomain: text("subdomain"),
fullDomain: text("fullDomain"), fullDomain: text("fullDomain"),

View File

@@ -345,6 +345,12 @@ authenticated.get(
verifyUserHasAction(ActionsEnum.getResource), verifyUserHasAction(ActionsEnum.getResource),
resource.getResource resource.getResource
); );
authenticated.get(
"/org/:orgId/resource/:niceId",
verifyOrgAccess,
verifyUserHasAction(ActionsEnum.getResource),
resource.getResource
);
authenticated.post( authenticated.post(
"/resource/:resourceId", "/resource/:resourceId",
verifyResourceAccess, verifyResourceAccess,

View File

@@ -2,32 +2,72 @@ import { Request, Response, NextFunction } from "express";
import { z } from "zod"; import { z } from "zod";
import { db } from "@server/db"; import { db } from "@server/db";
import { Resource, resources, sites } from "@server/db"; import { Resource, resources, sites } from "@server/db";
import { eq } from "drizzle-orm"; import { eq, and } from "drizzle-orm";
import response from "@server/lib/response"; import response from "@server/lib/response";
import HttpCode from "@server/types/HttpCode"; import HttpCode from "@server/types/HttpCode";
import createHttpError from "http-errors"; import createHttpError from "http-errors";
import { fromError } from "zod-validation-error"; import { fromError } from "zod-validation-error";
import logger from "@server/logger"; import logger from "@server/logger";
import stoi from "@server/lib/stoi";
import { OpenAPITags, registry } from "@server/openApi"; import { OpenAPITags, registry } from "@server/openApi";
const getResourceSchema = z const getResourceSchema = z
.object({ .object({
resourceId: z resourceId: z
.string() .string()
.transform(Number) .optional()
.pipe(z.number().int().positive()) .transform(stoi)
.pipe(z.number().int().positive().optional())
.optional(),
niceId: z.string().optional(),
orgId: z.string().optional()
}) })
.strict(); .strict();
export type GetResourceResponse = Resource; async function query(resourceId?: number, niceId?: string, orgId?: string) {
if (resourceId) {
const [res] = await db
.select()
.from(resources)
.where(eq(resources.resourceId, resourceId))
.limit(1);
return res;
} else if (niceId && orgId) {
const [res] = await db
.select()
.from(resources)
.where(and(eq(resources.niceId, niceId), eq(resources.orgId, orgId)))
.limit(1);
return res;
}
}
export type GetResourceResponse = NonNullable<Awaited<ReturnType<typeof query>>>;
registry.registerPath({
method: "get",
path: "/org/{orgId}/resource/{niceId}",
description:
"Get a resource by orgId and niceId. NiceId is a readable ID for the resource and unique on a per org basis.",
tags: [OpenAPITags.Org, OpenAPITags.Resource],
request: {
params: z.object({
orgId: z.string(),
niceId: z.string()
})
},
responses: {}
});
registry.registerPath({ registry.registerPath({
method: "get", method: "get",
path: "/resource/{resourceId}", path: "/resource/{resourceId}",
description: "Get a resource.", description: "Get a resource by resourceId.",
tags: [OpenAPITags.Resource], tags: [OpenAPITags.Resource],
request: { request: {
params: getResourceSchema params: z.object({
resourceId: z.number()
})
}, },
responses: {} responses: {}
}); });
@@ -48,29 +88,18 @@ export async function getResource(
); );
} }
const { resourceId } = parsedParams.data; const { resourceId, niceId, orgId } = parsedParams.data;
const [resp] = await db const resource = await query(resourceId, niceId, orgId);
.select()
.from(resources)
.where(eq(resources.resourceId, resourceId))
.limit(1);
const resource = resp;
if (!resource) { if (!resource) {
return next( return next(
createHttpError( createHttpError(HttpCode.NOT_FOUND, "Resource not found")
HttpCode.NOT_FOUND,
`Resource with ID ${resourceId} not found`
)
); );
} }
return response(res, { return response<GetResourceResponse>(res, {
data: { data: resource,
...resource
},
success: true, success: true,
error: false, error: false,
message: "Resource retrieved successfully", message: "Resource retrieved successfully",

View File

@@ -16,6 +16,7 @@ import logger from "@server/logger";
import stoi from "@server/lib/stoi"; import stoi from "@server/lib/stoi";
import { fromZodError } from "zod-validation-error"; import { fromZodError } from "zod-validation-error";
import { OpenAPITags, registry } from "@server/openApi"; import { OpenAPITags, registry } from "@server/openApi";
import { warn } from "console";
const listResourcesParamsSchema = z const listResourcesParamsSchema = z
.object({ .object({
@@ -54,7 +55,8 @@ function queryResources(accessibleResourceIds: number[], orgId: string) {
protocol: resources.protocol, protocol: resources.protocol,
proxyPort: resources.proxyPort, proxyPort: resources.proxyPort,
enabled: resources.enabled, enabled: resources.enabled,
domainId: resources.domainId domainId: resources.domainId,
niceId: resources.niceId
}) })
.from(resources) .from(resources)
.leftJoin( .leftJoin(

View File

@@ -76,8 +76,7 @@ export default async function ResourcesPage(props: ResourcesPageProps) {
id: resource.resourceId, id: resource.resourceId,
name: resource.name, name: resource.name,
orgId: params.orgId, orgId: params.orgId,
nice: resource.niceId,
domain: `${resource.ssl ? "https://" : "http://"}${toUnicode(resource.fullDomain || "")}`, domain: `${resource.ssl ? "https://" : "http://"}${toUnicode(resource.fullDomain || "")}`,
protocol: resource.protocol, protocol: resource.protocol,
proxyPort: resource.proxyPort, proxyPort: resource.proxyPort,
@@ -91,7 +90,8 @@ export default async function ResourcesPage(props: ResourcesPageProps) {
? "protected" ? "protected"
: "not_protected", : "not_protected",
enabled: resource.enabled, enabled: resource.enabled,
domainId: resource.domainId || undefined domainId: resource.domainId || undefined,
ssl: resource.ssl
}; };
}); });

View File

@@ -66,6 +66,7 @@ import { Alert, AlertDescription } from "@app/components/ui/alert";
export type ResourceRow = { export type ResourceRow = {
id: number; id: number;
nice: string | null;
name: string; name: string;
orgId: string; orgId: string;
domain: string; domain: string;
@@ -75,6 +76,7 @@ export type ResourceRow = {
proxyPort: number | null; proxyPort: number | null;
enabled: boolean; enabled: boolean;
domainId?: string; domainId?: string;
ssl: boolean;
}; };
export type InternalResourceRow = { export type InternalResourceRow = {
@@ -336,12 +338,28 @@ export default function ResourcesTable({
); );
} }
}, },
{
accessorKey: "nice",
header: ({ column }) => {
return (
<Button
variant="ghost"
onClick={() =>
column.toggleSorting(column.getIsSorted() === "asc")
}
>
{t("resource")}
<ArrowUpDown className="ml-2 h-4 w-4" />
</Button>
);
}
},
{ {
accessorKey: "protocol", accessorKey: "protocol",
header: t("protocol"), header: t("protocol"),
cell: ({ row }) => { cell: ({ row }) => {
const resourceRow = row.original; const resourceRow = row.original;
return <span>{resourceRow.protocol.toUpperCase()}</span>; return <span>{resourceRow.http ? (resourceRow.ssl ? "HTTPS" : "HTTP") : resourceRow.protocol.toUpperCase()}</span>;
} }
}, },
{ {