-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
170 lines (160 loc) · 5.82 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
OPSource API Server
Implements Bitcoin Core principles and AI development best practices
"""
from fastapi import FastAPI, HTTPException, Depends, Request
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import OAuth2PasswordBearer
from typing import Dict, Any, Optional
import logging
from pathlib import Path
from dev_config import DEV_CONFIG
# Configure logging
logging.basicConfig(
level=getattr(logging, DEV_CONFIG.logging.level),
format='%(asctime)s - %(levelname)s - %(message)s',
filename=str(Path(DEV_CONFIG.logging.dir) / 'api.log'),
filemode='a'
)
logger = logging.getLogger(__name__)
# Initialize FastAPI app
app = FastAPI(
title="OPSource API",
description="Bitcoin Core and AI Development API",
version="1.0.0"
)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=DEV_CONFIG.security.cors_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Security
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
@app.get("/")
async def root():
"""Root endpoint"""
return {
"name": "OPSource API",
"version": "1.0.0",
"status": "operational",
"bitcoin_network": DEV_CONFIG.bitcoin.network.value
}
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {
"status": "healthy",
"components": {
"bitcoin": {
"network": DEV_CONFIG.bitcoin.network.value,
"pruned": DEV_CONFIG.bitcoin.prune,
"prune_size": f"{DEV_CONFIG.bitcoin.prune_size}MB" if DEV_CONFIG.bitcoin.prune else "N/A",
"taproot": {
"enabled": DEV_CONFIG.bitcoin.taproot.enabled,
"schnorr_signatures": DEV_CONFIG.bitcoin.taproot.schnorr_signatures
},
"dlc": {
"enabled": DEV_CONFIG.bitcoin.dlc.enabled,
"privacy_enhanced": DEV_CONFIG.bitcoin.dlc.privacy_enhanced
}
},
"web5": {
"did_enabled": DEV_CONFIG.bitcoin.web5.did_enabled,
"handshake_enabled": DEV_CONFIG.bitcoin.web5.handshake_enabled,
"decentralized_storage": DEV_CONFIG.bitcoin.web5.decentralized_storage
},
"layer2": {
"lightning": {
"enabled": DEV_CONFIG.bitcoin.lightning.enabled,
"watchtower": DEV_CONFIG.bitcoin.lightning.watchtower_enabled
},
"rgb": {
"enabled": DEV_CONFIG.bitcoin.rgb.enabled,
"taproot_integration": DEV_CONFIG.bitcoin.rgb.taproot_integration
},
"rsk": {
"enabled": DEV_CONFIG.bitcoin.rsk.enabled,
"merge_mining": DEV_CONFIG.bitcoin.rsk.merge_mining
},
"stacks": {
"enabled": DEV_CONFIG.bitcoin.stacks.enabled,
"network": DEV_CONFIG.bitcoin.stacks.network
}
},
"ai": {
"federated_learning": DEV_CONFIG.ai.federated_learning,
"privacy_preserving": DEV_CONFIG.ai.privacy_preserving,
"agent_framework": DEV_CONFIG.ai.agent_framework
}
}
}
@app.get("/config")
async def get_config(token: str = Depends(oauth2_scheme)):
"""Get configuration (requires authentication)"""
try:
# Remove sensitive information
safe_config = {
"bitcoin": {
"network": DEV_CONFIG.bitcoin.network.value,
"prune": DEV_CONFIG.bitcoin.prune,
"prune_size": DEV_CONFIG.bitcoin.prune_size,
"discover_nodes": DEV_CONFIG.bitcoin.discover_nodes,
"max_connections": DEV_CONFIG.bitcoin.max_connections,
"taproot": {
"enabled": DEV_CONFIG.bitcoin.taproot.enabled,
"asset_issuance": DEV_CONFIG.bitcoin.taproot.asset_issuance
}
},
"web5": {
"did_enabled": DEV_CONFIG.bitcoin.web5.did_enabled,
"handshake_enabled": DEV_CONFIG.bitcoin.web5.handshake_enabled,
"decentralized_storage": DEV_CONFIG.bitcoin.web5.decentralized_storage
},
"ai": {
"federated_learning": DEV_CONFIG.ai.federated_learning,
"privacy_preserving": DEV_CONFIG.ai.privacy_preserving,
"agent_framework": DEV_CONFIG.ai.agent_framework,
"task_management": DEV_CONFIG.ai.task_management
}
}
return safe_config
except Exception as e:
logger.error(f"Error retrieving configuration: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
# Error handlers
@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
"""Handle HTTP exceptions"""
return JSONResponse(
status_code=exc.status_code,
content={
"status": "error",
"code": exc.status_code,
"message": exc.detail
}
)
@app.exception_handler(Exception)
async def general_exception_handler(request: Request, exc: Exception):
"""Handle general exceptions"""
logger.error(f"Unhandled exception: {str(exc)}")
return JSONResponse(
status_code=500,
content={
"status": "error",
"code": 500,
"message": "Internal server error"
}
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"main:app",
host=DEV_CONFIG.api.host,
port=DEV_CONFIG.api.port,
reload=DEV_CONFIG.dev_server.hot_reload,
workers=DEV_CONFIG.api.workers
)