gitscope
https://dev.kronoz.win/r/marzneshin.git
app/routes/inbounds.py · v0.3.0
Source
1 d4ec019 Dawsh 2y ago from fastapi import APIRouter, Depends, Query
2 2de9d72 Dawsh 2y ago from fastapi import HTTPException
3 d4ec019 Dawsh 2y ago from fastapi_pagination.ext.sqlalchemy import paginate
4 d4ec019 Dawsh 2y ago from fastapi_pagination.links import Page
5 2de9d72 Dawsh 2y ago
6 2de9d72 Dawsh 2y ago from app.db import crud
7 d4ec019 Dawsh 2y ago from app.db.models import InboundHost as DBInboundHost, Inbound as DBInbound
8 2de9d72 Dawsh 2y ago from app.dependencies import DBDep, sudo_admin
9 2de9d72 Dawsh 2y ago from app.models.proxy import Inbound, InboundHost, InboundHostResponse
10 2de9d72 Dawsh 2y ago
11 01cdf2e Dawsh 2y ago router = APIRouter(
12 01cdf2e Dawsh 2y ago prefix="/inbounds", dependencies=[Depends(sudo_admin)], tags=["Inbounds"]
13 01cdf2e Dawsh 2y ago )
14 2de9d72 Dawsh 2y ago
15 2de9d72 Dawsh 2y ago
16 d4ec019 Dawsh 2y ago @router.get("", response_model=Page[Inbound])
17 d4ec019 Dawsh 2y ago def get_inbounds(db: DBDep, tag: str = Query(None)):
18 2de9d72 Dawsh 2y ago """
19 2de9d72 Dawsh 2y ago Get all inbounds
20 2de9d72 Dawsh 2y ago """
21 d4ec019 Dawsh 2y ago query = db.query(DBInbound)
22 d4ec019 Dawsh 2y ago if tag:
23 d4ec019 Dawsh 2y ago query = query.filter(DBInbound.tag.ilike(f"%{tag}%"))
24 2de9d72 Dawsh 2y ago
25 d4ec019 Dawsh 2y ago return paginate(db, query)
26 2de9d72 Dawsh 2y ago
27 2de9d72 Dawsh 2y ago
28 7428e12 Mrdn 2y ago @router.get("/hosts", response_model=Page[InboundHostResponse])
29 2de9d72 Dawsh 2y ago def get_hosts(db: DBDep):
30 d4ec019 Dawsh 2y ago return paginate(db.query(DBInboundHost))
31 2de9d72 Dawsh 2y ago
32 2de9d72 Dawsh 2y ago
33 2de9d72 Dawsh 2y ago @router.put("/hosts/{id}", response_model=InboundHostResponse)
34 2de9d72 Dawsh 2y ago def update_host(id: int, host: InboundHost, db: DBDep):
35 2de9d72 Dawsh 2y ago """
36 2de9d72 Dawsh 2y ago Modify a host
37 2de9d72 Dawsh 2y ago """
38 2de9d72 Dawsh 2y ago
39 2de9d72 Dawsh 2y ago db_host = crud.get_host(db, id)
40 2de9d72 Dawsh 2y ago if not db_host:
41 2de9d72 Dawsh 2y ago raise HTTPException(status_code=404, detail="Host not found")
42 2de9d72 Dawsh 2y ago
43 2de9d72 Dawsh 2y ago return crud.update_host(db, db_host, host)
44 2de9d72 Dawsh 2y ago
45 2de9d72 Dawsh 2y ago
46 2de9d72 Dawsh 2y ago @router.delete("/hosts/{id}")
47 2de9d72 Dawsh 2y ago def delete_host(id: int, db: DBDep):
48 2de9d72 Dawsh 2y ago """
49 2de9d72 Dawsh 2y ago Remove a host
50 2de9d72 Dawsh 2y ago """
51 2de9d72 Dawsh 2y ago db_host = crud.get_host(db, id)
52 2de9d72 Dawsh 2y ago if not db_host:
53 2de9d72 Dawsh 2y ago raise HTTPException(status_code=404, detail="Host not found")
54 2de9d72 Dawsh 2y ago
55 2de9d72 Dawsh 2y ago db.delete(db_host)
56 2de9d72 Dawsh 2y ago db.commit()
57 2de9d72 Dawsh 2y ago return {}
58 2de9d72 Dawsh 2y ago
59 2de9d72 Dawsh 2y ago
60 2de9d72 Dawsh 2y ago @router.get("/{id}", response_model=Inbound)
61 2de9d72 Dawsh 2y ago def get_inbound(id: int, db: DBDep):
62 2de9d72 Dawsh 2y ago """
63 2de9d72 Dawsh 2y ago Get a specific inbound
64 2de9d72 Dawsh 2y ago """
65 2de9d72 Dawsh 2y ago inbound = crud.get_inbound(db, id)
66 2de9d72 Dawsh 2y ago if not inbound:
67 2de9d72 Dawsh 2y ago raise HTTPException(status_code=404, detail="Inbound not found")
68 2de9d72 Dawsh 2y ago
69 2de9d72 Dawsh 2y ago return inbound
70 2de9d72 Dawsh 2y ago
71 2de9d72 Dawsh 2y ago
72 d4ec019 Dawsh 2y ago @router.get("/{id}/hosts", response_model=Page[InboundHostResponse])
73 2de9d72 Dawsh 2y ago def get_inbound_hosts(id: int | None, db: DBDep):
74 2de9d72 Dawsh 2y ago """
75 2de9d72 Dawsh 2y ago Get hosts of a specific inbound
76 2de9d72 Dawsh 2y ago """
77 2de9d72 Dawsh 2y ago inbound = crud.get_inbound(db, id)
78 2de9d72 Dawsh 2y ago if not inbound:
79 2de9d72 Dawsh 2y ago raise HTTPException(status_code=404, detail="Inbound not found")
80 2de9d72 Dawsh 2y ago
81 d4ec019 Dawsh 2y ago return paginate(db, db.query(DBInboundHost).filter(DBInboundHost.inbound_id == id))
82 2de9d72 Dawsh 2y ago
83 2de9d72 Dawsh 2y ago
84 2de9d72 Dawsh 2y ago @router.post("/{id}/hosts", response_model=InboundHostResponse)
85 2de9d72 Dawsh 2y ago def create_host(id: int, host: InboundHost, db: DBDep):
86 2de9d72 Dawsh 2y ago """
87 2de9d72 Dawsh 2y ago Add a host to the inbound
88 2de9d72 Dawsh 2y ago """
89 2de9d72 Dawsh 2y ago inbound = crud.get_inbound(db, id)
90 2de9d72 Dawsh 2y ago if not inbound:
91 2de9d72 Dawsh 2y ago raise HTTPException(status_code=404, detail="Inbound not found")
92 2de9d72 Dawsh 2y ago
93 2de9d72 Dawsh 2y ago return crud.add_host(db, inbound, host)