Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
import express from 'express';
|
3 |
import swaggerUi from 'swagger-ui-express';
|
4 |
import YAML from 'yamljs';
|
@@ -13,20 +12,14 @@ const __dirname = path.dirname(__filename);
|
|
13 |
|
14 |
const app = express();
|
15 |
const port = process.env.PORT || 7860;
|
16 |
-
const CACHE_DIR =
|
17 |
-
const CACHE_DURATION_MS = 60 * 60 * 1000;
|
18 |
|
19 |
const swaggerDocument = YAML.load(path.join(__dirname, 'openapi.yaml'));
|
20 |
|
21 |
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
|
22 |
|
23 |
-
|
24 |
-
try {
|
25 |
-
await fs.mkdir(CACHE_DIR, { recursive: true });
|
26 |
-
} catch (err) {
|
27 |
-
console.error('Error creating cache directory:', err);
|
28 |
-
}
|
29 |
-
}
|
30 |
|
31 |
function generateCacheKey(prefix, identifier, queryParams = {}) {
|
32 |
const identifierPart = String(identifier).replace(/[^a-zA-Z0-9_-]/g, '_');
|
@@ -39,7 +32,8 @@ function generateCacheKey(prefix, identifier, queryParams = {}) {
|
|
39 |
queryPart += `_${key}_null`;
|
40 |
}
|
41 |
});
|
42 |
-
|
|
|
43 |
}
|
44 |
|
45 |
async function readCache(key) {
|
@@ -157,13 +151,9 @@ app.get('/', (req, res) => {
|
|
157 |
res.redirect('/docs');
|
158 |
});
|
159 |
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
});
|
166 |
-
}).catch(err => {
|
167 |
-
console.error("Failed to initialize cache directory. Exiting.", err);
|
168 |
-
process.exit(1);
|
169 |
});
|
|
|
|
|
1 |
import express from 'express';
|
2 |
import swaggerUi from 'swagger-ui-express';
|
3 |
import YAML from 'yamljs';
|
|
|
12 |
|
13 |
const app = express();
|
14 |
const port = process.env.PORT || 7860;
|
15 |
+
const CACHE_DIR = __dirname; // Use current directory for cache files
|
16 |
+
const CACHE_DURATION_MS = 60 * 60 * 1000; // 1 hour
|
17 |
|
18 |
const swaggerDocument = YAML.load(path.join(__dirname, 'openapi.yaml'));
|
19 |
|
20 |
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
|
21 |
|
22 |
+
// No need for ensureCacheDir anymore
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
function generateCacheKey(prefix, identifier, queryParams = {}) {
|
25 |
const identifierPart = String(identifier).replace(/[^a-zA-Z0-9_-]/g, '_');
|
|
|
32 |
queryPart += `_${key}_null`;
|
33 |
}
|
34 |
});
|
35 |
+
// Add prefix to avoid potential collisions with other files
|
36 |
+
return `_cache_${prefix}_${identifierPart}${queryPart}.json`;
|
37 |
}
|
38 |
|
39 |
async function readCache(key) {
|
|
|
151 |
res.redirect('/docs');
|
152 |
});
|
153 |
|
154 |
+
// No need to ensure cache dir here
|
155 |
+
app.listen(port, () => {
|
156 |
+
console.log(`StihiRus API wrapper listening on port ${port}`);
|
157 |
+
console.log(`Cache files will be stored in: ${CACHE_DIR}`);
|
158 |
+
console.log(`API Docs available at /docs`);
|
|
|
|
|
|
|
|
|
159 |
});
|