2026-04-24 09:31:50 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
"""
|
|
|
|
|
Minimaler Entwicklungs-Server für die Prompt Templates Webansicht.
|
|
|
|
|
Startet auf Port 8080 und dient die statischen Dateien aus.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import http.server
|
|
|
|
|
import socketserver
|
|
|
|
|
import os
|
2026-04-24 10:46:15 +02:00
|
|
|
import socket
|
|
|
|
|
import json
|
2026-04-24 09:31:50 +02:00
|
|
|
|
|
|
|
|
DIRECTORY = os.path.dirname(os.path.abspath(__file__))
|
2026-04-24 10:46:15 +02:00
|
|
|
ROOT_DIR = os.path.abspath(os.path.join(DIRECTORY, '..'))
|
|
|
|
|
|
|
|
|
|
def find_free_port(start_port=9000):
|
|
|
|
|
"""Finde einen freien Port ab start_port"""
|
|
|
|
|
port = start_port
|
|
|
|
|
while port < 10000:
|
|
|
|
|
try:
|
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
|
|
|
s.bind(('', port))
|
|
|
|
|
return port
|
|
|
|
|
except OSError:
|
|
|
|
|
port += 1
|
|
|
|
|
return None
|
2026-04-24 09:31:50 +02:00
|
|
|
|
|
|
|
|
class Handler(http.server.SimpleHTTPRequestHandler):
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super().__init__(*args, directory=DIRECTORY, **kwargs)
|
|
|
|
|
|
|
|
|
|
def do_GET(self):
|
2026-04-24 10:46:15 +02:00
|
|
|
# Für Root-Pfad: index.html servieren
|
2026-04-24 09:31:50 +02:00
|
|
|
if self.path == '/' or self.path == '/index.html':
|
|
|
|
|
self.path = '/index.html'
|
2026-04-24 10:46:15 +02:00
|
|
|
return super().do_GET()
|
|
|
|
|
|
|
|
|
|
# Anfragen für /templates.json oder /templates/* umleiten
|
|
|
|
|
if self.path.startswith('/templates'):
|
|
|
|
|
# Pfad relativ zur ROOT_DIR konstruieren
|
|
|
|
|
rel_path = self.path[1:] # '/templates.json' → 'templates.json'
|
|
|
|
|
file_path = os.path.join(ROOT_DIR, rel_path)
|
|
|
|
|
|
|
|
|
|
if os.path.exists(file_path) and not os.path.isdir(file_path):
|
|
|
|
|
try:
|
|
|
|
|
self.path = file_path
|
|
|
|
|
# Einfach die Datei senden
|
|
|
|
|
with open(file_path, 'rb') as f:
|
|
|
|
|
self.send_response(200)
|
|
|
|
|
self.send_header('Content-type', 'text/plain' if file_path.endswith('.md') else 'application/json')
|
|
|
|
|
self.end_headers()
|
|
|
|
|
self.wfile.write(f.read())
|
|
|
|
|
return
|
|
|
|
|
except Exception as e:
|
|
|
|
|
self.send_error(500, f"Error serving file: {e}")
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
self.send_error(404, "File not found")
|
|
|
|
|
return
|
|
|
|
|
|
2026-04-24 09:31:50 +02:00
|
|
|
return super().do_GET()
|
|
|
|
|
|
|
|
|
|
def main():
|
2026-04-24 10:46:15 +02:00
|
|
|
PORT = 8081
|
|
|
|
|
print(f"Serving on http://localhost:{PORT}")
|
|
|
|
|
|
2026-04-24 09:31:50 +02:00
|
|
|
with socketserver.TCPServer(("", PORT), Handler) as httpd:
|
|
|
|
|
print(f"Serving Prompt Templates on http://localhost:{PORT}")
|
|
|
|
|
print(f"Press Ctrl+C to stop")
|
2026-04-24 10:46:15 +02:00
|
|
|
print(f"Directory: {DIRECTORY}")
|
2026-04-24 09:31:50 +02:00
|
|
|
try:
|
|
|
|
|
httpd.serve_forever()
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
print("\nServer stopped")
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|