-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
285 lines (250 loc) · 7.4 KB
/
server.js
File metadata and controls
285 lines (250 loc) · 7.4 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const morgan = require('morgan');
const fs = require('fs');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(helmet());
app.use(cors());
app.use(morgan('combined'));
app.use(express.json());
// Load MacBook data
let macbookData;
try {
const dataPath = path.join(__dirname, 'data', 'macbook_models_2010_2025.json');
const rawData = fs.readFileSync(dataPath, 'utf8');
macbookData = JSON.parse(rawData);
} catch (error) {
console.error('Error loading MacBook data:', error);
process.exit(1);
}
// Helper functions
const filterByStatus = (devices, status) => {
return devices.filter(device => device.support_status === status);
};
const searchDevices = (devices, searchTerm) => {
const term = searchTerm.toLowerCase();
return devices.filter(device =>
device.model_name.toLowerCase().includes(term) ||
device.model_id.toLowerCase().includes(term) ||
device.support_status.toLowerCase().includes(term)
);
};
const sortDevices = (devices, sortBy = 'release_date', order = 'desc') => {
return [...devices].sort((a, b) => {
let aValue = a[sortBy];
let bValue = b[sortBy];
if (sortBy === 'release_date' || sortBy === 'supported_end_date') {
aValue = new Date(aValue);
bValue = new Date(bValue);
}
if (order === 'desc') {
return bValue > aValue ? 1 : -1;
} else {
return aValue > bValue ? 1 : -1;
}
});
};
// Routes
// Health check
app.get('/health', (req, res) => {
res.json({
status: 'healthy',
timestamp: new Date().toISOString(),
version: '1.0.0'
});
});
// Get all devices
app.get('/api/devices', (req, res) => {
try {
const { status, search, sort_by, order, type } = req.query;
let allDevices = [
...macbookData.macbook_models.macbook_air,
...macbookData.macbook_models.macbook_pro
];
// Filter by device type
if (type) {
if (type.toLowerCase() === 'air') {
allDevices = macbookData.macbook_models.macbook_air;
} else if (type.toLowerCase() === 'pro') {
allDevices = macbookData.macbook_models.macbook_pro;
}
}
// Filter by support status
if (status) {
allDevices = filterByStatus(allDevices, status);
}
// Search functionality
if (search) {
allDevices = searchDevices(allDevices, search);
}
// Sort devices
if (sort_by) {
allDevices = sortDevices(allDevices, sort_by, order);
}
res.json({
success: true,
count: allDevices.length,
data: allDevices,
metadata: {
support_status_definitions: macbookData.support_status_definitions,
notes: macbookData.notes
}
});
} catch (error) {
res.status(500).json({
success: false,
error: 'Internal server error',
message: error.message
});
}
});
// Get MacBook Air models only
app.get('/api/devices/macbook-air', (req, res) => {
try {
const { status, search, sort_by, order } = req.query;
let devices = [...macbookData.macbook_models.macbook_air];
if (status) devices = filterByStatus(devices, status);
if (search) devices = searchDevices(devices, search);
if (sort_by) devices = sortDevices(devices, sort_by, order);
res.json({
success: true,
count: devices.length,
data: devices
});
} catch (error) {
res.status(500).json({
success: false,
error: 'Internal server error',
message: error.message
});
}
});
// Get MacBook Pro models only
app.get('/api/devices/macbook-pro', (req, res) => {
try {
const { status, search, sort_by, order } = req.query;
let devices = [...macbookData.macbook_models.macbook_pro];
if (status) devices = filterByStatus(devices, status);
if (search) devices = searchDevices(devices, search);
if (sort_by) devices = sortDevices(devices, sort_by, order);
res.json({
success: true,
count: devices.length,
data: devices
});
} catch (error) {
res.status(500).json({
success: false,
error: 'Internal server error',
message: error.message
});
}
});
// Get device by model ID
app.get('/api/devices/:modelId', (req, res) => {
try {
const { modelId } = req.params;
const allDevices = [
...macbookData.macbook_models.macbook_air,
...macbookData.macbook_models.macbook_pro
];
const device = allDevices.find(d => d.model_id === modelId);
if (!device) {
return res.status(404).json({
success: false,
error: 'Device not found',
message: `No device found with model ID: ${modelId}`
});
}
res.json({
success: true,
data: device
});
} catch (error) {
res.status(500).json({
success: false,
error: 'Internal server error',
message: error.message
});
}
});
// Get support status definitions
app.get('/api/support-status', (req, res) => {
res.json({
success: true,
data: macbookData.support_status_definitions
});
});
// Get API documentation
app.get('/api/docs', (req, res) => {
const documentation = {
title: "MacBook Device API Documentation",
version: "1.0.0",
base_url: `http://localhost:${PORT}`,
endpoints: {
"GET /health": "Health check endpoint",
"GET /api/devices": {
description: "Get all MacBook devices",
query_parameters: {
status: "Filter by support status (supported, vintage, obsolete)",
search: "Search in model name, model ID, or support status",
sort_by: "Sort by field (release_date, model_name, supported_end_date)",
order: "Sort order (asc, desc) - default: desc",
type: "Filter by device type (air, pro)"
}
},
"GET /api/devices/macbook-air": "Get MacBook Air models only",
"GET /api/devices/macbook-pro": "Get MacBook Pro models only",
"GET /api/devices/:modelId": "Get specific device by model ID",
"GET /api/support-status": "Get support status definitions"
},
examples: {
"All supported devices": "/api/devices?status=supported",
"Search for M1 models": "/api/devices?search=M1",
"MacBook Air sorted by name": "/api/devices/macbook-air?sort_by=model_name&order=asc",
"Specific device": "/api/devices/MacBookAir10,1"
}
};
res.json(documentation);
});
// Root endpoint
app.get('/', (req, res) => {
res.json({
message: 'MacBook Device API Service',
version: '1.0.0',
documentation: `/api/docs`,
endpoints: {
devices: '/api/devices',
health: '/health'
}
});
});
// 404 handler
app.use('*', (req, res) => {
res.status(404).json({
success: false,
error: 'Endpoint not found',
message: `The endpoint ${req.originalUrl} does not exist. Visit /api/docs for available endpoints.`
});
});
// Error handler
app.use((error, req, res, next) => {
console.error('Unhandled error:', error);
res.status(500).json({
success: false,
error: 'Internal server error',
message: 'Something went wrong on our end'
});
});
// Start server
app.listen(PORT, () => {
console.log(`🚀 MacBook API Service running on port ${PORT}`);
console.log(`📚 API Documentation: http://localhost:${PORT}/api/docs`);
console.log(`🔍 All devices: http://localhost:${PORT}/api/devices`);
console.log(`❤️ Health check: http://localhost:${PORT}/health`);
});
module.exports = app;