mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-21 11:15:13 +02:00
add list domains for org endpoint
This commit is contained in:
parent
851bedb2e5
commit
82f990eb8b
7 changed files with 136 additions and 7 deletions
|
@ -62,6 +62,7 @@ export enum ActionsEnum {
|
||||||
deleteResourceRule = "deleteResourceRule",
|
deleteResourceRule = "deleteResourceRule",
|
||||||
listResourceRules = "listResourceRules",
|
listResourceRules = "listResourceRules",
|
||||||
updateResourceRule = "updateResourceRule",
|
updateResourceRule = "updateResourceRule",
|
||||||
|
listOrgDomains = "listOrgDomains",
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function checkUserActionPermission(
|
export async function checkUserActionPermission(
|
||||||
|
|
|
@ -438,3 +438,4 @@ export type ResourceAccessToken = InferSelectModel<typeof resourceAccessToken>;
|
||||||
export type ResourceWhitelist = InferSelectModel<typeof resourceWhitelist>;
|
export type ResourceWhitelist = InferSelectModel<typeof resourceWhitelist>;
|
||||||
export type VersionMigration = InferSelectModel<typeof versionMigrations>;
|
export type VersionMigration = InferSelectModel<typeof versionMigrations>;
|
||||||
export type ResourceRule = InferSelectModel<typeof resourceRules>;
|
export type ResourceRule = InferSelectModel<typeof resourceRules>;
|
||||||
|
export type Domain = InferSelectModel<typeof domains>;
|
||||||
|
|
1
server/routers/domain/index.ts
Normal file
1
server/routers/domain/index.ts
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export * from "./listDomains";
|
108
server/routers/domain/listDomains.ts
Normal file
108
server/routers/domain/listDomains.ts
Normal file
|
@ -0,0 +1,108 @@
|
||||||
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { db } from "@server/db";
|
||||||
|
import { domains, orgDomains, users } from "@server/db/schema";
|
||||||
|
import response from "@server/lib/response";
|
||||||
|
import HttpCode from "@server/types/HttpCode";
|
||||||
|
import createHttpError from "http-errors";
|
||||||
|
import { eq, sql } from "drizzle-orm";
|
||||||
|
import logger from "@server/logger";
|
||||||
|
import { fromError } from "zod-validation-error";
|
||||||
|
|
||||||
|
const listDomainsParamsSchema = z
|
||||||
|
.object({
|
||||||
|
orgId: z.string()
|
||||||
|
})
|
||||||
|
.strict();
|
||||||
|
|
||||||
|
const listDomainsSchema = z
|
||||||
|
.object({
|
||||||
|
limit: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.default("1000")
|
||||||
|
.transform(Number)
|
||||||
|
.pipe(z.number().int().nonnegative()),
|
||||||
|
offset: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.default("0")
|
||||||
|
.transform(Number)
|
||||||
|
.pipe(z.number().int().nonnegative())
|
||||||
|
})
|
||||||
|
.strict();
|
||||||
|
|
||||||
|
async function queryDomains(orgId: string, limit: number, offset: number) {
|
||||||
|
return await db
|
||||||
|
.select({
|
||||||
|
domainId: domains.domainId,
|
||||||
|
baseDomain: domains.baseDomain
|
||||||
|
})
|
||||||
|
.from(orgDomains)
|
||||||
|
.where(eq(orgDomains.orgId, orgId))
|
||||||
|
.leftJoin(domains, eq(domains.domainId, orgDomains.domainId))
|
||||||
|
.limit(limit)
|
||||||
|
.offset(offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ListDomainsResponse = {
|
||||||
|
domains: NonNullable<Awaited<ReturnType<typeof queryDomains>>>;
|
||||||
|
pagination: { total: number; limit: number; offset: number };
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function listDomains(
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
): Promise<any> {
|
||||||
|
try {
|
||||||
|
const parsedQuery = listDomainsSchema.safeParse(req.query);
|
||||||
|
if (!parsedQuery.success) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
fromError(parsedQuery.error).toString()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const { limit, offset } = parsedQuery.data;
|
||||||
|
|
||||||
|
const parsedParams = listDomainsParamsSchema.safeParse(req.params);
|
||||||
|
if (!parsedParams.success) {
|
||||||
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.BAD_REQUEST,
|
||||||
|
fromError(parsedParams.error).toString()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { orgId } = parsedParams.data;
|
||||||
|
|
||||||
|
const domains = await queryDomains(orgId.toString(), limit, offset);
|
||||||
|
|
||||||
|
const [{ count }] = await db
|
||||||
|
.select({ count: sql<number>`count(*)` })
|
||||||
|
.from(users);
|
||||||
|
|
||||||
|
return response<ListDomainsResponse>(res, {
|
||||||
|
data: {
|
||||||
|
domains,
|
||||||
|
pagination: {
|
||||||
|
total: count,
|
||||||
|
limit,
|
||||||
|
offset
|
||||||
|
}
|
||||||
|
},
|
||||||
|
success: true,
|
||||||
|
error: false,
|
||||||
|
message: "Users retrieved successfully",
|
||||||
|
status: HttpCode.OK
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error);
|
||||||
|
return next(
|
||||||
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ import config from "@server/lib/config";
|
||||||
import * as site from "./site";
|
import * as site from "./site";
|
||||||
import * as org from "./org";
|
import * as org from "./org";
|
||||||
import * as resource from "./resource";
|
import * as resource from "./resource";
|
||||||
|
import * as domain from "./domain";
|
||||||
import * as target from "./target";
|
import * as target from "./target";
|
||||||
import * as user from "./user";
|
import * as user from "./user";
|
||||||
import * as auth from "./auth";
|
import * as auth from "./auth";
|
||||||
|
@ -133,6 +134,13 @@ authenticated.get(
|
||||||
resource.listResources
|
resource.listResources
|
||||||
);
|
);
|
||||||
|
|
||||||
|
authenticated.get(
|
||||||
|
"/org/:orgId/domains",
|
||||||
|
verifyOrgAccess,
|
||||||
|
verifyUserHasAction(ActionsEnum.listOrgDomains),
|
||||||
|
domain.listDomains
|
||||||
|
);
|
||||||
|
|
||||||
authenticated.post(
|
authenticated.post(
|
||||||
"/org/:orgId/create-invite",
|
"/org/:orgId/create-invite",
|
||||||
verifyOrgAccess,
|
verifyOrgAccess,
|
||||||
|
|
|
@ -89,7 +89,10 @@ export async function createOrg(
|
||||||
let org: Org | null = null;
|
let org: Org | null = null;
|
||||||
|
|
||||||
await db.transaction(async (trx) => {
|
await db.transaction(async (trx) => {
|
||||||
const allDomains = await trx.select().from(domains);
|
const allDomains = await trx
|
||||||
|
.select()
|
||||||
|
.from(domains)
|
||||||
|
.where(eq(domains.configManaged, true));
|
||||||
|
|
||||||
const newOrg = await trx
|
const newOrg = await trx
|
||||||
.insert(orgs)
|
.insert(orgs)
|
||||||
|
|
|
@ -26,6 +26,7 @@ export async function traefikConfigProvider(
|
||||||
proxyPort: resources.proxyPort,
|
proxyPort: resources.proxyPort,
|
||||||
protocol: resources.protocol,
|
protocol: resources.protocol,
|
||||||
isBaseDomain: resources.isBaseDomain,
|
isBaseDomain: resources.isBaseDomain,
|
||||||
|
domainId: resources.domainId,
|
||||||
// Site fields
|
// Site fields
|
||||||
site: {
|
site: {
|
||||||
siteId: sites.siteId,
|
siteId: sites.siteId,
|
||||||
|
@ -34,8 +35,7 @@ export async function traefikConfigProvider(
|
||||||
},
|
},
|
||||||
// Org fields
|
// Org fields
|
||||||
org: {
|
org: {
|
||||||
orgId: orgs.orgId,
|
orgId: orgs.orgId
|
||||||
domain: orgs.domain
|
|
||||||
},
|
},
|
||||||
// Targets as a subquery
|
// Targets as a subquery
|
||||||
targets: sql<string>`json_group_array(json_object(
|
targets: sql<string>`json_group_array(json_object(
|
||||||
|
@ -105,15 +105,22 @@ export async function traefikConfigProvider(
|
||||||
const site = resource.site;
|
const site = resource.site;
|
||||||
const org = resource.org;
|
const org = resource.org;
|
||||||
|
|
||||||
if (!org.domain) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const routerName = `${resource.resourceId}-router`;
|
const routerName = `${resource.resourceId}-router`;
|
||||||
const serviceName = `${resource.resourceId}-service`;
|
const serviceName = `${resource.resourceId}-service`;
|
||||||
const fullDomain = `${resource.fullDomain}`;
|
const fullDomain = `${resource.fullDomain}`;
|
||||||
|
|
||||||
if (resource.http) {
|
if (resource.http) {
|
||||||
|
if (!resource.domainId) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!resource.fullDomain) {
|
||||||
|
logger.error(
|
||||||
|
`Resource ${resource.resourceId} has no fullDomain`
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// HTTP configuration remains the same
|
// HTTP configuration remains the same
|
||||||
if (!resource.subdomain && !resource.isBaseDomain) {
|
if (!resource.subdomain && !resource.isBaseDomain) {
|
||||||
continue;
|
continue;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue