Skip to content

Commit

Permalink
♻️ refactor: refactor the api router to app route handlers (lobehub#254)
Browse files Browse the repository at this point in the history
* ♻️ refactor: refactor the api router to app route handlers

* 💚 refactor: refactor the runtime config

* 📝 docs: update document
  • Loading branch information
arvinxx authored Sep 30, 2023
1 parent ba42d0a commit f032112
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 73 deletions.
13 changes: 0 additions & 13 deletions docs/Environment-Variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ LobeChat provides additional configuration options during deployment, which can
- [`AGENTS_INDEX_URL`](#agents_index_url)
- [Data Analytics](#data-analytics)
- [Posthog Analytics](#posthog-analytics)
- [Development Environment](#development-environment)
- [`DEV_API_END_PORT_URL`](#dev_api_end_port_url)

## General Variables

Expand Down Expand Up @@ -130,16 +128,5 @@ If you need to use Azure OpenAI to provide model services, you can refer to the
- Default: -
- Example: `1`

<br/>

## Development Environment

### `DEV_API_END_PORT_URL`

- Type: Optional
- Description: Define the proxy address of the LobeChat server request forwarding. Using this variable can conveniently forward requests to the line during development. See [configuration code](https://github.com/lobehub/lobe-chat/blob/main/next.config.mjs#L29-L38)
- Default: -
- Example: `https://chat-preview.lobehub.com`

[azure-api-verion-url]: https://docs.microsoft.com/zh-cn/azure/developer/javascript/api-reference/es-modules/azure-sdk/ai-translation/translationconfiguration?view=azure-node-latest#api-version
[openai-api-page]: https://platform.openai.com/account/api-keys
11 changes: 0 additions & 11 deletions docs/Environment-Variable.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ LobeChat 在部署时提供了一些额外的配置项,使用环境变量进
- [Vercel Analytics](#vercel-analytics)
- [Mixpanel Analytics](#mixpanel-analytics)
- [Posthog Analytics](#posthog-analytics)
- [开发环境](#开发环境)
- [`DEV_API_END_PORT_URL`](#dev_api_end_port_url)

## 通用变量

Expand Down Expand Up @@ -169,15 +167,6 @@ LobeChat 在部署时提供了一些额外的配置项,使用环境变量进
- 默认值: -
- 示例:`1`

## 开发环境

### `DEV_API_END_PORT_URL`

- 类型:可选
- 描述:定义 LobeChat 服务端请求转发的代理地址,使用该变量可以方便开发时将请求转发到线上。详见[配置代码](https://github.com/lobehub/lobe-chat/blob/main/next.config.mjs#L29-L38)
- 默认值:-
- 示例:`https://chat-preview.lobehub.com`

[azure-api-verion-url]: https://docs.microsoft.com/zh-cn/azure/developer/javascript/api-reference/es-modules/azure-sdk/ai-translation/translationconfiguration?view=azure-node-latest#api-version
[mixpanel-analytics-url]: https://mixpanel.com
[mixpanel-project-url]: https://mixpanel.com/settings/project
Expand Down
18 changes: 0 additions & 18 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import nextPWA from 'next-pwa';

const isProd = process.env.NODE_ENV === 'production';
const DEV_API_END_PORT_URL = process.env.DEV_API_END_PORT_URL || '';

const withPWA = nextPWA({
dest: 'public',
Expand All @@ -12,7 +11,6 @@ const withPWA = nextPWA({
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
pageExtensions: ['tsx', 'api.ts'],
// not sure why antd-style cause multi ThemeProvider instance
// So we need to transpile it to lib mode
transpilePackages: ['@lobehub/ui', 'antd-style'],
Expand All @@ -25,22 +23,6 @@ const nextConfig = {
return config;
},

async rewrites() {
return [
{
source: '/api/openai/chat-dev',
destination: `${DEV_API_END_PORT_URL}/api/openai/chat`,
},
{
source: '/api/openai/models-dev',
destination: `${DEV_API_END_PORT_URL}/api/openai/models`,
},
{
source: '/api/plugins-dev',
destination: `${DEV_API_END_PORT_URL}/api/plugins`,
},
];
},
env: {
AGENTS_INDEX_URL: process.env.AGENTS_INDEX_URL,
PLUGINS_INDEX_URL: process.env.PLUGINS_INDEX_URL,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { getOpenAIAuthFromRequest } from '@/const/fetch';
import { ErrorType } from '@/types/fetch';
import { OpenAIStreamPayload } from '@/types/openai';

import { checkAuth } from '../auth';
import { checkAuth } from '../../auth';
import { createAzureOpenai } from '../createAzureOpenai';
import { createChatCompletion } from '../createChatCompletion';
import { createErrorResponse } from '../error';
import { createAzureOpenai } from './createAzureOpenai';
import { createOpenai } from './createOpenai';
import { createOpenai } from '../createOpenai';
import { createErrorResponse } from '../errorResponse';

export const runtime = 'edge';

export default async function handler(req: Request) {
export const POST = async (req: Request) => {
const payload = (await req.json()) as OpenAIStreamPayload;

const { apiKey, accessCode, endpoint, useAzure, apiVersion } = getOpenAIAuthFromRequest(req);
Expand All @@ -36,4 +36,4 @@ export default async function handler(req: Request) {
}

return createChatCompletion({ openai, payload });
}
};
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OpenAIStream, StreamingTextResponse } from 'ai';
import OpenAI from 'openai';

import { createErrorResponse } from '@/pages/api/error';
import { createErrorResponse } from '@/app/api/openai/errorResponse';
import { ChatErrorType } from '@/types/fetch';
import { OpenAIStreamPayload } from '@/types/openai';

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getOpenAIAuthFromRequest } from '@/const/fetch';

import { createOpenai } from './createOpenai';
import { createOpenai } from '../createOpenai';

export const runtime = 'edge';

export default async function handler(req: Request) {
export const POST = async (req: Request) => {
const { apiKey, endpoint } = getOpenAIAuthFromRequest(req);

const openAI = createOpenai(apiKey, endpoint);
Expand All @@ -14,4 +14,4 @@ export default async function handler(req: Request) {
const modelList = res.data.map((i) => i.id);

return new Response(JSON.stringify(modelList));
}
};
7 changes: 7 additions & 0 deletions src/app/api/plugins/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createLobeChatPluginGateway } from '@lobehub/chat-plugins-gateway';

import { PLUGINS_INDEX_URL } from '@/const/url';

export const runtime = 'edge';

export const POST = createLobeChatPluginGateway({ pluginsIndexUrl: PLUGINS_INDEX_URL });
9 changes: 0 additions & 9 deletions src/pages/api/plugins.api.ts

This file was deleted.

15 changes: 3 additions & 12 deletions src/services/_url.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
const isDev = process.env.NODE_ENV === 'development';

const prefix = isDev ? '-dev' : '';

export const URLS = {
plugins: '/api/plugins' + prefix,
plugins: '/api/plugins',
};

export const OPENAI_URLS = {
chat: '/api/openai/chat' + prefix,
models: '/api/openai/models' + prefix,
};

export const AZURE_OPENAI_URLS = {
chat: '/api/azure-openai/chat' + prefix,
models: '/api/azure-openai/models' + prefix,
chat: '/api/openai/chat',
models: '/api/openai/models',
};

0 comments on commit f032112

Please sign in to comment.