mirror of
https://github.com/blackboxprogramming/BlackRoad-Operating-System.git
synced 2026-03-17 05:57:21 -05:00
Add comprehensive FastAPI backend for BlackRoad OS
This commit adds a complete backend infrastructure with: **Core Infrastructure:** - FastAPI application with async/await support - PostgreSQL database with SQLAlchemy ORM - Redis caching layer - JWT authentication and authorization - Docker and Docker Compose configuration **API Services:** - Authentication API (register, login, JWT tokens) - RoadMail API (email service with folders, send/receive) - BlackRoad Social API (posts, comments, likes, follows) - BlackStream API (video streaming with views/likes) - File Storage API (file explorer with upload/download) - RoadCoin Blockchain API (mining, transactions, wallet) - AI Chat API (conversations with AI assistant) **Database Models:** - User accounts with wallet integration - Email and folder management - Social media posts and engagement - Video metadata and analytics - File storage with sharing - Blockchain blocks and transactions - AI conversation history **Features:** - Complete CRUD operations for all services - Real-time blockchain mining with proof-of-work - Transaction validation and wallet management - File upload with S3 integration (ready) - Social feed with engagement metrics - Email system with threading support - AI chat with conversation persistence **Documentation:** - Comprehensive README with setup instructions - API documentation (Swagger/ReDoc auto-generated) - Deployment guide for multiple platforms - Testing framework with pytest **DevOps:** - Docker containerization - Docker Compose for local development - Database migrations with Alembic - Health check endpoints - Makefile for common tasks All APIs are production-ready with proper error handling, input validation, and security measures.
This commit is contained in:
141
backend/app/main.py
Normal file
141
backend/app/main.py
Normal file
@@ -0,0 +1,141 @@
|
||||
"""Main FastAPI application"""
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
from contextlib import asynccontextmanager
|
||||
import time
|
||||
|
||||
from app.config import settings
|
||||
from app.database import async_engine, Base
|
||||
from app.redis_client import close_redis
|
||||
from app.routers import auth, email, social, video, files, blockchain, ai_chat
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""Application lifespan events"""
|
||||
# Startup
|
||||
print("Starting BlackRoad Operating System Backend...")
|
||||
|
||||
# Create database tables
|
||||
async with async_engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
|
||||
print("Database tables created successfully")
|
||||
print(f"Server running on {settings.ENVIRONMENT} mode")
|
||||
|
||||
yield
|
||||
|
||||
# Shutdown
|
||||
print("Shutting down...")
|
||||
await close_redis()
|
||||
await async_engine.dispose()
|
||||
print("Shutdown complete")
|
||||
|
||||
|
||||
# Create FastAPI app
|
||||
app = FastAPI(
|
||||
title=settings.APP_NAME,
|
||||
version=settings.APP_VERSION,
|
||||
description="Backend API for BlackRoad Operating System - A Windows 95-inspired web OS",
|
||||
lifespan=lifespan,
|
||||
docs_url="/api/docs",
|
||||
redoc_url="/api/redoc",
|
||||
openapi_url="/api/openapi.json"
|
||||
)
|
||||
|
||||
# CORS middleware
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=settings.allowed_origins_list,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
# Request timing middleware
|
||||
@app.middleware("http")
|
||||
async def add_process_time_header(request: Request, call_next):
|
||||
"""Add processing time header to responses"""
|
||||
start_time = time.time()
|
||||
response = await call_next(request)
|
||||
process_time = time.time() - start_time
|
||||
response.headers["X-Process-Time"] = str(process_time)
|
||||
return response
|
||||
|
||||
|
||||
# Error handlers
|
||||
@app.exception_handler(404)
|
||||
async def not_found_handler(request: Request, exc):
|
||||
"""Handle 404 errors"""
|
||||
return JSONResponse(
|
||||
status_code=404,
|
||||
content={"detail": "Resource not found"}
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(500)
|
||||
async def internal_error_handler(request: Request, exc):
|
||||
"""Handle 500 errors"""
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content={"detail": "Internal server error"}
|
||||
)
|
||||
|
||||
|
||||
# Include routers
|
||||
app.include_router(auth.router)
|
||||
app.include_router(email.router)
|
||||
app.include_router(social.router)
|
||||
app.include_router(video.router)
|
||||
app.include_router(files.router)
|
||||
app.include_router(blockchain.router)
|
||||
app.include_router(ai_chat.router)
|
||||
|
||||
|
||||
# Root endpoint
|
||||
@app.get("/")
|
||||
async def root():
|
||||
"""Root endpoint"""
|
||||
return {
|
||||
"name": settings.APP_NAME,
|
||||
"version": settings.APP_VERSION,
|
||||
"environment": settings.ENVIRONMENT,
|
||||
"docs": "/api/docs",
|
||||
"status": "operational"
|
||||
}
|
||||
|
||||
|
||||
# Health check
|
||||
@app.get("/health")
|
||||
async def health_check():
|
||||
"""Health check endpoint"""
|
||||
return {
|
||||
"status": "healthy",
|
||||
"timestamp": time.time()
|
||||
}
|
||||
|
||||
|
||||
# API info
|
||||
@app.get("/api")
|
||||
async def api_info():
|
||||
"""API information"""
|
||||
return {
|
||||
"name": settings.APP_NAME,
|
||||
"version": settings.APP_VERSION,
|
||||
"endpoints": {
|
||||
"auth": "/api/auth",
|
||||
"email": "/api/email",
|
||||
"social": "/api/social",
|
||||
"videos": "/api/videos",
|
||||
"files": "/api/files",
|
||||
"blockchain": "/api/blockchain",
|
||||
"ai_chat": "/api/ai-chat"
|
||||
},
|
||||
"documentation": {
|
||||
"swagger": "/api/docs",
|
||||
"redoc": "/api/redoc",
|
||||
"openapi": "/api/openapi.json"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user