Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): support external logging #838

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/server/src/api/routes/auth/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ const key = 'CALLBACK';
export default async function route(request: Request, config: Config) {
const { error } = Logger(
{ ...config, debug: config.debug } as Config,
'[ROUTES]',
`[${key}]`
`[ROUTES][${key}]`
);
const [provider] = new URL(request.url).pathname.split('/').reverse();
const passThroughUrl = new URL(request.url);
Expand Down
10 changes: 1 addition & 9 deletions packages/server/src/api/routes/me/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Logger from '../../../utils/Logger';
import { Routes } from '../../types';
import { apiRoutes } from '../../utils/routes/apiRoutes';
import urlMatches from '../../utils/routes/urlMatches';
Expand Down Expand Up @@ -39,25 +38,18 @@ const key = 'ME';
async function GET(
url: string,
init: RequestInit & { request: Request },
log: (...args: string[]) => void,
config: Config
) {
log('[GET]', url);
const res = await request(url, init, config);
return res;
}

export default async function route(request: Request, config: Config) {
const url = apiRoutes(config)[key];
const { info } = Logger(
{ ...config, debug: config.debug } as Config,
'[ROUTES]',
`[${key}]`
);

switch (request.method) {
case 'GET':
return await GET(url, { request }, info, config);
return await GET(url, { request }, config);

default:
return new Response('method not allowed', { status: 405 });
Expand Down
4 changes: 1 addition & 3 deletions packages/server/src/api/routes/tenants/GET.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ import { apiRoutes } from '../../utils/routes/apiRoutes';
export async function GET(
config: Config,
session: ActiveSession,
init: RequestInit & { request: Request },
log: (...args: string[]) => void
init: RequestInit & { request: Request }
) {
let url = `${apiRoutes(config).USER_TENANTS(session.id)}`;
if (typeof session === 'object' && 'user' in session && session.user) {
url = `${apiRoutes(config).USER_TENANTS(session.user.id)}`;
}
log('[GET]', url);

const res = await request(url, init, config);
return res;
Expand Down
4 changes: 1 addition & 3 deletions packages/server/src/api/routes/tenants/POST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ import { apiRoutes } from '../../utils/routes/apiRoutes';
*/
export async function POST(
config: Config,
init: RequestInit & { request: Request },
log: (...args: string[]) => void
init: RequestInit & { request: Request }
) {
init.body = init.request.body;
init.method = 'POST';
const url = `${apiRoutes(config).TENANTS}`;
log('[POST]', url);

return await request(url, init, config);
}
4 changes: 1 addition & 3 deletions packages/server/src/api/routes/tenants/[tenantId]/DELETE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ import { apiRoutes } from '../../../utils/routes/apiRoutes';
*/
export async function DELETE(
config: Config,
init: RequestInit & { request: Request },
log: (...args: string[]) => void
init: RequestInit & { request: Request }
) {
const yurl = new URL(init.request.url);
const [tenantId] = yurl.pathname.split('/').reverse();
Expand All @@ -45,7 +44,6 @@ export async function DELETE(

init.method = 'DELETE';
const url = `${apiRoutes(config).TENANT(tenantId)}`;
log('[DELETE]', url);

return await fetch(url, init, config);
}
8 changes: 4 additions & 4 deletions packages/server/src/api/routes/tenants/[tenantId]/GET.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Config } from '../../../../utils/Config';
import fetch from '../../../utils/request';
import request from '../../../utils/request';
import { apiRoutes } from '../../../utils/routes/apiRoutes';

/**
Expand Down Expand Up @@ -38,17 +38,17 @@ import { apiRoutes } from '../../../utils/routes/apiRoutes';
export async function GET(
config: Config,
init: RequestInit & { request: Request },
log: (...args: string[]) => void
log: (message: string | unknown, meta?: Record<string, unknown>) => void
) {
const yurl = new URL(init.request.url);
const [tenantId] = yurl.pathname.split('/').reverse();
if (!tenantId) {
log('[GET] No tenant id provided.');
return new Response(null, { status: 404 });
}

init.method = 'GET';
const url = `${apiRoutes(config).TENANT(tenantId)}`;
log('[GET]', url);

return await fetch(url, init, config);
return await request(url, init, config);
}
4 changes: 1 addition & 3 deletions packages/server/src/api/routes/tenants/[tenantId]/PUT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ import { apiRoutes } from '../../../utils/routes/apiRoutes';
*/
export async function PUT(
config: Config,
init: RequestInit & { request: Request },
log: (...args: string[]) => void
init: RequestInit & { request: Request }
) {
const yurl = new URL(init.request.url);
const [tenantId] = yurl.pathname.split('/').reverse();
Expand All @@ -49,7 +48,6 @@ export async function PUT(
init.body = init.request.body;
init.method = 'PUT';
const url = `${apiRoutes(config).TENANT(tenantId)}`;
log('[PUT]', url);

return await fetch(url, init, config);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,11 @@ import request from '../../../../utils/request';
*/
export async function GET(
config: Config,
init: RequestInit & { request: Request },
log: (...args: string[]) => void
init: RequestInit & { request: Request }
) {
const yurl = new URL(init.request.url);
const [, tenantId] = yurl.pathname.split('/').reverse();
if (!tenantId) {
return new Response(null, { status: 404 });
}

const url = `${apiRoutes(config).TENANT_USERS(tenantId)}`;
log('[GET]', '[TENANT_USERS]', url);
return await request(url, init, config);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ import { Config } from '../../../../../utils/Config';
export async function POST(
config: Config,
session: ActiveSession,
init: RequestInit & { request: Request },
log: (...args: string[]) => void
init: RequestInit & { request: Request }
) {
const yurl = new URL(init.request.url);
const [, tenantId] = yurl.pathname.split('/').reverse();
Expand All @@ -56,7 +55,6 @@ export async function POST(
init.body = JSON.stringify({ email: session.email });
init.method = 'PUT';
const url = apiRoutes(config).TENANT_USERS(tenantId);
log('[PUT]', url);

return await fetch(url, init, config);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,13 @@ import { Config } from '../../../../../utils/Config';

export async function PUT(
config: Config,
init: RequestInit & { request: Request },
log: (...args: string[]) => void
init: RequestInit & { request: Request }
) {
const yurl = new URL(init.request.url);
const [, tenantId] = yurl.pathname.split('/').reverse();
if (!tenantId) {
return new Response(null, { status: 404 });
}

init.method = 'PUT';
const url = `${apiRoutes(config).TENANT_USERS(tenantId)}`;
log('[PUT]', url);

return await fetch(url, init, config);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,15 @@ import { Config } from '../../../../../../utils/Config';

export async function DELETE(
config: Config,
init: RequestInit & { request: Request },
log: (...args: string[]) => void
init: RequestInit & { request: Request }
) {
const yurl = new URL(init.request.url);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [userId, _, tenantId] = yurl.pathname.split('/').reverse();
if (!tenantId) {
return new Response(null, { status: 404 });
}

init.method = 'DELETE';
init.body = JSON.stringify({ email: userId });
const url = `${apiRoutes(config).TENANT_USER(tenantId, userId)}`;
log('[DELETE]', url);

return await fetch(url, init, config);
}
18 changes: 12 additions & 6 deletions packages/server/src/api/routes/tenants/[tenantId]/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,31 @@ const key = 'TENANT_USERS';
export default async function route(request: Request, config: Config) {
const { info } = Logger(
{ ...config, debug: config.debug } as Config,
'[ROUTES]',
`[${key}]`
`[ROUTES][${key}]`
);
const session = await auth(request, config);

if (!session) {
info('401');
return new Response(null, { status: 401 });
}
const yurl = new URL(request.url);
const [, tenantId] = yurl.pathname.split('/').reverse();

if (!tenantId) {
info('No tenant id found in path');
return new Response(null, { status: 404 });
}

switch (request.method) {
case 'GET':
return await GET(config, { request }, info);
return await GET(config, { request });
case 'POST':
return await POST(config, session, { request }, info);
return await POST(config, session, { request });
case 'PUT':
return await PUT(config, { request }, info);
return await PUT(config, { request });
case 'DELETE':
return await DELETE(config, { request }, info);
return await DELETE(config, { request });

default:
return new Response('method not allowed', { status: 405 });
Expand Down
11 changes: 5 additions & 6 deletions packages/server/src/api/routes/tenants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ const key = 'TENANTS';
export default async function route(request: Request, config: Config) {
const { info } = Logger(
{ ...config, debug: config.debug } as Config,
'[ROUTES]',
`[${key}]`
`[ROUTES][${key}]`
);
const session = await auth(request, config);

Expand All @@ -42,13 +41,13 @@ export default async function route(request: Request, config: Config) {
if (isUUID(possibleTenantId)) {
return await TENANT_GET(config, { request }, info);
}
return await GET(config, session, { request }, info);
return await GET(config, session, { request });
case 'POST':
return await POST(config, { request }, info);
return await POST(config, { request });
case 'DELETE':
return await DELETE(config, { request }, info);
return await DELETE(config, { request });
case 'PUT':
return await PUT(config, { request }, info);
return await PUT(config, { request });

default:
return new Response('method not allowed', { status: 405 });
Expand Down
5 changes: 2 additions & 3 deletions packages/server/src/api/routes/users/GET.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,17 @@ import { Config } from '../../../utils/Config';
export async function GET(
config: Config,
init: RequestInit & { request: Request },
log: (...args: string[]) => void
log: (message: string | unknown, meta?: Record<string, unknown>) => void
) {
const yurl = new URL(init.request.url);
const tenantId = yurl.searchParams.get('tenantId');
const tenant = tenantId ?? getTenantFromHttp(init.request.headers);

if (!tenant) {
log('[GET]', '[ERROR]', 'No tenant id provided.');
log('[GET] No tenant id provided.');
return new Response(null, { status: 404 });
}
const url = apiRoutes(config).TENANT_USERS(tenant);
log('[GET]', url);
init.method = 'GET';
return await request(url, init, config);
}
4 changes: 1 addition & 3 deletions packages/server/src/api/routes/users/POST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ import { Config } from '../../../utils/Config';

export async function POST(
config: Config,
init: RequestInit & { request: Request },
log?: (...args: string[]) => void
init: RequestInit & { request: Request }
) {
init.body = init.request.body;
init.method = 'POST';
Expand All @@ -77,7 +76,6 @@ export async function POST(
const tenant = tenantId ?? getTenantFromHttp(init.request.headers);

const url = apiRoutes(config).USERS(tenant ? tenant : undefined);
log && log('[POST]', url);

return await request(url, init, config);
}
5 changes: 1 addition & 4 deletions packages/server/src/api/routes/users/[userId]/PUT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ import { Config } from '../../../../utils/Config';
export async function PUT(
config: Config,
session: void | ActiveSession,
init: RequestInit & { request: Request },
log: (...args: string[]) => void
init: RequestInit & { request: Request }
) {
if (!session) {
return new Response(null, { status: 401 });
Expand All @@ -58,7 +57,5 @@ export async function PUT(

const url = apiRoutes(config).USER(userId);

log('[PUT]', url);

return await fetch(url, init, config);
}
7 changes: 3 additions & 4 deletions packages/server/src/api/routes/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ const key = 'USERS';
export default async function route(request: Request, config: Config) {
const { info } = Logger(
{ ...config, debug: config.debug } as Config,
'[ROUTES]',
`[${key}]`
`[ROUTES][${key}]`
);
const session = await auth(request, config);

switch (request.method) {
case 'GET':
return await GET(config, { request }, info);
case 'POST':
return await POST(config, { request }, info);
return await POST(config, { request });
case 'PUT':
return await PUT(config, session, { request }, info);
return await PUT(config, session, { request });

default:
return new Response('method not allowed', { status: 405 });
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/api/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default async function auth(
info('checking auth');

const sessionUrl = `${config.api.basePath}/auth/session`;
info('using session', sessionUrl);
info(`using session${sessionUrl}`);
// handle the pass through with posts
req.headers.delete('content-length');

Expand Down
12 changes: 9 additions & 3 deletions packages/server/src/api/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@ export default async function request(
params.duplex = 'half';
}

info(`[${params.method ?? 'GET'}]`, url);
const res = await fetch(url, { ...params }).catch((e) => {
error('An error has occurred in the fetch', e);
error('An error has occurred in the fetch', {
message: e.message,
stack: e.stack,
});
return new Response(
'An unexpected (most likely configuration) problem has occurred',
{ status: 500 }
);
});
const loggingRes = typeof res?.clone === 'function' ? res?.clone() : null;
info('[Response]', res?.status, res?.statusText, await loggingRes?.text());
info(`[${params.method ?? 'GET'}] ${url}`, {
status: res?.status,
statusText: res?.statusText,
text: await loggingRes?.text(),
});
return res;
}
Loading