# Server Setup Information

## Server Details
- **Operating System:** Debian
- **Firewall:** iptables
- **Web Server (Proxy):** Apache
- **Technology:** FastAPI (Python Framework)
- **Additional Tools:** 
  - Python 3 (venv, pip)
  - Screen
  - OpenSSH Server

**Note:** All necessary passwords and keys for authentication are stored in Prestonet 1Password (L3 Network).

## Important Information

### Accessing the FastAPI Server
To enter the screen terminal where the FastAPI server is running, use the following command:

```bash
screen -r API
```

To detach from the screen session and return to the console, press:
```bash
Ctrl+A D
```

### Starting the API Server
To start the FastAPI server, run the following command:

```bash
uvicorn main:app --host 0.0.0.0 --port 8000
```

### Activating/Deactivating the Virtual Environment
To create and manage the Python virtual environment, use the following commands:

- Create the Virtual Env:
```bash
python3 -m venv /var/www/api/venv
```

- Activate the Virtual Env:
```bash
source /var/www/api/venv/bin/activate
```

- Deactivate the Virtual Env:
```bash
deactivate
```

## API Endpoints

### Authentication
All endpoints require an API key for authentication. The API key should be provided in the `x-api-key` header.

### Default Route
- **Endpoint:** `/`
- **Method:** `GET`
- **Description:** Returns a welcome message and authentication status.
- **Response:**
  ```json
  {
    "message": "Welcome to the Prestonet API server!",
    "auth": 1
  }
  ```

### Test Route
- **Endpoint:** `/test`
- **Method:** `GET`
- **Description:** Returns a test JSON message.
- **Response:**
  ```json
  {
    "message": "Hello there!"
  }
  ```

### QR Code Route
- **Endpoint:** `/qrcode`
- **Method:** `GET`
- **Parameters:**
  - `ssid`: Wi-Fi SSID
  - `password`: Wi-Fi password
  - `output_file`: Output file name (optional)
- **Description:** Generates a QR code for the provided Wi-Fi credentials.
- **Response:** Returns a PNG image of the QR code.

### Token Count Route
- **Endpoint** `/gpt-tokens`
- **Method:** `GET`
- **Parameters:**
  - `prompt`: Prompt for AI Assistent
  - `model`: ChatGPT model
- **Description:** Counts number of tokens from provided prompt in specified AI model
- **Response:** 
 ```json
 {
   "number_of_tokens" : 4
 }
 ``` 

### Logging Webhooks
- **Endpoint:** `/log/prestonet-webhooks`
- **Method:** `POST`
- **Description:** Logs incoming webhook data to a file.
- **Request Body:** JSON data to be logged.
- **Response:**
  ```json
  {
    "message": "Log saved",
    "status": "success"
  }
  ```

## Logging
Logs for the Prestonet webhooks are stored in `logs/prestonet_webhooks.log`.

## Scripts
The `scripts` directory contains the following scripts:
- `qrcode.py`: Contains the `generate_qrcode` function to create custom QR codes.
- `test.py`: Contains the `get_test_json` function to return test JSON data.
- `__init__.py`: Imports all functions from `qrcode.py` and `test.py`.

## .gitignore
The `.gitignore` file is configured to ignore common Python artifacts, virtual environments, and other unnecessary files.

## Contact
For any issues or questions, please contact Vojtech Pokorny at vojtech.pokorny@prestonet.cz.
