first
This commit is contained in:
108
server_simple.py
Normal file
108
server_simple.py
Normal file
@@ -0,0 +1,108 @@
|
||||
# server_simple.py - упрощенная версия без Jinja2
|
||||
import re
|
||||
import json
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from fastapi import FastAPI, Form
|
||||
from fastapi.responses import HTMLResponse, JSONResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
# ==================== КОНФИГУРАЦИЯ ====================
|
||||
HOST = "0.0.0.0"
|
||||
PORT = 8000
|
||||
|
||||
BASE_DIR = Path(__file__).parent
|
||||
STATIC_DIR = BASE_DIR / "static"
|
||||
DATA_DIR = BASE_DIR / "data"
|
||||
|
||||
STATIC_DIR.mkdir(exist_ok=True)
|
||||
DATA_DIR.mkdir(exist_ok=True)
|
||||
(STATIC_DIR / "css").mkdir(exist_ok=True)
|
||||
(STATIC_DIR / "js").mkdir(exist_ok=True)
|
||||
|
||||
app = FastAPI()
|
||||
app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
|
||||
|
||||
# Читаем HTML файл
|
||||
HTML_FILE = BASE_DIR / "templates" / "index.html"
|
||||
|
||||
|
||||
def save_registration_to_file(data: dict) -> dict:
|
||||
filepath = DATA_DIR / "registrations.json"
|
||||
registrations = []
|
||||
if filepath.exists():
|
||||
try:
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
registrations = json.load(f)
|
||||
except:
|
||||
registrations = []
|
||||
|
||||
new_record = {
|
||||
"id": len(registrations) + 1,
|
||||
"created_at": datetime.now().isoformat(),
|
||||
**data,
|
||||
"status": "pending"
|
||||
}
|
||||
registrations.append(new_record)
|
||||
with open(filepath, 'w', encoding='utf-8') as f:
|
||||
json.dump(registrations, f, ensure_ascii=False, indent=2)
|
||||
return new_record
|
||||
|
||||
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
async def get_landing_page():
|
||||
if HTML_FILE.exists():
|
||||
with open(HTML_FILE, 'r', encoding='utf-8') as f:
|
||||
return HTMLResponse(content=f.read())
|
||||
else:
|
||||
return HTMLResponse(content="<h1>Файл templates/index.html не найден</h1>")
|
||||
|
||||
|
||||
@app.post("/api/register")
|
||||
async def register(
|
||||
fullname: str = Form(...),
|
||||
email: str = Form(...),
|
||||
phone: str = Form(...),
|
||||
company: str = Form(...),
|
||||
businessSize: str = Form(""),
|
||||
message: str = Form(""),
|
||||
agree: bool = Form(False)
|
||||
):
|
||||
if not agree:
|
||||
return JSONResponse(status_code=400, content={"success": False, "message": "Необходимо согласие"})
|
||||
|
||||
data = {
|
||||
"fullname": fullname.strip(),
|
||||
"email": email.strip().lower(),
|
||||
"phone": phone.strip(),
|
||||
"company": company.strip(),
|
||||
"businessSize": businessSize,
|
||||
"message": message.strip(),
|
||||
}
|
||||
|
||||
try:
|
||||
save_registration_to_file(data)
|
||||
return JSONResponse(content={"success": True, "message": "Заявка отправлена!"})
|
||||
except Exception as e:
|
||||
return JSONResponse(status_code=500, content={"success": False, "message": str(e)})
|
||||
|
||||
|
||||
@app.get("/admin/registrations", response_class=HTMLResponse)
|
||||
async def admin_registrations():
|
||||
filepath = DATA_DIR / "registrations.json"
|
||||
registrations = []
|
||||
if filepath.exists():
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
registrations = json.load(f)
|
||||
|
||||
html = "<h1>Заявки</h1><table border='1'>"
|
||||
for r in registrations:
|
||||
html += f"<tr><td>{r.get('fullname')}</td><td>{r.get('email')}</td></tr>"
|
||||
html += "</table><a href='/'>Назад</a>"
|
||||
return HTMLResponse(content=html)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
uvicorn.run(app, host=HOST, port=PORT)
|
||||
Reference in New Issue
Block a user