Skip to content

Commit

Permalink
Refactor(api)!: Added router to APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
erfjab committed Aug 17, 2024
1 parent 150f6b4 commit cf6d8ac
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 74 deletions.
8 changes: 5 additions & 3 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from fastapi_responses import custom_openapi

from config import DOCS, XRAY_SUBSCRIPTION_PATH
from app import dashboard, telegram, routers, jobs # noqa
from app.routers import api_router

__version__ = "0.6.0"

Expand All @@ -21,9 +23,11 @@
docs_url='/docs' if DOCS else None,
redoc_url='/redoc' if DOCS else None
)

app.openapi = custom_openapi(app)
scheduler = BackgroundScheduler({'apscheduler.job_defaults.max_instances': 20}, timezone='UTC')
logger = logging.getLogger('uvicorn.error')

app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
Expand All @@ -32,9 +36,7 @@
allow_headers=["*"],
)


from app import dashboard, telegram, routers, jobs # noqa

app.include_router(api_router)

def use_route_names_as_operation_ids(app: FastAPI) -> None:
for route in app.routes:
Expand Down
37 changes: 29 additions & 8 deletions app/routers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
from .admin import *
from .subscription import *
from .system import *
from .core import *
from .user import *
from .user_template import *
from .node import *
from .home import *
from fastapi import APIRouter
from . import (
admin,
core,
home,
node,
subscription,
system,
user_template,
user
)

api_router = APIRouter()

routers = [
admin.router,
core.router,
home.router,
node.router,
subscription.router,
system.router,
user_template.router,
user.router,
]

for router in routers:
api_router.include_router(router)

__all__ = ["api_router"]
16 changes: 8 additions & 8 deletions app/routers/admin.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from typing import List, Optional

import sqlalchemy
from app import app
from app.db import Session, crud, get_db
from app.models.admin import Admin, AdminCreate, AdminInDB, AdminModify, Token
from app.utils.jwt import create_admin_token
from config import SUDOERS
from fastapi import Depends, HTTPException, status, Request
from fastapi import Depends, HTTPException, status, Request, APIRouter
from fastapi.security import OAuth2PasswordRequestForm
from app.utils import report

router = APIRouter(tags=['Admin'], prefix='/api')

def authenticate_env_sudo(username: str, password: str) -> bool:
try:
Expand All @@ -35,7 +35,7 @@ def get_client_ip(request: Request) -> str:
return "Unknown"


