habppyar commited on
Commit
28a739d
·
verified ·
1 Parent(s): 625a577

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +18 -37
server.js CHANGED
@@ -1,49 +1,30 @@
1
  const { spawn } = require('child_process');
2
- const fs = require('fs');
3
  const path = require('path');
 
 
4
 
5
  const PORT = process.env.PORT || 7860;
6
  const HOST = '0.0.0.0';
7
  const opencodeBin = path.join(__dirname, 'node_modules', '.bin', 'opencode');
8
 
9
- // 运行时设置持久化目录
10
- const DATA_MOUNT = '/data';
11
- const LOCAL_SHARE = path.join(process.env.HOME, '.local', 'share', 'opencode');
12
- const CONFIG_DIR = path.join(process.env.HOME, '.config', 'opencode');
 
 
13
 
14
- function setupPersistentDirs() {
15
- try {
16
- // 确保 /data 存在(运行时由 HF 挂载)
17
- if (!fs.existsSync(DATA_MOUNT)) {
18
- console.warn(`Warning: ${DATA_MOUNT} does not exist, using ephemeral storage.`);
19
- return;
20
- }
21
-
22
- const dataLocal = path.join(DATA_MOUNT, 'opencode_local');
23
- const dataConfig = path.join(DATA_MOUNT, 'opencode_config');
24
-
25
- // 创建持久化目录
26
- if (!fs.existsSync(dataLocal)) fs.mkdirSync(dataLocal, { recursive: true });
27
- if (!fs.existsSync(dataConfig)) fs.mkdirSync(dataConfig, { recursive: true });
28
-
29
- // 删除原有目录(如果存在且不是软链接)
30
- if (fs.existsSync(LOCAL_SHARE) && !fs.lstatSync(LOCAL_SHARE).isSymbolicLink()) {
31
- fs.rmSync(LOCAL_SHARE, { recursive: true, force: true });
32
- }
33
- if (fs.existsSync(CONFIG_DIR) && !fs.lstatSync(CONFIG_DIR).isSymbolicLink()) {
34
- fs.rmSync(CONFIG_DIR, { recursive: true, force: true });
35
- }
36
-
37
- // 创建软链接
38
- fs.symlinkSync(dataLocal, LOCAL_SHARE, 'dir');
39
- fs.symlinkSync(dataConfig, CONFIG_DIR, 'dir');
40
- console.log('Persistent storage linked to /data');
41
- } catch (err) {
42
- console.error('Failed to set up persistent directories:', err.message);
43
- }
44
  }
45
-
46
- setupPersistentDirs();
47
 
48
  const env = {
49
  ...process.env,
 
1
  const { spawn } = require('child_process');
 
2
  const path = require('path');
3
+ const fs = require('fs');
4
+ const os = require('os');
5
 
6
  const PORT = process.env.PORT || 7860;
7
  const HOST = '0.0.0.0';
8
  const opencodeBin = path.join(__dirname, 'node_modules', '.bin', 'opencode');
9
 
10
+ // 设置持久化目录:如果 /data/opencode_local 存在,则创建软链接
11
+ const homeDir = os.homedir();
12
+ const dataLocal = '/data/opencode_local';
13
+ const dataConfig = '/data/opencode_config';
14
+ const targetLocal = path.join(homeDir, '.local', 'share', 'opencode');
15
+ const targetConfig = path.join(homeDir, '.config', 'opencode');
16
 
17
+ if (fs.existsSync(dataLocal)) {
18
+ // 移除原有的目录或链接
19
+ if (fs.existsSync(targetLocal)) fs.rmSync(targetLocal, { recursive: true, force: true });
20
+ fs.symlinkSync(dataLocal, targetLocal, 'dir');
21
+ console.log(`Linked ${targetLocal} -> ${dataLocal}`);
22
+ }
23
+ if (fs.existsSync(dataConfig)) {
24
+ if (fs.existsSync(targetConfig)) fs.rmSync(targetConfig, { recursive: true, force: true });
25
+ fs.symlinkSync(dataConfig, targetConfig, 'dir');
26
+ console.log(`Linked ${targetConfig} -> ${dataConfig}`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  }
 
 
28
 
29
  const env = {
30
  ...process.env,