Skip to content

Commit

Permalink
Merge pull request #271 from TBXark/dev
Browse files Browse the repository at this point in the history
fix: 修复1.8.0 pre-release已知BUG
  • Loading branch information
TBXark authored Aug 14, 2024
2 parents 0ec5b5b + c271e6d commit b8bf5cd
Show file tree
Hide file tree
Showing 54 changed files with 2,384 additions and 696 deletions.
2 changes: 1 addition & 1 deletion adapter/docker/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
config
build
12 changes: 5 additions & 7 deletions adapter/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
FROM node:18 AS build

FROM node:20
WORKDIR /app
COPY package*.json index.js ./
RUN npm install
COPY ./build/index.js /app/index.js
RUN apt-get update && apt-get install -y sqlite3
VOLUME /app/config
ENV CONFIG_PATH=/app/config.json
ENV CACHE_PATH=/app/cache.json
EXPOSE 8787

CMD [ "node", "index.js" ]
CMD [ "node", "index.js" ]
13 changes: 13 additions & 0 deletions adapter/docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Docker

### 1. Build image

```bash
yarn && yarn run docker
```

### 2. Run container

```bash
docker run -v $(pwd)/config.json:/app/config.json -p 8787:8787 chatgpt-telegram-bot:latest --name chatgpt-telegram-bot
```
3 changes: 3 additions & 0 deletions adapter/docker/esbuild.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {build} from '../script/esbuild.config.js';

build('../../dist/buildinfo.json', 'build/index.js').catch(() => process.exit(1));
43 changes: 12 additions & 31 deletions adapter/docker/index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,23 @@
import adapter from 'cloudflare-worker-adapter';
import {default as worker} from 'chatgpt-telegram-workers';
import {default as adapter} from 'cloudflare-worker-adapter';
import {default as worker} from '../../main.js';
import fs from 'fs';
import {createCache} from 'cloudflare-worker-adapter/cache';


const config = JSON.parse(fs.readFileSync('./config/config.json', 'utf-8'));

// 配置数据库
let cache;
switch (config?.database?.type) {
case 'local':
// eslint-disable-next-line no-case-declarations
const {LocalCache} = await import('cloudflare-worker-adapter/cache/local.js');
cache = new LocalCache(config.database.uri);
break;
case 'sqlite':
// eslint-disable-next-line no-case-declarations
const {SqliteCache} = await import('cloudflare-worker-adapter/cache/sqlite.js');
cache = new SqliteCache(config.database.uri);
break;
case 'redis':
// eslint-disable-next-line no-case-declarations
const {RedisCache} = await import('cloudflare-worker-adapter/cache/redis.js');
cache = new RedisCache(config.database.uri);
break;
default:
// eslint-disable-next-line no-case-declarations
const {MemoryCache} = await import('cloudflare-worker-adapter/cache/memory.js');
cache = new MemoryCache();
break;
}
const {
CONFIG_PATH = './config/config.json',
TOML_PATH = './config/config.toml',
} = process.env;

const config = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8'));
const cache = await createCache(config?.database?.type, config?.database);
console.log(`database: ${config?.database?.type} is ready`);

adapter.startServer(
8787,
'127.0.0.1',
'./config/config.toml',
{DATABASE: cache},
{server: config.server},
TOML_PATH,
{ DATABASE: cache },
{ server: config.server },
worker.fetch,
);
12 changes: 8 additions & 4 deletions adapter/docker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
"main": "index.js",
"type": "module",
"scripts": {
"start": "node index.js"
"start": "node index.js",
"build": "node esbuild.config.js",
"docker": "npm run build && docker build -t chatgpt-telegram-bot:latest .",
"lint": "eslint --fix index.js"
},
"author": "TBXark",
"license": "MIT",
"dependencies": {
"chatgpt-telegram-workers": "github:TBXark/ChatGPT-Telegram-Workers#dev",
"cloudflare-worker-adapter": "github:TBXark/cloudflare-worker-adapter"
"cloudflare-worker-adapter": "1.0.3"
},
"devDependencies": {}
"devDependencies": {
"@eslint/js": "^9.8.0"
}
}
34 changes: 18 additions & 16 deletions adapter/docker/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
# yarn lockfile v1


"@eslint/js@^9.8.0":
version "9.9.0"
resolved "https://registry.yarnpkg.com/@eslint/js/-/js-9.9.0.tgz#d8437adda50b3ed4401964517b64b4f59b0e2638"
integrity sha512-hhetes6ZHP3BlXLxmd8K2SNgkhNSi+UcecbnwWKwpP7kyi/uC75DJ1lOOBO3xrC4jyojtGE3YxKZPHfk4yrgug==

