171 lines
6.4 KiB
Python
171 lines
6.4 KiB
Python
"""
|
|
|
|
AUTHOR:
|
|
|
|
Khushal P Soonderji
|
|
|
|
DATE:
|
|
|
|
Created: Wednesday, 25th Dec., 2024
|
|
|
|
OBJECTIVE:
|
|
|
|
To register and enlist servers for various projects.
|
|
|
|
REFERENCES:
|
|
|
|
N/A
|
|
|
|
DOWNLOADS:
|
|
|
|
N/A
|
|
|
|
NOTES:
|
|
|
|
N/A
|
|
|
|
"""
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** IMPORT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# To make sibling directories accessible for imports:
|
|
import sys
|
|
sys.path.append(".")
|
|
sys.path.append("..")
|
|
|
|
# For using Quart:
|
|
from quart import Blueprint, current_app
|
|
|
|
# My utils:
|
|
from utils_v2.string import json
|
|
from utils_v2.api.codes import StatusCodes, HttpCodes
|
|
from utils_v2.api.response import ResponseModel
|
|
from utils_v2.api.async_quart import (
|
|
set_api_version,
|
|
read_input,
|
|
log_request_to_mongo,
|
|
should_not_be_under_maintenance,
|
|
only_whitelisted_ips,
|
|
limit_rate,
|
|
validate_input,
|
|
handle_cancelled_request
|
|
)
|
|
|
|
# Models:
|
|
from utils_v2.api.log import APILogModel
|
|
from models.servers.api import RegisterServerRequestData
|
|
from models.servers.core import CoreServerInfoModel
|
|
|
|
# For asynchronous activities:
|
|
import asyncio
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MACROS / ONE-TIME INIT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# Related to Quart:
|
|
servers_bp = Blueprint("int_servers", __name__)
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** VARIABLES ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** FUNCTIONS ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
@servers_bp.record_once
|
|
def init(blueprint_setup_state):
|
|
|
|
# This gets called when the blueprint is registered.
|
|
# Consider this to be a one-time setup for the whole blueprint:
|
|
pass
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
@servers_bp.route("", methods = ["POST", "GET"])
|
|
@set_api_version(api_version = "2.1.0")
|
|
@read_input(sanitize_headers = False, sanitize_data = False)
|
|
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
|
|
@only_whitelisted_ips(attr_name = "whitelisted_ips")
|
|
@validate_input(data_validator = lambda x: RegisterServerRequestData(**x))
|
|
@handle_cancelled_request()
|
|
async def get_logs_by_filter(
|
|
inbound_headers: dict = None,
|
|
inbound_data: dict | RegisterServerRequestData = None,
|
|
inbound_files: dict = None,
|
|
**kwargs
|
|
):
|
|
|
|
"""
|
|
To register a server's details.
|
|
:param inbound_headers: auto-extracted by the decorators.
|
|
:param inbound_data: auto-extracted by the decorators.
|
|
:param inbound_files: auto-extracted by the decorators.
|
|
:param kwargs: Any number of extra inputs supplied by the decorators.
|
|
"""
|
|
|
|
batch = await current_app.server_controller.get_batch(mongo_data_conn = current_app.mongo)
|
|
logs = await current_app.server_controller.check_server_batch(
|
|
mongo_data_conn = current_app.mongo,
|
|
servers = batch
|
|
)
|
|
for log in logs: print("ONLINE:", log.online)
|
|
|
|
# for b in batch:
|
|
# print("SERVER:", json.to_string(b.model_dump(), default=str))
|
|
# server_log = await current_app.server_controller.check_one_server(
|
|
# mongo_data_conn=current_app.mongo,
|
|
# server = b
|
|
# )
|
|
# print("ONLINE:", server_log.online)
|
|
return "ok"
|
|
|
|
# # register the server to the database:
|
|
# registered_server = await current_app.server_controller.register(
|
|
# mongo_data_conn = current_app.mongo,
|
|
# server = CoreServerInfoModel(**inbound_data.model_dump())
|
|
# )
|
|
#
|
|
# # Done here:
|
|
# success = True if registered_server else False
|
|
# return ResponseModel(
|
|
# status_code = StatusCodes.OK if success else StatusCodes.FAILED,
|
|
# http_code = HttpCodes.SUCCESS if success else HttpCodes.INTERNAL_SERVER_ERROR,
|
|
# message = "Server registered successfully" if success else "Failed to register server."
|
|
# )
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MAIN PROGRAM ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|