gitscope
https://dev.kronoz.win/r/marzneshin.git
app/routes/service.py · v0.3.0
Source
1 a47eb4c Dawsh 2y ago import sqlalchemy
2 d4ec019 Dawsh 2y ago from fastapi import APIRouter, Depends, Query
3 c13a1aa Dawsh 2y ago from fastapi import HTTPException
4 d4ec019 Dawsh 2y ago from fastapi_pagination.ext.sqlalchemy import paginate
5 d4ec019 Dawsh 2y ago from fastapi_pagination.links import Page
6 a47eb4c Dawsh 2y ago
7 d6e2aad Dawsh 2y ago from app import marznode
8 c13a1aa Dawsh 2y ago from app.db import crud
9 d4ec019 Dawsh 2y ago from app.db.models import Service
10 f7af2b3 Dawsh 2y ago from app.dependencies import DBDep, sudo_admin
11 01cdf2e Dawsh 2y ago from app.models.service import ServiceCreate, ServiceModify, ServiceResponse
12 a47eb4c Dawsh 2y ago
13 01cdf2e Dawsh 2y ago router = APIRouter(
14 01cdf2e Dawsh 2y ago prefix="/services", dependencies=[Depends(sudo_admin)], tags=["Service"]
15 01cdf2e Dawsh 2y ago )
16 a47eb4c Dawsh 2y ago
17 f7af2b3 Dawsh 2y ago
18 d4ec019 Dawsh 2y ago @router.get("", response_model=Page[ServiceResponse])
19 d4ec019 Dawsh 2y ago def get_services(db: DBDep, name: str = Query(None)):
20 d4ec019 Dawsh 2y ago query = db.query(Service)
21 d4ec019 Dawsh 2y ago if name:
22 d4ec019 Dawsh 2y ago query = query.filter(Service.name.ilike(f"%{name}%"))
23 d4ec019 Dawsh 2y ago return paginate(query)
24 d4ec019 Dawsh 2y ago
25 d4ec019 Dawsh 2y ago
26 2de9d72 Dawsh 2y ago @router.post("", response_model=ServiceResponse)
27 01cdf2e Dawsh 2y ago def add_service(new_service: ServiceCreate, db: DBDep):
28 a47eb4c Dawsh 2y ago """
29 d6e2aad Dawsh 2y ago Add a new service
30 a47eb4c Dawsh 2y ago
31 d6e2aad Dawsh 2y ago - **name** service name
32 d6e2aad Dawsh 2y ago - **inbounds** list of inbound ids
33 a47eb4c Dawsh 2y ago """
34 a47eb4c Dawsh 2y ago try:
35 a47eb4c Dawsh 2y ago return crud.create_service(db, new_service)
36 a47eb4c Dawsh 2y ago except sqlalchemy.exc.IntegrityError:
37 a47eb4c Dawsh 2y ago db.rollback()
38 01cdf2e Dawsh 2y ago raise HTTPException(
39 01cdf2e Dawsh 2y ago status_code=409, detail="Service by this name already exists"
40 01cdf2e Dawsh 2y ago )
41 a47eb4c Dawsh 2y ago
42 a47eb4c Dawsh 2y ago
43 2de9d72 Dawsh 2y ago @router.get("/{id}", response_model=ServiceResponse)
44 f7af2b3 Dawsh 2y ago def get_service(id: int, db: DBDep):
45 a47eb4c Dawsh 2y ago """
46 a47eb4c Dawsh 2y ago Get Service information with id
47 a47eb4c Dawsh 2y ago """
48 a47eb4c Dawsh 2y ago dbservice = crud.get_service(db, id)
49 a47eb4c Dawsh 2y ago if not dbservice:
50 a47eb4c Dawsh 2y ago raise HTTPException(status_code=404, detail="Service not found")
51 a47eb4c Dawsh 2y ago
52 a47eb4c Dawsh 2y ago return dbservice
53 a47eb4c Dawsh 2y ago
54 a47eb4c Dawsh 2y ago
55 2de9d72 Dawsh 2y ago @router.put("/{id}", response_model=ServiceResponse)
56 01cdf2e Dawsh 2y ago async def modify_service(id: int, modification: ServiceModify, db: DBDep):
57 a47eb4c Dawsh 2y ago """
58 a47eb4c Dawsh 2y ago Modify Service
59 a47eb4c Dawsh 2y ago
60 a47eb4c Dawsh 2y ago - **name** can be up to 64 characters
61 ee1351a Dawsh 2y ago - **inbounds** list of inbound ids. if not specified no change will be applied;
62 ee1351a Dawsh 2y ago in case of an empty list all inbounds would be removed.
63 a47eb4c Dawsh 2y ago """
64 a47eb4c Dawsh 2y ago dbservice = crud.get_service(db, id)
65 c13a1aa Dawsh 2y ago if not dbservice:
66 01cdf2e Dawsh 2y ago raise HTTPException(status_code=404, detail="Service not found")
67 d6e2aad Dawsh 2y ago old_inbounds = {(i.node_id, i.protocol, i.tag) for i in dbservice.inbounds}
68 a47eb4c Dawsh 2y ago try:
69 d6e2aad Dawsh 2y ago response = crud.update_service(db, dbservice, modification)
70 a47eb4c Dawsh 2y ago except sqlalchemy.exc.IntegrityError:
71 a47eb4c Dawsh 2y ago db.rollback()
72 d6e2aad Dawsh 2y ago raise HTTPException(status_code=409, detail="problem updating the service")
73 d6e2aad Dawsh 2y ago else:
74 d6e2aad Dawsh 2y ago for user in response.users:
75 d6e2aad Dawsh 2y ago await marznode.operations.update_user(user, old_inbounds=old_inbounds)
76 d6e2aad Dawsh 2y ago return response
77 a47eb4c Dawsh 2y ago
78 a47eb4c Dawsh 2y ago
79 2de9d72 Dawsh 2y ago @router.delete("/{id}")
80 01cdf2e Dawsh 2y ago def remove_service(id: int, db: DBDep):
81 a47eb4c Dawsh 2y ago dbservice = crud.get_service(db, id)
82 c13a1aa Dawsh 2y ago if not dbservice:
83 a47eb4c Dawsh 2y ago raise HTTPException(status_code=404, detail="Service not found")
84 01cdf2e Dawsh 2y ago
85 ee1351a Dawsh 2y ago crud.remove_service(db, dbservice)
86 ee1351a Dawsh 2y ago return dict()