-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
83 lines (63 loc) · 2.74 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import json
from typing import Optional
from fastapi import FastAPI, Form, Request, Depends, UploadFile, File, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from sqlalchemy.orm import Session
from starlette.staticfiles import StaticFiles
from app import crud, services, schemas
from app.db import Base, engine, get_db
app = FastAPI()
templates = Jinja2Templates(directory="templates")
# Veritabanı oluşturulması
Base.metadata.create_all(bind=engine)
app.mount("/static", StaticFiles(directory="static"), name="static")
# Ana sayfa
@app.get("/", response_class=HTMLResponse)
async def form_view(request: Request):
return templates.TemplateResponse("form.html", {"request": request})
# API Ekleme
@app.post("/submit")
async def submit_form(name: str = Form(...), url: str = Form(...), method: str = Form(...),
params: Optional[str] = Form(None),
cookie: Optional[str] = Form(None),
db: Session = Depends(get_db)):
api_data = {"name": name, "url": url, "method": method, "params": params, "cookie": cookie}
crud.create_api(db=db, api_data=api_data)
return {"message": "API data saved successfully!"}
# JSON File ile API Ekleme
@app.post("/fileSubmit/")
async def submit_file(file: UploadFile = File(...), db: Session = Depends(get_db)):
content = await file.read()
try:
json_data = json.loads(content.decode('utf-8'))
except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="Geçersiz json")
for api in json_data.get("apis", []):
api_data = {
"name": api["name"],
"url": api["url"],
"method": api["method"],
"params": api.get("params"),
"cookie": api.get("cookie")
}
print(api_data)
crud.create_api(db=db, api_data=api_data)
return {"message": "File saved successfully!"}
@app.post("/api/add/")
def add_new_api(api: schemas.APICreate, db: Session = Depends(get_db)):
try:
new_api = crud.create_api(db=db, api_data=api.dict())
return new_api
except Exception as e:
raise HTTPException(status_code=400, detail=f"An error occured: {str(e)}")
# API Health Check Listeleme
@app.get("/list")
def list_apis(request: Request, db: Session = Depends(get_db)):
apis = crud.get_apis(db)
return templates.TemplateResponse("index.html", {"request": request, "apis_health": apis})
@app.get("/openList")
def open_list(request: Request, db: Session = Depends(get_db)):
apis = crud.get_apis(db)
health_results = [services.check_health(api.__dict__) for api in apis]
return templates.TemplateResponse("index.html", {"request": request, "apis_health": health_results})