Spaces:
Sleeping
Sleeping
Vu Minh Chien
commited on
Commit
Β·
d0e91b6
1
Parent(s):
cd66431
Fix file permissions issue in Docker container - add fallback to in-memory storage
Browse files- Dockerfile +4 -0
- server.js +23 -1
Dockerfile
CHANGED
@@ -17,6 +17,10 @@ COPY . .
|
|
17 |
RUN addgroup -g 1001 -S nodejs
|
18 |
RUN adduser -S nodejs -u 1001
|
19 |
|
|
|
|
|
|
|
|
|
20 |
# Change ownership of the app directory to the nodejs user
|
21 |
RUN chown -R nodejs:nodejs /app
|
22 |
|
|
|
17 |
RUN addgroup -g 1001 -S nodejs
|
18 |
RUN adduser -S nodejs -u 1001
|
19 |
|
20 |
+
# Ensure devices.json exists and has proper permissions
|
21 |
+
RUN touch /app/devices.json && \
|
22 |
+
chmod 664 /app/devices.json
|
23 |
+
|
24 |
# Change ownership of the app directory to the nodejs user
|
25 |
RUN chown -R nodejs:nodejs /app
|
26 |
|
server.js
CHANGED
@@ -62,6 +62,9 @@ const path = require('path');
|
|
62 |
// File storage for FCM tokens
|
63 |
const DEVICES_FILE = path.join(__dirname, 'devices.json');
|
64 |
|
|
|
|
|
|
|
65 |
// Load devices from file or create empty storage
|
66 |
let deviceTokens = new Map();
|
67 |
|
@@ -74,20 +77,37 @@ function loadDevicesFromFile() {
|
|
74 |
console.log(`π Loaded ${deviceTokens.size} devices from file`);
|
75 |
} else {
|
76 |
console.log('π No devices file found, starting fresh');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
}
|
78 |
} catch (error) {
|
79 |
console.error('β Error loading devices file:', error);
|
|
|
|
|
80 |
deviceTokens = new Map();
|
81 |
}
|
82 |
}
|
83 |
|
84 |
function saveDevicesToFile() {
|
|
|
|
|
|
|
|
|
|
|
85 |
try {
|
86 |
const devicesArray = Array.from(deviceTokens.entries());
|
87 |
fs.writeFileSync(DEVICES_FILE, JSON.stringify(devicesArray, null, 2));
|
88 |
console.log(`πΎ Saved ${deviceTokens.size} devices to file`);
|
89 |
} catch (error) {
|
90 |
console.error('β Error saving devices file:', error);
|
|
|
|
|
91 |
}
|
92 |
}
|
93 |
|
@@ -101,7 +121,9 @@ app.get('/', (req, res) => {
|
|
101 |
res.json({
|
102 |
message: 'Houzou Medical Notification Server',
|
103 |
status: 'running',
|
104 |
-
timestamp: new Date().toISOString()
|
|
|
|
|
105 |
});
|
106 |
});
|
107 |
|
|
|
62 |
// File storage for FCM tokens
|
63 |
const DEVICES_FILE = path.join(__dirname, 'devices.json');
|
64 |
|
65 |
+
// Fallback to in-memory storage if file operations fail
|
66 |
+
let fileOperationsEnabled = true;
|
67 |
+
|
68 |
// Load devices from file or create empty storage
|
69 |
let deviceTokens = new Map();
|
70 |
|
|
|
77 |
console.log(`π Loaded ${deviceTokens.size} devices from file`);
|
78 |
} else {
|
79 |
console.log('π No devices file found, starting fresh');
|
80 |
+
// Try to create an empty file to test write permissions
|
81 |
+
try {
|
82 |
+
fs.writeFileSync(DEVICES_FILE, '[]');
|
83 |
+
console.log('π Created empty devices file');
|
84 |
+
} catch (writeError) {
|
85 |
+
console.warn('β οΈ Cannot create devices file, will use in-memory storage only');
|
86 |
+
fileOperationsEnabled = false;
|
87 |
+
}
|
88 |
}
|
89 |
} catch (error) {
|
90 |
console.error('β Error loading devices file:', error);
|
91 |
+
console.log('β οΈ Using in-memory storage only');
|
92 |
+
fileOperationsEnabled = false;
|
93 |
deviceTokens = new Map();
|
94 |
}
|
95 |
}
|
96 |
|
97 |
function saveDevicesToFile() {
|
98 |
+
if (!fileOperationsEnabled) {
|
99 |
+
console.log('πΎ File operations disabled, keeping devices in memory only');
|
100 |
+
return;
|
101 |
+
}
|
102 |
+
|
103 |
try {
|
104 |
const devicesArray = Array.from(deviceTokens.entries());
|
105 |
fs.writeFileSync(DEVICES_FILE, JSON.stringify(devicesArray, null, 2));
|
106 |
console.log(`πΎ Saved ${deviceTokens.size} devices to file`);
|
107 |
} catch (error) {
|
108 |
console.error('β Error saving devices file:', error);
|
109 |
+
console.log('β οΈ Disabling file operations, using in-memory storage only');
|
110 |
+
fileOperationsEnabled = false;
|
111 |
}
|
112 |
}
|
113 |
|
|
|
121 |
res.json({
|
122 |
message: 'Houzou Medical Notification Server',
|
123 |
status: 'running',
|
124 |
+
timestamp: new Date().toISOString(),
|
125 |
+
fileOperationsEnabled: fileOperationsEnabled,
|
126 |
+
deviceCount: deviceTokens.size
|
127 |
});
|
128 |
});
|
129 |
|