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