"@gar/promisify@^1.0.1":
version "1.1.3"
resolved "https://registry.yarnpkg.com/@gar/promisify/-/promisify-1.1.3.tgz#555193ab2e3bb3b6adc3d551c9c030d9e860daf6"
Expand Down Expand Up @@ -144,10 +149,6 @@ cacache@^15.2.0:
tar "^6.0.2"
unique-filename "^1.1.1"

"chatgpt-telegram-workers@github:TBXark/ChatGPT-Telegram-Workers#dev":
version "1.7.0"
resolved "https://codeload.github.com/TBXark/ChatGPT-Telegram-Workers/tar.gz/36495eb82a134f73f8eb4518bd5cca7692b3e4a5"

chownr@^1.1.1:
version "1.1.4"
resolved "https://registry.yarnpkg.com/chownr/-/chownr-1.1.4.tgz#6fc9d7b42d32a583596337666e7d08084da2cc6b"
Expand All @@ -163,9 +164,10 @@ clean-stack@^2.0.0:
resolved "https://registry.yarnpkg.com/clean-stack/-/clean-stack-2.2.0.tgz#ee8472dbb129e727b31e8a10a427dee9dfe4008b"
integrity sha512-4diC9HaTE+KRAMWhDhrGOECgWZxoevMc5TlkObMqNSsVU62PYzXZ/SMTjzyGAFF1YusgxGcSWTEXBhp0CPwQ1A==

"cloudflare-worker-adapter@github:TBXark/cloudflare-worker-adapter":
version "1.0.1"
resolved "https://codeload.github.com/TBXark/cloudflare-worker-adapter/tar.gz/48062ba3705f5a282e9d45c85e84d8c5017231b5"
[email protected]:
version "1.0.3"
resolved "https://registry.yarnpkg.com/cloudflare-worker-adapter/-/cloudflare-worker-adapter-1.0.3.tgz#c87dda4b9f036c1b6887e36b52dbc4da8de198a0"
integrity sha512-Eyd2JsmGvzSt46fG2spccARgIhlcTXcpPVGIwxpG9ocC229LVYtw+JZ1R6FbMUVplFjrPqNJ2zK+tqX2pkBoKw==
dependencies:
ioredis "^5.4.1"
node-fetch "^3.3.2"
Expand Down Expand Up @@ -198,9 +200,9 @@ data-uri-to-buffer@^4.0.0:
integrity sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==

debug@4, debug@^4.3.3, debug@^4.3.4:
version "4.3.5"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.5.tgz#e83444eceb9fedd4a1da56d671ae2446a01a6e1e"
integrity sha512-pt0bNEmneDIvdL1Xsd9oDQ/wrQRkXDT4AUWlNZNPKvW5x/jyO9VFXkJUP07vQ2upmw5PlaITaPKc31jK13V+jg==
version "4.3.6"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.6.tgz#2ab2c38fbaffebf8aa95fdfe6d88438c7a13c52b"
integrity sha512-O/09Bd4Z1fBrU4VzkhFqVgpPzaGbw6Sm9FEkBT1A/YBXQFGuuSxa1dN2nxgxS34JmKXqYx8CZAwEVoJFImUXIg==
dependencies:
ms "2.1.2"

Expand Down Expand Up @@ -613,9 +615,9 @@ node-abi@^3.3.0:
semver "^7.3.5"

node-addon-api@^7.0.0:
version "7.1.0"
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-7.1.0.tgz#71f609369379c08e251c558527a107107b5e0fdb"
integrity sha512-mNcltoe1R8o7STTegSOHdnJNN7s5EUvhoS7ShnTHDyOSd+8H+UdWODq6qSv67PjC8Zc5JRT8+oLAMCr0SIXw7g==
version "7.1.1"
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-7.1.1.tgz#1aba6693b0f255258a049d621329329322aad558"
integrity sha512-5m3bsyrjFWE1xf7nz7YXdN4udnVtXK6/Yfgn5qnahL6bCkf2yKt4k3nuTKAtT4r3IG8JNR2ncsIMdZuAzJjHQQ==

node-domexception@^1.0.0:
version "1.0.0"
Expand Down Expand Up @@ -776,9 +778,9 @@ safe-buffer@^5.0.1, safe-buffer@~5.2.0:
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==

