feat: integrate Convex API and add N5 proxy functionality
- Added Convex client setup in a new module for handling Convex API interactions. - Implemented N5Proxy to handle requests for the /n5/:slug route, querying Convex for application links. - Updated app context to include Convex client and API. - Adjusted routing to support new Convex API endpoints. - Enhanced error handling for missing applications in the N5 proxy.
This commit is contained in:
31
src/modules/n5/index.ts
Normal file
31
src/modules/n5/index.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { convex, convexApi } from '@/app.ts';
|
||||
import { IncomingMessage, ServerResponse } from 'http';
|
||||
|
||||
type ProxyOptions = {
|
||||
createNotFoundPage: (msg?: string) => any;
|
||||
};
|
||||
// /n5/:slug
|
||||
export const N5Proxy = async (req: IncomingMessage, res: ServerResponse, opts?: ProxyOptions) => {
|
||||
const { url } = req;
|
||||
const _url = new URL(url || '', `http://localhost`);
|
||||
const { pathname, searchParams } = _url;
|
||||
let [user, app, userAppKey] = pathname.split('/').slice(1);
|
||||
if (!app) {
|
||||
opts?.createNotFoundPage?.('应用未找到');
|
||||
return false;
|
||||
}
|
||||
const convexResult = await convex.query(convexApi.nCode.getBySlug, { slug: app })
|
||||
if (!convexResult) {
|
||||
opts?.createNotFoundPage?.('应用未找到');
|
||||
return false;
|
||||
}
|
||||
const link = convexResult.data.link;
|
||||
if (!link) {
|
||||
opts?.createNotFoundPage?.('应用未找到');
|
||||
return false;
|
||||
}
|
||||
res.writeHead(302, {
|
||||
Location: link,
|
||||
});
|
||||
res.end();
|
||||
};
|
||||
Reference in New Issue
Block a user