-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathindex.js
More file actions
172 lines (150 loc) · 4.31 KB
/
index.js
File metadata and controls
172 lines (150 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const express = require('express');
const fs = require('fs');
const path = require('path');
const lock_api = require('./locks_api')
const swaggerUi = require('swagger-ui-express');
const swaggerJsdoc = require('swagger-jsdoc');
const app = express();
const dynamicCors = (req, res, next) => {
const origin = req.headers.origin
// Allow requests from any origin but with credentials
res.header('Access-Control-Allow-Origin', origin || '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
res.header('Access-Control-Allow-Credentials', 'true');
// Handle preflight requests
if (req.method === 'OPTIONS') {
res.sendStatus(200);
} else {
next();
}
};
const dataDir = process.env.DATA_DIR || path.join(__dirname, 'json_files');
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
}
// Middleware to parse text request bodies
app.use(express.text());
// Swagger configuration
const swaggerOptions = {
definition: {
openapi: '3.0.0',
info: {
title: 'File API',
version: '1.0.0',
description: 'API for saving and deleting files',
},
},
apis: ['index.js','locks_api.js'], // Update the path to reflect the compiled JavaScript file
};
const swaggerSpec = swaggerJsdoc(swaggerOptions);
// cors enable
app.use(dynamicCors);
// Serve Swagger UI
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
// PUT endpoint to save a file
/**
* @swagger
* /data/{filename}:
* put:
* summary: Save data to a file
* tags: [Metadata File]
* parameters:
* - in: path
* name: filename
* schema:
* type: string
* required: true
* description: The name of the file to save
* requestBody:
* required: true
* content:
* text/plain:
* schema:
* type: string
* responses:
* '201':
* description: File saved successfully
*/
app.put('/data/:filename', (req, res) => {
const filename = req.params.filename;
const filePath = path.join(dataDir, filename);
fs.writeFile(filePath, req.body, (err) => {
if (err) {
console.error(err);
return res.status(500).send('Failed to save file');
}
res.status(201).send({'success': true});
});
});
// GET endpoint to retrieve a file
/**
* @swagger
* /data/{filename}:
* get:
* summary: Get a file
* tags: [Metadata File]
* parameters:
* - in: path
* name: filename
* schema:
* type: string
* required: true
* description: The name of the file to retrieve
* responses:
* '200':
* description: File retrieved successfully
* content:
* text/plain:
* schema:
* type: string
*/
app.get('/data/:filename', (req, res) => {
const filename = req.params.filename;
const filePath = path.join(dataDir, filename);
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(err);
return res.status(404).send({'message': 'File not found'});
}
res.status(200).send(data);
});
});
// DELETE endpoint to delete a file
/**
* @swagger
* /data/{filename}:
* delete:
* summary: Delete a file
* tags: [Metadata File]
* parameters:
* - in: path
* name: filename
* schema:
* type: string
* required: true
* description: The name of the file to delete
* responses:
* '200':
* description: File deleted successfully
*/
app.delete('/data/:filename', (req, res) => {
const filename = req.params.filename;
const filePath = path.join(dataDir, filename);
fs.unlink(filePath, (err) => {
if (err) {
console.error(err);
return res.status(500).send({'message':'Failed to delete file'});
}
res.send('File deleted successfully');
});
});
app.get('/', (req, res) => {
res.redirect('/docs');
});
lock_api.setup(app)
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});