semver@^7.3.5:
version "7.6.2"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.6.2.tgz#1e3b34759f896e8f14d6134732ce798aeb0c6e13"
integrity sha512-FNAIBWCx9qcRhoHcgcJ0gvU7SN1lYU2ZXuSfl04bSC5OpvDHFyJCjdNHomPXxjQlCBU67YW64PzY7/VIEH7F2w==
version "7.6.3"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.6.3.tgz#980f7b5550bc175fb4dc09403085627f9eb33143"
integrity sha512-oVekP1cKtI+CTDvHWYFUcMtsK/00wmAEfyqKfNdARm8u1wNVhSgaX7A8d4UuIlUI5e84iEwOhs7ZPYRmzU9U6A==

set-blocking@^2.0.0:
version "2.0.0"
Expand Down
62 changes: 17 additions & 45 deletions adapter/local/index.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,39 @@
import adapter, {bindGlobal} from 'cloudflare-worker-adapter';
import {MemoryCache} from 'cloudflare-worker-adapter/cache/memory.js';
import fs from 'fs';
import { HttpsProxyAgent } from 'https-proxy-agent';
import {HttpsProxyAgent} from 'https-proxy-agent';
import fetch from 'node-fetch';
import {default as worker} from '../../main.js';
import {ENV} from '../../src/config/env.js';
import {createCache} from 'cloudflare-worker-adapter/cache';


const config = JSON.parse(fs.readFileSync('./config.json', 'utf-8'));


// 配置数据库
let cache = new MemoryCache();
switch (config?.database?.type) {
case 'local':
// eslint-disable-next-line no-case-declarations
const {LocalCache} = await import('cloudflare-worker-adapter/cache/local.js');
cache = new LocalCache(config.database.uri);
break;
case 'sqlite':
// eslint-disable-next-line no-case-declarations
const {SqliteCache} = await import('cloudflare-worker-adapter/cache/sqlite.js');
cache = new SqliteCache(config.database.uri);
break;
case 'redis':
// eslint-disable-next-line no-case-declarations
const {RedisCache} = await import('cloudflare-worker-adapter/cache/redis.js');
cache = new RedisCache(config.database.uri);
break;
default:
// eslint-disable-next-line no-case-declarations
const {MemoryCache} = await import('cloudflare-worker-adapter/cache/memory.js');
cache = new MemoryCache();
break;
}

const cache = await createCache(config?.database?.type, config?.database);
console.log(`database: ${config?.database?.type} is ready`);

// 配置代理
const proxy = config.https_proxy || process.env.https_proxy || process.env.HTTPS_PROXY;
const proxy = config?.https_proxy || process.env.https_proxy || process.env.HTTPS_PROXY;
if (proxy) {
console.log(`https proxy: ${proxy}`);
const agent = new HttpsProxyAgent(proxy);
const proxyFetch = async (url, init) => {
return fetch(url, {agent, ...init});
};
bindGlobal({
fetch: proxyFetch,
});
console.log(`https proxy: ${proxy}`);
const agent = new HttpsProxyAgent(proxy);
const proxyFetch = async (url, init) => {
return fetch(url, {agent, ...init});
};
bindGlobal({
fetch: proxyFetch,
});
}


// 配置版本信息
try {
const buildInfo = JSON.parse(fs.readFileSync('../../dist/buildinfo.json', 'utf-8'));
ENV.BUILD_TIMESTAMP = buildInfo.timestamp;
ENV.BUILD_VERSION = buildInfo.sha;
console.log(buildInfo);
const buildInfo = JSON.parse(fs.readFileSync('../../dist/buildinfo.json', 'utf-8'));
ENV.BUILD_TIMESTAMP = buildInfo.timestamp;
ENV.BUILD_VERSION = buildInfo.sha;
console.log(buildInfo);
} catch (e) {
console.log(e);
console.log(e);
}


