forked from EdgarHnd/markprompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
38 lines (31 loc) · 1.08 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { NextFetchEvent, NextRequest, NextResponse } from 'next/server';
import CompletionsMiddleware from './lib/middleware/completions';
import AppMiddleware from './lib/middleware/app';
import { getHost } from './lib/utils';
import TrainMiddleware from './lib/middleware/train';
export const config = {
matcher: [
'/((?!_next/|_proxy/|_auth/|_root/|_static|static|_vercel|[\\w-]+\\.\\w+).*)',
],
};
export default async function middleware(req: NextRequest, ev: NextFetchEvent) {
const hostname = req.headers.get('host');
if (req.method === 'OPTIONS') {
return new Response('ok', { status: 200 });
}
if (hostname === getHost()) {
return AppMiddleware(req);
}
if (hostname === 'api.markprompt.com' || hostname === 'api.localhost:3000') {
const path = req.nextUrl.pathname;
if (
path?.startsWith('/completions') ||
path?.startsWith('/v1/completions')
) {
return CompletionsMiddleware(req);
} else if (path?.startsWith('/train') || path?.startsWith('/v1/train')) {
return TrainMiddleware(req);
}
}
return NextResponse.next();
}