feat: implement data persistence with server-side storage and Docker support

This commit is contained in:
Greg 2025-05-27 01:10:05 +02:00
parent cd46d581b5
commit 94040bf553
4 changed files with 71 additions and 56 deletions

View File

@ -19,6 +19,37 @@ const port = process.env.PORT || 3000;
const DATA_DIR = process.env.DATA_DIR || '/data'; const DATA_DIR = process.env.DATA_DIR || '/data';
const DATA_FILE = path.join(DATA_DIR, 'weight-tracker-data.json'); const DATA_FILE = path.join(DATA_DIR, 'weight-tracker-data.json');
// Print data file path for debugging
console.log(`[STARTUP] Data directory: ${DATA_DIR}`);
console.log(`[STARTUP] Data file path: ${DATA_FILE}`);
// List files in data directory if it exists
if (fs.existsSync(DATA_DIR)) {
try {
const files = fs.readdirSync(DATA_DIR);
console.log(`[STARTUP] Files in ${DATA_DIR}:`, files);
// If data file exists, log its size and content preview
if (fs.existsSync(DATA_FILE)) {
const stats = fs.statSync(DATA_FILE);
console.log(`[STARTUP] Data file exists: ${DATA_FILE}, size: ${stats.size} bytes`);
if (stats.size > 0) {
const preview = fs.readFileSync(DATA_FILE, 'utf8').substring(0, 200);
console.log(`[STARTUP] Data file content preview: ${preview}...`);
} else {
console.log(`[STARTUP] Data file is empty`);
}
} else {
console.log(`[STARTUP] Data file does not exist: ${DATA_FILE}`);
}
} catch (error) {
console.error(`[STARTUP] Error reading data directory: ${error.message}`);
}
} else {
console.log(`[STARTUP] Data directory does not exist: ${DATA_DIR}`);
}
// Middleware // Middleware
app.use(cors()); app.use(cors());
app.use(bodyParser.json({ limit: '5mb' })); app.use(bodyParser.json({ limit: '5mb' }));

View File

@ -14,6 +14,8 @@ services:
networks: networks:
- weight-tracker-network - weight-tracker-network
environment: environment:
# Data file location - make sure this is correct
- DATA_DIR=/data
# Authentication Configuration # Authentication Configuration
- PASSWORD_HASH=${PASSWORD_HASH:-$2a$10$EgxHKjDDFcZKtQY9hl/N4.QvEQHCXVnQXw9dzFYlUDVKOcLMGp9eq} - PASSWORD_HASH=${PASSWORD_HASH:-$2a$10$EgxHKjDDFcZKtQY9hl/N4.QvEQHCXVnQXw9dzFYlUDVKOcLMGp9eq}
- AUTH_USERNAME=${AUTH_USERNAME:-user} - AUTH_USERNAME=${AUTH_USERNAME:-user}

View File

@ -20,7 +20,8 @@ const DataManager = (() => {
return true; return true;
}; };
// Storage file path for Docker environment - this needs to match the API endpoint in data-api.js // API endpoint path for data operations - this must match the API endpoint in data-api.js
// Note: This is NOT a file system path, but an API endpoint URL path
const serverDataPath = '/data/weight-tracker-data.json'; const serverDataPath = '/data/weight-tracker-data.json';
/** /**

View File

@ -1,3 +1,4 @@
# Main server block
server { server {
listen 80; listen 80;
server_name localhost; server_name localhost;
@ -8,46 +9,29 @@ server {
gzip on; gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# Protect all routes with basic authentication # Global CORS configuration
location / { add_header 'Access-Control-Allow-Origin' '*' always;
# Include authentication configuration add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
include /etc/nginx/auth.conf; add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization' always;
# Serve static files # Global OPTIONS handler
try_files $uri $uri/ =404; if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
# CORS headers add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always; add_header 'Access-Control-Max-Age' 1728000;
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization' always; add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
# Handle preflight requests return 204;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
} }
# Special location for index.html to avoid redirection loops # DATA API ENDPOINTS - NO AUTHENTICATION
location = /index.html { # This location must be defined BEFORE the root location to take precedence
# Include authentication configuration location ^~ /data/ {
include /etc/nginx/auth.conf; # Explicitly disable authentication for data API
auth_basic off;
# CORS headers # API Proxy configuration
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization' always;
}
# Proxy requests to the data API - no auth required for API endpoints
location /data/ {
# No authentication for data API to allow the app to save/load data
auth_basic off; # Explicitly disable auth for data API
proxy_pass http://localhost:3000/data/; proxy_pass http://localhost:3000/data/;
proxy_http_version 1.1; proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade; proxy_set_header Upgrade $http_upgrade;
@ -56,26 +40,23 @@ server {
proxy_cache_bypass $http_upgrade; proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# CORS headers for data API # AUTHENTICATED APPLICATION ROUTES
add_header 'Access-Control-Allow-Origin' '*' always; # This covers all routes except those specifically excluded above
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always; location / {
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization' always; # Apply authentication
include /etc/nginx/auth.conf;
# Handle preflight requests for the API # Serve static files
if ($request_method = 'OPTIONS') { try_files $uri $uri/ /index.html;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Authorization';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
} }
# Enable browser caching for static assets # Enable browser caching for static assets
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ { location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
# Include authentication for static assets
include /etc/nginx/auth.conf;
expires 30d; expires 30d;
add_header Cache-Control "public, no-transform"; add_header Cache-Control "public, no-transform";
add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Allow-Origin' '*' always;