immich-docker/heatmap-app/server.js

107 lines
3.3 KiB
JavaScript

const http = require('http');
const https = require('https');
const fs = require('fs');
const path = require('path');
const PORT = process.env.PORT || 3000;
const IMMICH_INTERNAL_URL = process.env.IMMICH_URL || 'http://immich-server:2283';
const IMMICH_PUBLIC_URL = process.env.IMMICH_PUBLIC_URL || 'https://photos.jeffemmett.com';
const server = http.createServer((req, res) => {
// CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, x-api-key');
if (req.method === 'OPTIONS') {
res.writeHead(204);
res.end();
return;
}
// Config endpoint
if (req.url === '/api/config') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
immichUrl: '' // Empty - we'll proxy through this server
}));
return;
}
// Proxy Immich API requests
if (req.url.startsWith('/api/')) {
const apiKey = req.headers['x-api-key'];
const targetUrl = IMMICH_INTERNAL_URL + req.url;
let body = '';
req.on('data', chunk => { body += chunk; });
req.on('end', () => {
const urlParts = new URL(targetUrl);
const options = {
hostname: urlParts.hostname,
port: urlParts.port || 80,
path: urlParts.pathname + urlParts.search,
method: req.method,
headers: {
'Content-Type': 'application/json',
'x-api-key': apiKey
}
};
const proxyReq = http.request(options, (proxyRes) => {
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res);
});
proxyReq.on('error', (e) => {
console.error('Proxy error:', e.message);
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: e.message }));
});
if (body) {
proxyReq.write(body);
}
proxyReq.end();
});
return;
}
// Serve static files
let filePath = req.url === '/' ? '/index.html' : req.url.split('?')[0];
filePath = path.join(__dirname, filePath);
const extname = path.extname(filePath);
const contentTypes = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.ico': 'image/x-icon'
};
const contentType = contentTypes[extname] || 'application/octet-stream';
fs.readFile(filePath, (err, content) => {
if (err) {
if (err.code === 'ENOENT') {
res.writeHead(404);
res.end('Not found');
} else {
res.writeHead(500);
res.end('Server error');
}
return;
}
res.writeHead(200, { 'Content-Type': contentType });
res.end(content);
});
});
server.listen(PORT, '0.0.0.0', () => {
console.log(`Immich Heatmap server running on port ${PORT}`);
console.log(`Proxying to: ${IMMICH_INTERNAL_URL}`);
});