Spaces:
No application file
No application file
File size: 6,115 Bytes
839a850 | 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | # FastAPI Server Startup Guide
## Prerequisites
Before starting the server, make sure you have all the required dependencies installed:
```bash
# Install FastAPI and related dependencies
pip install fastapi uvicorn python-dotenv
# Install any other dependencies your connectors need
pip install aiohttp requests # for API connectors
pip install pandas numpy # for data processing (if needed)
```
## Project Structure
Your project should look like this:
```
your-osint-project/
├── main.py # Your improved main file
├── .env # Environment variables (API keys)
├── requirements.txt # Python dependencies
├── frontend/
│ └── index.html # The frontend I created
├── connectors/
│ ├── __init__.py
│ ├── intelligencex.py
│ ├── enisa_cve.py
│ ├── virustotal.py
│ └── shodan.py
├── core/
│ ├── __init__.py
│ ├── normalize.py
│ ├── correlate.py
│ └── risk_engine.py
└── llm_agent/
├── __init__.py
├── llm_chatGPT.py
└── prompt_templates.py # The template file I created
```
## Environment Setup
Create a `.env` file in your project root with your API keys:
```env
# API Keys for OSINT sources
INTELLIGENCE_X_API_KEY=your_intelx_key_here
VIRUSTOTAL_API_KEY=your_virustotal_key_here
SHODAN_API_KEY=your_shodan_key_here
# LLM Configuration
OPENAI_API_KEY=your_openai_key_here
# OR if using Google Gemini:
GOOGLE_API_KEY=your_gemini_key_here
# Server Configuration
HOST=0.0.0.0
PORT=8000
DEBUG=True
```
## Method 1: Direct Python Execution
The easiest way to start your server:
```bash
# Navigate to your project directory
cd /path/to/your-osint-project
# Run the server directly
python main.py
```
This works because of the code at the bottom of your `main.py`:
```python
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000, log_level="info")
```
## Method 2: Using Uvicorn Command
More control over server settings:
```bash
# Basic startup
uvicorn main:app --reload
# With specific host and port
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
# With detailed logging
uvicorn main:app --host 0.0.0.0 --port 8000 --reload --log-level debug
```
### Uvicorn Options Explained:
- `main:app` - Refers to the `app` object in `main.py`
- `--reload` - Automatically restart when code changes (development only)
- `--host 0.0.0.0` - Accept connections from any IP
- `--port 8000` - Server port
- `--log-level debug` - Detailed logging
## Method 3: Production Deployment
For production environments:
```bash
# Install production server
pip install gunicorn
# Run with Gunicorn (more robust for production)
gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
```
## Verification Steps
Once your server starts, you should see output like:
```
INFO: Started server process [12345]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
```
### Test Your Server:
1. **Health Check:**
```bash
curl http://localhost:8000/api/health
```
Should return: `{"status": "healthy", "version": "1.0.0"}`
2. **Sources Check:**
```bash
curl http://localhost:8000/api/sources
```
3. **Frontend Access:**
Open your browser and go to: `http://localhost:8000`
4. **API Test:**
```bash
curl "http://localhost:8000/api/osint?entity=8.8.8.8"
```
## Common Issues and Solutions
### Issue 1: Frontend Not Loading
```
ERROR: Frontend directory not found
```
**Solution:** Make sure the frontend directory exists and contains `index.html`:
```bash
mkdir -p frontend
# Copy the HTML file I created to frontend/index.html
```
### Issue 2: Import Errors
```
ModuleNotFoundError: No module named 'connectors'
```
**Solution:** Make sure all directories have `__init__.py` files:
```bash
touch connectors/__init__.py
touch core/__init__.py
touch llm_agent/__init__.py
```
### Issue 3: API Key Issues
```
Error: Missing API key
```
**Solution:** Check your `.env` file is in the project root and contains the required keys.
### Issue 4: Port Already in Use
```
OSError: [Errno 98] Address already in use
```
**Solution:** Use a different port:
```bash
uvicorn main:app --port 8001 --reload
```
## Development Workflow
1. **Start the server in development mode:**
```bash
uvicorn main:app --reload --log-level debug
```
2. **Make changes to your code** - The server will automatically restart
3. **Test in browser:** Go to `http://localhost:8000`
4. **Check logs** in the terminal for any errors
## Production Deployment
For production, consider:
1. **Use environment variables** for configuration
2. **Set up proper logging**
3. **Use a process manager** like systemd or PM2
4. **Add reverse proxy** (nginx) for better performance
5. **Enable HTTPS** for security
Example systemd service file (`/etc/systemd/system/osint-api.service`):
```ini
[Unit]
Description=OSINT Early Warning API
After=network.target
[Service]
User=your-user
Group=your-group
WorkingDirectory=/path/to/your-osint-project
Environment=PATH=/path/to/your-venv/bin
ExecStart=/path/to/your-venv/bin/gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
Restart=always
[Install]
WantedBy=multi-user.target
```
## Quick Start Script
Create a `start_server.sh` script:
```bash
#!/bin/bash
echo "Starting OSINT Early Warning System..."
# Check if virtual environment exists
if [ ! -d "venv" ]; then
echo "Creating virtual environment..."
python3 -m venv venv
fi
# Activate virtual environment
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Start server
echo "Starting FastAPI server..."
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
```
Make it executable:
```bash
chmod +x start_server.sh
./start_server.sh
```
This should get your FastAPI server up and running! Let me know if you encounter any specific issues. |