Skip to content
Siddharth Sriram edited this page Jan 4, 2026 · 23 revisions

Interceptor Documentation:

main.py documentation:

Notes:

from fastapi import FastAPI, Request
from fastapi refers specifically to the FastAPI framework
"import FastAPI" indicates that the FastAPI class imported from the fastapi module
"import Request" indicates that the Request class is also imported from the fastapi module

"import CORSMiddleware" indicates that the CORSMiddleware class is imported from the fastapi.middleware.cors module

CORSMiddleware is used to handle Cross-Origin Resource Sharing (CORS) in FastAPI applications (security feature) that controls which websites are allowed to talk to your application

"import logging" library that helps track whats happening in program, records messages about errors, warnings, or just general information "import os" library that lets program interact with OS, can read environment variables, work with file paths, etc

"logging.basicConfig(" Configuring or setting up how logging your system will work (remember Cassandra and setting up logs on there so actions be tracked and basicConfig (basic configuration) means your choosing you setting

"level=logging.INFO" Sets what level of messages you want to record Different levels:

  • DEBUG (very detailed)
  • ERROR (actual problems)
  • INFO (general info)
  • WARNING (potential problems)
  • CRITICAL (serious problem)

logging.INFO "record INFO messages and anything more serious)

Logging message format:

format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'

  • %(asctime)s ; time
  • %(name)s : which part of application or program sent this
  • %(levelname)s : the level (INFO, WARNING, ERROR, etc)
  • %(message)s : Actual message (Server started successfully)

Example: 2024-12-29 10:30:45 - my_app - INFO - Server started successfully

logger = logging.getLogger(name)

  • Creates actual logger object that you'll use to write messages
  • "getLogger(name)" - create a logger with the name of this file/module
  • name is a special Python variable that contains the name of your current file
  • Now you can use logger.info("something happened") to record messages!

app = FastAPI(title="Interceptor", description="API Security Gateway with Behavior Analysis", version="1.0.0", docs_url="/api/docs", )

Add the CORS middleware (allows dashboard to connect)

app.add_middleware( CORSMiddleware, # React dashboard allow_origins=["http://localhost:3000"], allow_credentials=True, allow_methods=[""], allow_headers=[""], )

Starting up and shutting down interceptor tool

@app.on_event("startup") async def startup_event(): """Run when the application starts""" logger.info("=" * 50) logger.info("Interceptor Gateway Starting Up") logger.info("=" * 50) # TODO: Initialize database connections logger.info("Gateway ready to accept requests")

@app.on_event("shutdown") async def shutdown_event(): """Run when the application shuts down""" logger.info("Interceptor Gateway Shutting Down") # TODO: Close database connections

Basic Endpoint

@app.get("/") async def root(): """Root endpoint - welcome message""" return { "message": "Welcome to Interceptor Gateway", "version": "1.0.0", "docs": "/docs", "health": "/health" }

@app.get("/health") async def health_check(): """Health check endpoint""" return { "status": "healthy", "service": "interceptor-gateway", "timestamp": datetime.utcnow().isoformat() }

@app.get("/api/test") async def test_endpoint(request: Request): """Test endpoint to verify gateway is working""" return { "message": "Gateway is working!", "client_ip": request.client.host, "path": request.url.path, "method": request.method, "timestamp": datetime.utcnow().isoformat() }

Create the database configuration

init.py

Empty file to make it a python packet

Now create database.py

Clone this wiki locally