gitscope
https://dev.kronoz.win/r/marzneshin.git
app/routes/service.py · v0.3.2
Source
1 a47eb4c Dawsh 2y ago import sqlalchemy
2 9ddfbc8 Mardin 2y ago from fastapi import APIRouter, 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 db68a58 Dawsh 2y ago from app.db.models import Service, User
10 9ddfbc8 Mardin 2y ago from app.dependencies import DBDep, AdminDep, SudoAdminDep
11 db68a58 Dawsh 2y ago from app.models.proxy import Inbound
12 01cdf2e Dawsh 2y ago from app.models.service import ServiceCreate, ServiceModify, ServiceResponse
13 db68a58 Dawsh 2y ago from app.models.user import UserResponse
14 a47eb4c Dawsh 2y ago
15 9ddfbc8 Mardin 2y ago router = APIRouter(prefix="/services", tags=["Service"])
16 a47eb4c Dawsh 2y ago
17 f7af2b3 Dawsh 2y ago
18 d4ec019 Dawsh 2y ago @router.get("", response_model=Page[ServiceResponse])
19 9ddfbc8 Mardin 2y ago def get_services(db: DBDep, admin: AdminDep, 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 9ddfbc8 Mardin 2y ago def add_service(new_service: ServiceCreate, db: DBDep, admin: SudoAdminDep):
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 9ddfbc8 Mardin 2y ago def get_service(id: int, db: DBDep, admin: AdminDep):
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 db68a58 Dawsh 2y ago @router.get("/{id}/users", response_model=Page[UserResponse])
56 9ddfbc8 Mardin 2y ago def get_service_users(id: int, db: DBDep, admin: SudoAdminDep):
57 db68a58 Dawsh 2y ago """
58 db68a58 Dawsh 2y ago Get service users
59 db68a58 Dawsh 2y ago """
60 db68a58 Dawsh 2y ago service = crud.get_service(db, id)
61 db68a58 Dawsh 2y ago
62 db68a58 Dawsh 2y ago if not service:
63 db68a58 Dawsh 2y ago raise HTTPException(status_code=404, detail="Service not found")
64 db68a58 Dawsh 2y ago
65 db68a58 Dawsh 2y ago query = db.query(User).join(User.services).where(Service.id == service.id)
66 db68a58 Dawsh 2y ago
67 db68a58 Dawsh 2y ago return paginate(query)
68 db68a58 Dawsh 2y ago
69 db68a58 Dawsh 2y ago
70 db68a58 Dawsh 2y ago @router.get("/{id}/inbounds", response_model=Page[Inbound])
71 9ddfbc8 Mardin 2y ago def get_service_inbounds(id: int, db: DBDep, admin: SudoAdminDep):
72 db68a58 Dawsh 2y ago """
73 db68a58 Dawsh 2y ago Get service inbounds
74 db68a58 Dawsh 2y ago """
75 db68a58 Dawsh 2y ago service = crud.get_service(db, id)
76 db68a58 Dawsh 2y ago
77 db68a58 Dawsh 2y ago if not service:
78 db68a58 Dawsh 2y ago raise HTTPException(status_code=404, detail="Service not found")
79 db68a58 Dawsh 2y ago
80 f1ab16c Dawsh 2y ago query = (
81 f1ab16c Dawsh 2y ago db.query(Inbound)
82 f1ab16c Dawsh 2y ago .join(Inbound.services)
83 f1ab16c Dawsh 2y ago .where(Service.id == service.id)
84 f1ab16c Dawsh 2y ago )
85 db68a58 Dawsh 2y ago
86 db68a58 Dawsh 2y ago return paginate(query)
87 db68a58 Dawsh 2y ago
88 db68a58 Dawsh 2y ago
89 2de9d72 Dawsh 2y ago @router.put("/{id}", response_model=ServiceResponse)
90 9ddfbc8 Mardin 2y ago async def modify_service(
91 9ddfbc8 Mardin 2y ago id: int, modification: ServiceModify, db: DBDep, admin: SudoAdminDep
92 9ddfbc8 Mardin 2y ago ):
93 a47eb4c Dawsh 2y ago """
94 a47eb4c Dawsh 2y ago Modify Service
95 a47eb4c Dawsh 2y ago
96 a47eb4c Dawsh 2y ago - **name** can be up to 64 characters
97 ee1351a Dawsh 2y ago - **inbounds** list of inbound ids. if not specified no change will be applied;
98 ee1351a Dawsh 2y ago in case of an empty list all inbounds would be removed.
99 a47eb4c Dawsh 2y ago """
100 a47eb4c Dawsh 2y ago dbservice = crud.get_service(db, id)
101 c13a1aa Dawsh 2y ago if not dbservice:
102 01cdf2e Dawsh 2y ago raise HTTPException(status_code=404, detail="Service not found")
103 d6e2aad Dawsh 2y ago old_inbounds = {(i.node_id, i.protocol, i.tag) for i in dbservice.inbounds}
104 a47eb4c Dawsh 2y ago try:
105 d6e2aad Dawsh 2y ago response = crud.update_service(db, dbservice, modification)
106 a47eb4c Dawsh 2y ago except sqlalchemy.exc.IntegrityError:
107 a47eb4c Dawsh 2y ago db.rollback()
108 f1ab16c Dawsh 2y ago raise HTTPException(
109 f1ab16c Dawsh 2y ago status_code=409, detail="problem updating the service"
110 f1ab16c Dawsh 2y ago )
111 d6e2aad Dawsh 2y ago else:
112 d6e2aad Dawsh 2y ago for user in response.users:
113 d377c4c Dawsh 2y ago if user.activated:
114 d377c4c Dawsh 2y ago marznode.operations.update_user(
115 d377c4c Dawsh 2y ago user, old_inbounds=old_inbounds
116 d377c4c Dawsh 2y ago )
117 d6e2aad Dawsh 2y ago return response
118 a47eb4c Dawsh 2y ago
119 a47eb4c Dawsh 2y ago
120 2de9d72 Dawsh 2y ago @router.delete("/{id}")
121 9ddfbc8 Mardin 2y ago def remove_service(id: int, db: DBDep, admin: SudoAdminDep):
122 a47eb4c Dawsh 2y ago dbservice = crud.get_service(db, id)
123 c13a1aa Dawsh 2y ago if not dbservice:
124 a47eb4c Dawsh 2y ago raise HTTPException(status_code=404, detail="Service not found")
125 01cdf2e Dawsh 2y ago
126 ee1351a Dawsh 2y ago crud.remove_service(db, dbservice)
127 ee1351a Dawsh 2y ago return dict()