@app.post("/api/admin/token", tags=['Admin'], response_model=Token)
@router.post("/admin/token", response_model=Token)
def admin_token(
request: Request,
form_data: OAuth2PasswordRequestForm = Depends(),
Expand All @@ -59,7 +59,7 @@ def admin_token(
)


@app.post("/api/admin", tags=['Admin'], response_model=Admin)
@router.post("/admin", response_model=Admin)
def create_admin(new_admin: AdminCreate,
db: Session = Depends(get_db),
admin: Admin = Depends(Admin.get_current)):
Expand All @@ -76,7 +76,7 @@ def create_admin(new_admin: AdminCreate,
return dbadmin


@app.put("/api/admin/{username}", tags=['Admin'], response_model=Admin)
@router.put("/admin/{username}", response_model=Admin)
def modify_admin(username: str,
modified_admin: AdminModify,
db: Session = Depends(get_db),
Expand All @@ -103,7 +103,7 @@ def modify_admin(username: str,
return dbadmin


@app.delete("/api/admin/{username}", tags=['Admin'])
@router.delete("/admin/{username}")
def remove_admin(username: str,
db: Session = Depends(get_db),
admin: Admin = Depends(Admin.get_current)):
Expand All @@ -124,12 +124,12 @@ def remove_admin(username: str,
return {}


@app.get("/api/admin", tags=["Admin"], response_model=Admin)
@router.get("/admin", response_model=Admin)
def get_current_admin(admin: Admin = Depends(Admin.get_current)):
return admin


@app.get("/api/admins", tags=['Admin'], response_model=List[Admin])
@router.get("/admins", response_model=List[Admin])
def get_admins(offset: int = None,
limit: int = None,
username: str = None,
Expand Down
17 changes: 9 additions & 8 deletions app/routers/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
import time

import commentjson
from fastapi import Depends, HTTPException, WebSocket
from fastapi import Depends, HTTPException, WebSocket, APIRouter
from starlette.websockets import WebSocketDisconnect

from app import app, xray
from app import xray
from app.db import Session, get_db
from app.models.admin import Admin
from app.models.core import CoreStats
from app.xray import XRayConfig
from config import XRAY_JSON

router = APIRouter(tags=['Core'], prefix='/api')

@app.websocket("/api/core/logs")
@router.websocket("/core/logs")
async def core_logs(websocket: WebSocket, db: Session = Depends(get_db)):
token = (
websocket.query_params.get('token')
Expand Down Expand Up @@ -72,16 +73,16 @@ async def core_logs(websocket: WebSocket, db: Session = Depends(get_db)):
break


@app.get("/api/core", tags=["Core"], response_model=CoreStats)
@router.get("/core", response_model=CoreStats)
def get_core_stats(admin: Admin = Depends(Admin.get_current)):
return CoreStats(
version=xray.core.version,
started=xray.core.started,
logs_websocket=app.url_path_for('core_logs')
logs_websocket=router.url_path_for('core_logs')
)


@app.post("/api/core/restart", tags=["Core"])
@router.post("/core/restart")
def restart_core(admin: Admin = Depends(Admin.get_current)):
if not admin.is_sudo:
raise HTTPException(status_code=403, detail="You're not allowed")
Expand All @@ -94,7 +95,7 @@ def restart_core(admin: Admin = Depends(Admin.get_current)):
return {}


@app.get("/api/core/config", tags=["Core"])
@router.get("/core/config")
def get_core_config(admin: Admin = Depends(Admin.get_current)) -> dict:
if not admin.is_sudo:
raise HTTPException(status_code=403, detail="You're not allowed")
Expand All @@ -105,7 +106,7 @@ def get_core_config(admin: Admin = Depends(Admin.get_current)) -> dict:
return config


@app.put("/api/core/config", tags=["Core"])
@router.put("/core/config")
def modify_core_config(payload: dict, admin: Admin = Depends(Admin.get_current)) -> dict:
if not admin.is_sudo:
raise HTTPException(status_code=403, detail="You're not allowed")
Expand Down
5 changes: 3 additions & 2 deletions app/routers/home.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from fastapi.responses import HTMLResponse

from app import app
from fastapi import APIRouter
from app.templates import render_template
from config import HOME_PAGE_TEMPLATE

router = APIRouter()

@app.get("/", response_class=HTMLResponse)
@router.get("/", response_class=HTMLResponse)
def base():
return render_template(HOME_PAGE_TEMPLATE)
24 changes: 13 additions & 11 deletions app/routers/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
from typing import List

import sqlalchemy
from fastapi import BackgroundTasks, Depends, HTTPException, WebSocket
from fastapi import BackgroundTasks, Depends, HTTPException, WebSocket, APIRouter
from starlette.websockets import WebSocketDisconnect

from app import app, logger, xray
from app import logger, xray
from app.db import Session, crud, get_db
from app.models.admin import Admin
from app.models.node import (NodeCreate, NodeModify, NodeResponse,
NodeSettings, NodeStatus, NodesUsageResponse)
from app.models.proxy import ProxyHost

router = APIRouter(tags=['Node'], prefix='/api')

@app.get("/api/node/settings", tags=['Node'], response_model=NodeSettings)

@router.get("/node/settings", response_model=NodeSettings)
def get_node_settings(db: Session = Depends(get_db),
admin: Admin = Depends(Admin.get_current)):
if not admin.is_sudo:
Expand All @@ -28,7 +30,7 @@ def get_node_settings(db: Session = Depends(get_db),
)


@app.post("/api/node", tags=['Node'], response_model=NodeResponse)
@router.post("/node", response_model=NodeResponse)
def add_node(new_node: NodeCreate,
bg: BackgroundTasks,
db: Session = Depends(get_db),
Expand Down Expand Up @@ -62,7 +64,7 @@ def add_node(new_node: NodeCreate,
return dbnode


@app.get("/api/node/{node_id}", tags=['Node'], response_model=NodeResponse)
@router.get("/node/{node_id}", response_model=NodeResponse)
def get_node(node_id: int,
db: Session = Depends(get_db),
admin: Admin = Depends(Admin.get_current)):
Expand All @@ -76,7 +78,7 @@ def get_node(node_id: int,
return dbnode


@app.websocket("/api/node/{node_id}/logs")
@router.websocket("/node/{node_id}/logs")
async def node_logs(node_id: int, websocket: WebSocket, db: Session = Depends(get_db)):
token = (
websocket.query_params.get('token')
Expand Down Expand Up @@ -144,15 +146,15 @@ async def node_logs(node_id: int, websocket: WebSocket, db: Session = Depends(ge
break


@app.get("/api/nodes", tags=['Node'], response_model=List[NodeResponse])
@router.get("/nodes", response_model=List[NodeResponse])
def get_nodes(db: Session = Depends(get_db),
admin: Admin = Depends(Admin.get_current)):
if not admin.is_sudo:
raise HTTPException(status_code=403, detail="You're not allowed")
return crud.get_nodes(db)


@app.put("/api/node/{node_id}", tags=['Node'], response_model=NodeResponse)
@router.put("/node/{node_id}", response_model=NodeResponse)
def modify_node(node_id: int,
modified_node: NodeModify,
bg: BackgroundTasks,
Expand All @@ -179,7 +181,7 @@ def modify_node(node_id: int,
return dbnode


@app.post("/api/node/{node_id}/reconnect", tags=['Node'])
@router.post("/node/{node_id}/reconnect")
def reconnect_node(node_id: int,
bg: BackgroundTasks,
db: Session = Depends(get_db),
Expand All @@ -198,7 +200,7 @@ def reconnect_node(node_id: int,
return {}


@app.delete("/api/node/{node_id}", tags=['Node'])
@router.delete("/node/{node_id}")
def remove_node(node_id: int,
db: Session = Depends(get_db),
admin: Admin = Depends(Admin.get_current)):
Expand All @@ -217,7 +219,7 @@ def remove_node(node_id: int,
return {}


@app.get("/api/nodes/usage", tags=['Node'], response_model=NodesUsageResponse)
@router.get("/nodes/usage", response_model=NodesUsageResponse)
def get_usage(db: Session = Depends(get_db),
start: str = None,
end: str = None,
Expand Down
14 changes: 7 additions & 7 deletions app/routers/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
from datetime import datetime
from distutils.version import LooseVersion

from fastapi import Depends, Header, HTTPException, Path, Request, Response
from fastapi import Depends, Header, HTTPException, Path, Request, Response, APIRouter
from fastapi.responses import HTMLResponse

from app import app
from app.db import Session, crud, get_db
from app.models.user import SubscriptionUserResponse, UserResponse
from app.subscription.share import encode_title, generate_subscription
Expand All @@ -23,9 +22,10 @@
XRAY_SUBSCRIPTION_PATH
)

router = APIRouter(tags=['Subscription'], prefix=f'/{XRAY_SUBSCRIPTION_PATH}')

@app.get("/%s/{token}/" % XRAY_SUBSCRIPTION_PATH, tags=['Subscription'])
@app.get("/%s/{token}" % XRAY_SUBSCRIPTION_PATH, include_in_schema=False)
@router.get("/{token}/")
@router.get("/{token}", include_in_schema=False)
def user_subscription(token: str,
request: Request,
db: Session = Depends(get_db),
Expand Down Expand Up @@ -130,7 +130,7 @@ def get_subscription_user_info(user: UserResponse) -> dict:
return Response(content=conf, media_type="text/plain", headers=response_headers)


@app.get("/%s/{token}/info" % XRAY_SUBSCRIPTION_PATH, tags=['Subscription'], response_model=SubscriptionUserResponse)
@router.get("/{token}/info", response_model=SubscriptionUserResponse)
def user_subscription_info(token: str,
db: Session = Depends(get_db)):
sub = get_subscription_payload(token)
Expand All @@ -147,7 +147,7 @@ def user_subscription_info(token: str,
return dbuser


@app.get("/%s/{token}/usage" % XRAY_SUBSCRIPTION_PATH, tags=['Subscription'])
@router.get("/{token}/usage")
def user_get_usage(token: str,
start: str = None,
end: str = None,
Expand Down Expand Up @@ -179,7 +179,7 @@ def user_get_usage(token: str,
return {"usages": usages, "username": dbuser.username}


@app.get("/%s/{token}/{client_type}" % XRAY_SUBSCRIPTION_PATH, tags=['Subscription'])
@router.get("/{token}/{client_type}")
def user_subscription_with_client_type(
token: str,
request: Request,
Expand Down
Loading

0 comments on commit cf6d8ac

Please sign in to comment.