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