// 延迟加载 ../main.js, 防止ENV过早初始化
adapter.startServer(
config.port || 8787,
Expand Down
22 changes: 11 additions & 11 deletions adapter/local/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ clean-stack@^2.0.0:
integrity sha512-4diC9HaTE+KRAMWhDhrGOECgWZxoevMc5TlkObMqNSsVU62PYzXZ/SMTjzyGAFF1YusgxGcSWTEXBhp0CPwQ1A==

"cloudflare-worker-adapter@github:TBXark/cloudflare-worker-adapter":
version "1.0.1"
resolved "https://codeload.github.com/TBXark/cloudflare-worker-adapter/tar.gz/48062ba3705f5a282e9d45c85e84d8c5017231b5"
version "1.0.2"
resolved "https://codeload.github.com/TBXark/cloudflare-worker-adapter/tar.gz/0212060e81af289d719a1ecc1ff98732004d9a3c"
dependencies:
ioredis "^5.4.1"
node-fetch "^3.3.2"
Expand Down Expand Up @@ -201,9 +201,9 @@ data-uri-to-buffer@^4.0.0:
integrity sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==

debug@4, debug@^4.3.3, debug@^4.3.4:
version "4.3.5"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.5.tgz#e83444eceb9fedd4a1da56d671ae2446a01a6e1e"
integrity sha512-pt0bNEmneDIvdL1Xsd9oDQ/wrQRkXDT4AUWlNZNPKvW5x/jyO9VFXkJUP07vQ2upmw5PlaITaPKc31jK13V+jg==
version "4.3.6"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.6.tgz#2ab2c38fbaffebf8aa95fdfe6d88438c7a13c52b"
integrity sha512-O/09Bd4Z1fBrU4VzkhFqVgpPzaGbw6Sm9FEkBT1A/YBXQFGuuSxa1dN2nxgxS34JmKXqYx8CZAwEVoJFImUXIg==
dependencies:
ms "2.1.2"

Expand Down Expand Up @@ -624,9 +624,9 @@ node-abi@^3.3.0:
semver "^7.3.5"

node-addon-api@^7.0.0:
version "7.1.0"
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-7.1.0.tgz#71f609369379c08e251c558527a107107b5e0fdb"
integrity sha512-mNcltoe1R8o7STTegSOHdnJNN7s5EUvhoS7ShnTHDyOSd+8H+UdWODq6qSv67PjC8Zc5JRT8+oLAMCr0SIXw7g==
version "7.1.1"
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-7.1.1.tgz#1aba6693b0f255258a049d621329329322aad558"
integrity sha512-5m3bsyrjFWE1xf7nz7YXdN4udnVtXK6/Yfgn5qnahL6bCkf2yKt4k3nuTKAtT4r3IG8JNR2ncsIMdZuAzJjHQQ==

node-domexception@^1.0.0:
version "1.0.0"
Expand Down Expand Up @@ -787,9 +787,9 @@ safe-buffer@^5.0.1, safe-buffer@~5.2.0:
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==

semver@^7.3.5:
version "7.6.2"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.6.2.tgz#1e3b34759f896e8f14d6134732ce798aeb0c6e13"
integrity sha512-FNAIBWCx9qcRhoHcgcJ0gvU7SN1lYU2ZXuSfl04bSC5OpvDHFyJCjdNHomPXxjQlCBU67YW64PzY7/VIEH7F2w==
version "7.6.3"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.6.3.tgz#980f7b5550bc175fb4dc09403085627f9eb33143"
integrity sha512-oVekP1cKtI+CTDvHWYFUcMtsK/00wmAEfyqKfNdARm8u1wNVhSgaX7A8d4UuIlUI5e84iEwOhs7ZPYRmzU9U6A==

set-blocking@^2.0.0:
version "2.0.0"
Expand Down
3 changes: 3 additions & 0 deletions adapter/local_v2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build
config.json
cache.json
6 changes: 6 additions & 0 deletions adapter/local_v2/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM node:20
WORKDIR /app
COPY ./build/index.js /app/index.js
ENV CONFIG_PATH=/app/config.json
ENV CACHE_PATH=/app/cache.json
CMD [ "node", "index.js" ]
11 changes: 11 additions & 0 deletions adapter/local_v2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 本地部署(无需域名版本)

## 使用步骤
1. 使用 `npm run config` 将toml配置文件转换为json配置文件

### 本地运行
1. 使用 `npm run start` 启动服务

### Docker运行
1. 使用 `npm run docker` 构建镜像
2. 使用 `docker run -v $(pwd)/config.json:/app/config.json -v $(pwd)/cache.json:/app/cache.json chatgpt-telegram-bot:latest --name chatgpt-telegram-bot` 运行容器
7 changes: 7 additions & 0 deletions adapter/local_v2/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import fs from 'node:fs';
import toml from 'toml';

const TOML_PATH = '../../wrangler.toml';
const raw = fs.readFileSync(TOML_PATH, 'utf-8');
const config = toml.parse(raw);
fs.writeFileSync('./config.json', JSON.stringify(config, null, ' '));
3 changes: 3 additions & 0 deletions adapter/local_v2/esbuild.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {build} from '../script/esbuild.config.js';

build('../../dist/buildinfo.json', 'build/index.js').catch(() => process.exit(1));
Loading

0 comments on commit b8bf5cd

Please sign in to comment.