67 lines
2.8 KiB
Python
67 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Servidor HTTP simples para SteelBase
|
|
Execute: python server.py
|
|
Acesse: http://localhost:8000
|
|
"""
|
|
|
|
import http.server
|
|
import socketserver
|
|
import os
|
|
import webbrowser
|
|
import threading
|
|
|
|
PORT = 8000
|
|
|
|
class MyHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
|
|
def end_headers(self):
|
|
# Adicionar headers CORS
|
|
self.send_header('Access-Control-Allow-Origin', '*')
|
|
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
|
|
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
|
|
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate')
|
|
super().end_headers()
|
|
|
|
def do_GET(self):
|
|
# Se acessar a raiz, redirecionar para index.html
|
|
if self.path == '/':
|
|
self.path = '/index.html'
|
|
return super().do_GET()
|
|
|
|
def open_browser():
|
|
"""Abre o navegador automaticamente após 1 segundo"""
|
|
import time
|
|
time.sleep(1)
|
|
webbrowser.open(f'http://localhost:{PORT}/index.html')
|
|
|
|
if __name__ == '__main__':
|
|
os.chdir(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
with socketserver.TCPServer(("", PORT), MyHTTPRequestHandler) as httpd:
|
|
print(f"""
|
|
╔════════════════════════════════════════════════════════════╗
|
|
║ SteelBase - Servidor ║
|
|
╠════════════════════════════════════════════════════════════╣
|
|
║ ║
|
|
║ ✅ Servidor iniciado com sucesso! ║
|
|
║ ║
|
|
║ 🌐 Acesse: http://localhost:{PORT}/index.html ║
|
|
║ ║
|
|
║ 📁 Servindo: {os.getcwd()[:45]}... ║
|
|
║ ║
|
|
║ 🚀 Abrindo navegador automaticamente... ║
|
|
║ ║
|
|
║ ⚠️ Pressione Ctrl+C para parar o servidor ║
|
|
║ ║
|
|
╚════════════════════════════════════════════════════════════╝
|
|
""")
|
|
|
|
# Abrir navegador em thread separada
|
|
threading.Thread(target=open_browser, daemon=True).start()
|
|
|
|
try:
|
|
httpd.serve_forever()
|
|
except KeyboardInterrupt:
|
|
print("\n\n🛑 Servidor parado pelo usuário")
|
|
print("✅ Até logo!")
|