#!/usr/bin/env python3 """ Python standard library HTTP server replacing legacy CGI execution. Serves static frontend files and provides JSON API endpoint for scraper paths. """ import http.server import json import os import sys from urllib.parse import parse_qs, urlparse, unquote # Add cgi-bin directory to sys.path so scraper can be imported cleanly sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'cgi-bin')) from scraper import run class PhikipathiaHandler(http.server.SimpleHTTPRequestHandler): def do_GET(self): parsed_url = urlparse(self.path) # Handle API endpoints: /cgi-bin/init.py or /api/find-path if parsed_url.path in ['/cgi-bin/init.py', '/api/find-path']: query_params = parse_qs(parsed_url.query) taal = 'en' start = 'Special:Random' einde = 'Philosophy' if 'data' in query_params: try: data_raw = unquote(query_params['data'][0]) data_json = json.loads(data_raw) taal = data_json.get('taal', taal) start = data_json.get('start', start) einde = data_json.get('einde', einde) except Exception as err: self.send_json({'error': f"Fout bij verwerken van request parameters: {str(err)}"}, status=400) return else: taal = query_params.get('taal', [taal])[0] start = query_params.get('start', [start])[0] einde = query_params.get('einde', [einde])[0] try: antwoord = run(taal, start, einde) self.send_json(antwoord) except Exception as e: self.send_json({'error': f"Interne serverfout: {str(e)}"}, status=500) return # Default: serve static files (index.html, style.css, index.js, images) super().do_GET() def send_json(self, data, status=200): body = json.dumps(data).encode('utf-8') self.send_response(status) self.send_header('Content-Type', 'application/json; charset=utf-8') self.send_header('Content-Length', str(len(body))) self.send_header('Access-Control-Allow-Origin', '*') self.send_header('X-Content-Type-Options', 'nosniff') self.send_header('X-Frame-Options', 'SAMEORIGIN') self.end_headers() self.wfile.write(body) def log_message(self, format, *args): # Clean logging format sys.stderr.write(f"[{self.log_date_time_string()}] {format % args}\n") def main(): port = int(os.environ.get('PORT', 8000)) server_address = ('', port) httpd = http.server.HTTPServer(server_address, PhikipathiaHandler) print(f"Phikipathia server active on http://localhost:{port}") try: httpd.serve_forever() except KeyboardInterrupt: print("\nServer shutting down.") httpd.server_close() if __name__ == '__main__': main()