-
Notifications
You must be signed in to change notification settings - Fork 0
Home
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", )
app.add_middleware( CORSMiddleware, # React dashboard allow_origins=["http://localhost:3000"], allow_credentials=True, allow_methods=[""], allow_headers=[""], )
@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
@app.get("/") async def root(): """Root endpoint - welcome message""" return { "message": "Welcome to Interceptor Gateway", "version": "1.0.0", "docs": "/docs", "health": "/health" }