diff --git a/database.py b/database.py index 1c9efbf..8dcb549 100644 --- a/database.py +++ b/database.py @@ -1,7 +1,23 @@ from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, declarative_base +import os +from dotenv import load_dotenv -DATABASE_URL = "postgresql://postgres:postgres@172.16.10.178:5444/postgres" + +load_dotenv() + +DB_HOST = os.getenv("DB_HOST") +DB_PORT = os.getenv("DB_PORT") +DB_NAME = os.getenv("DB_NAME") +DB_USER = os.getenv("DB_USER") +DB_PASSWORD = os.getenv("DB_PASSWORD") + +DATABASE_URL = ( + f"postgresql://{DB_USER}:{DB_PASSWORD}" + f"@{DB_HOST}:{DB_PORT}/{DB_NAME}" +) + +# DATABASE_URL = "postgresql://postgres:postgres@172.16.10.178:5444/postgres" engine = create_engine( DATABASE_URL, diff --git a/main.py b/main.py index e0d54df..74fd95a 100644 --- a/main.py +++ b/main.py @@ -8,13 +8,17 @@ from psycopg2.extras import RealDictCursor import asyncio from fastapi.middleware.cors import CORSMiddleware -from database import Base, engine,SessionLocal +from database import Base, engine,SessionLocal,get_db +from uuid import UUID # import models +from sqlalchemy.orm import Session from models import NodeInterface -origins = [ - "http://localhost:5173", - "http://127.0.0.1:5173", -] +import os +from dotenv import load_dotenv + +load_dotenv() + +origins = os.getenv("CORS_ORIGINS", "").split(",") app = FastAPI() @@ -33,9 +37,9 @@ app.add_middleware( class RegisterNodeRequest(BaseModel): node_name: str host: str - port: int = 5444 - dbname: str = "postgres" - user: str = "postgres" + port: int + dbname: str + user: str password: str @@ -44,14 +48,14 @@ class LinkNodeRequest(BaseModel): # The node where sub_create() must run subscriber_host: str - subscriber_port: int = 5444 + subscriber_port: int # The provider node provider_host: str - provider_port: int = 5444 + provider_port: int - dbname: str = "postgres" - user: str = "postgres" + dbname: str + user: str password: str @@ -75,8 +79,9 @@ class LinkNodeRequest(BaseModel): # ============================================================ # JWT TOKEN CREATION # ============================================================ -SECRET_KEY = "my-super-secret-key-change-this" -ALGORITHM = "HS256" +SECRET_KEY = os.getenv("SECRET_KEY") +ALGORITHM = os.getenv("ALGORITHM", "HS256") +# ALGORITHM = "HS256" @app.post("/get-token") @@ -378,6 +383,7 @@ def link_nodes(data: LinkNodeRequest,token_data: dict = Depends(verify_token)): if conn: conn.close() +#!!!!!!!!!!!!THIS IS A TEMPORARY FIXSS!!!!!!!!!!!!! DB_CONFIG = { "host": "172.16.10.123", @@ -494,4 +500,59 @@ def get_spock_subscription_status(token_data: dict = Depends(verify_token),): +@app.get("/spock/node-interfaces") +def get_node_interfaces(db: Session = Depends(get_db),token_data: dict = Depends(verify_token)): + try: + + rows = db.query(NodeInterface).all() + + return { + "status": "success", + "count": len(rows), + "nodes": [ + {"uuid": str(row.uuid), + "if_id": row.if_id, + "if_name": row.if_name, + "if_nodeid": row.if_nodeid, + "host": row.host, + "port": row.port, + "dbname": row.dbname, + "user": row.user + } + for row in rows + ] + } + + except Exception as e: + raise HTTPException( + status_code=500, + detail=str(e) + ) + +@app.delete("/spock/del-node-interfaces") +def delete_node_interface( + uuid: UUID, + db: Session = Depends(get_db), + token_data: dict = Depends(verify_token) + ): + print("API called DEL API") + print(f"uuid received: {uuid}") + node = ( + db.query(NodeInterface) + .filter(NodeInterface.uuid == uuid) + .first() + ) + if not node: + raise HTTPException( + status_code=404, + detail="Node interface not found" + ) + + # Print fetched record + + return { + "status": "success", + "message": "API called DEL API", + "uuid": str(uuid) + } diff --git a/main_experimental.py b/main_experimental.py index 4f03ceb..48a66e4 100644 --- a/main_experimental.py +++ b/main_experimental.py @@ -60,6 +60,14 @@ class LinkNodeRequest(BaseModel): # JWT TOKEN CREATION # ============================================================ +@app.post("/spock/nodesoo") +def get_node_interfaces1(): + print("Fetching node interfaces from the database...") + + return { + "status": "working" + } + @app.post("/get-token") def get_token(): @@ -504,4 +512,39 @@ def get_spock_subscription_status( cursor.close() if conn: - conn.close() \ No newline at end of file + conn.close() + + +# @app.get("/spock/node-interfaces") +# def get_node_interfaces(): +# print("Fetching node interfaces from the database...") + # db = SessionLocal() + + # try: + # interfaces = db.query(NodeInterface).all() + + # return { + # "status": "success", + # "count": len(interfaces), + # "nodes": [ + # { + # "if_id": node.if_id, + # "if_name": node.if_name, + # "if_nodeid": node.if_nodeid, + # "host": node.host, + # "port": node.port, + # "dbname": node.dbname, + # "user": node.user + # } + # for node in interfaces + # ] + # } + + # except Exception as e: + # raise HTTPException( + # status_code=500, + # detail=str(e) + # ) + + # finally: + # db.close() diff --git a/models.py b/models.py index a448872..2bc0a84 100644 --- a/models.py +++ b/models.py @@ -1,17 +1,27 @@ from sqlalchemy import Column, BigInteger, Integer, String from database import Base - +import uuid +from sqlalchemy.dialects.postgresql import UUID class NodeInterface(Base): __tablename__ = "fastAPI_node_interface" + uuid = Column( + UUID(as_uuid=True), + primary_key=True, + default=uuid.uuid4, + nullable=False + ) + if_id = Column( BigInteger, - primary_key=True + unique=True, + nullable=False ) if_name = Column( String, + unique=True, nullable=False ) @@ -22,6 +32,7 @@ class NodeInterface(Base): host = Column( String, + unique=True, nullable=False )