Skip to content

Commit bf88751

Browse files
committed
CORS issues
1 parent 9b62960 commit bf88751

2 files changed

Lines changed: 78 additions & 6 deletions

File tree

app.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ async function loadConfig() {
4343
config.preferences = { columnOrder: [], columnLayout: {} };
4444
}
4545

46-
console.log('Loaded config from server');
46+
console.log('Loaded config from server (using API proxy)');
4747
} else {
4848
throw new Error('Could not load config.json');
4949
}
@@ -55,11 +55,11 @@ async function loadConfig() {
5555

5656
// Make API request with authentication
5757
async function makeAPIRequest(endpoint) {
58-
const url = `${config.karakeepUrl}/api${endpoint}`;
58+
// Use local proxy to avoid CORS issues
59+
const url = `/api/karakeep${endpoint}`;
5960
const response = await fetch(url, {
6061
method: 'GET',
6162
headers: {
62-
'Authorization': `Bearer ${config.apiKey}`,
6363
'Content-Type': 'application/json'
6464
}
6565
});

server.py

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
#!/usr/bin/env python3
22
"""
3-
Simple server for KaraKeep Homedash with preference persistence
4-
This is optional - you can use any static file server if you don't need
5-
cross-device preference syncing.
3+
Simple server for KaraKeep Homedash with preference persistence and API proxy
4+
This server also proxies API requests to KaraKeep to avoid CORS issues.
65
"""
76

87
from http.server import HTTPServer, SimpleHTTPRequestHandler
98
import json
109
import os
1110
from urllib.parse import urlparse
11+
import urllib.request
12+
import urllib.error
13+
14+
# Try to import ssl, but don't fail if it's not available
15+
try:
16+
import ssl
17+
SSL_AVAILABLE = True
18+
except ImportError:
19+
SSL_AVAILABLE = False
20+
print("Warning: SSL module not available. HTTPS connections may fail.")
1221

1322
DEFAULT_CONFIG = {
1423
"karakeepUrl": "http://localhost:3000",
@@ -40,9 +49,71 @@ def do_GET(self):
4049
if self.path == '/config.json':
4150
self.path = '/config/config.json'
4251

52+
# Proxy API requests to KaraKeep
53+
elif self.path.startswith('/api/karakeep/'):
54+
self.proxy_to_karakeep('GET')
55+
return
56+
4357
# Serve files normally
4458
return super().do_GET()
4559

60+
def proxy_to_karakeep(self, method):
61+
"""Proxy requests to KaraKeep API"""
62+
try:
63+
# Load config to get KaraKeep URL and API key
64+
config_path = 'config/config.json'
65+
with open(config_path, 'r') as f:
66+
config = json.load(f)
67+
68+
karakeep_url = config.get('karakeepUrl', 'http://localhost:3000')
69+
api_key = config.get('apiKey', '')
70+
71+
# Remove /api/karakeep prefix and construct the actual API path
72+
api_path = self.path.replace('/api/karakeep', '/api', 1)
73+
full_url = f"{karakeep_url}{api_path}"
74+
75+
# Create request with auth header
76+
req = urllib.request.Request(full_url)
77+
req.add_header('Authorization', f'Bearer {api_key}')
78+
req.add_header('Content-Type', 'application/json')
79+
80+
# Create SSL context that handles self-signed certificates
81+
# Note: This accepts any certificate, including self-signed ones
82+
# In production with proper certificates, you may want to remove these lines
83+
ssl_context = None
84+
if SSL_AVAILABLE and full_url.startswith('https://'):
85+
ssl_context = ssl.create_default_context()
86+
ssl_context.check_hostname = False
87+
ssl_context.verify_mode = ssl.CERT_NONE
88+
89+
# Make the request
90+
if ssl_context:
91+
response = urllib.request.urlopen(req, context=ssl_context)
92+
else:
93+
response = urllib.request.urlopen(req)
94+
data = response.read()
95+
96+
# Send response back to client
97+
self.send_response(response.getcode())
98+
self.send_header('Content-Type', 'application/json')
99+
self.end_headers()
100+
self.wfile.write(data)
101+
102+
except urllib.error.HTTPError as e:
103+
# Forward HTTP errors
104+
self.send_response(e.code)
105+
self.send_header('Content-Type', 'application/json')
106+
self.end_headers()
107+
error_data = e.read()
108+
self.wfile.write(error_data if error_data else json.dumps({"error": str(e)}).encode())
109+
110+
except Exception as e:
111+
# Handle other errors
112+
self.send_response(500)
113+
self.send_header('Content-Type', 'application/json')
114+
self.end_headers()
115+
self.wfile.write(json.dumps({"error": str(e)}).encode())
116+
46117
def do_POST(self):
47118
"""Handle POST requests for saving preferences"""
48119
if self.path == '/api/preferences':
@@ -105,6 +176,7 @@ def do_OPTIONS(self):
105176
httpd = HTTPServer(server_address, KaraKeepHandler)
106177

107178
print(f"KaraKeep HomeDash Server running on http://localhost:{port}")
179+
print("API requests will be proxied to avoid CORS issues")
108180
print("Press Ctrl+C to stop")
109181

110182
try:

0 commit comments

Comments
 (0)