# MetaMuLate Proxy Server

The proxy server enables browser-based API requests that would otherwise be blocked by CORS restrictions.

## Quick Start

From the main MetaMuLate folder, run:
```bash
./start.sh      # macOS/Linux
start.bat       # Windows
```

This starts the proxy automatically.

## Manual Start

```bash
cd bin
./metamulate-proxy --config ../proxy-config.json
```

## Configuration

Edit `proxy-config.json`:

```json
{
  "log_level": 3,           // 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=verbose
  "cors_enabled": true,     // Enable CORS proxy
  "cors_port": 8080,        // CORS proxy port
  "snowflake_enabled": false,  // Enable Snowflake proxy
  "snowflake_port": 8081,      // Snowflake proxy port
  "query_timeout": 60,      // Query timeout in seconds
  "login_timeout": 30       // Login timeout in seconds
}
```

### Snowflake Configuration (Optional)

Add Snowflake credentials to enable database queries. Choose ONE authentication method:

#### Password Authentication

```json
{
  "snowflake_enabled": true,
  "snowflake_account": "your-account.us-east-1",
  "snowflake_user": "username",
  "snowflake_password": "password",
  "snowflake_database": "DATABASE",
  "snowflake_schema": "SCHEMA",
  "snowflake_warehouse": "WAREHOUSE",
  "snowflake_role": "ROLE"
}
```

#### External Browser / SSO Authentication (Recommended)

```json
{
  "snowflake_enabled": true,
  "snowflake_account": "your-account",
  "snowflake_user": "user@example.com",
  "snowflake_external_browser": true,
  "external_browser_timeout": 120,
  "snowflake_database": "DATABASE",
  "snowflake_schema": "SCHEMA",
  "snowflake_warehouse": "WAREHOUSE",
  "snowflake_role": "ROLE"
}
```

#### JWT / Key-Pair Authentication

```json
{
  "snowflake_enabled": true,
  "snowflake_account": "your-account.us-east-1",
  "snowflake_user": "username",
  "snowflake_private_key_path": "/path/to/private_key.pem",
  "snowflake_database": "DATABASE",
  "snowflake_schema": "SCHEMA",
  "snowflake_warehouse": "WAREHOUSE",
  "snowflake_role": "ROLE"
}
```

#### OAuth Authentication

```json
{
  "snowflake_enabled": true,
  "snowflake_account": "your-account.us-east-1",
  "snowflake_user": "username",
  "snowflake_oauth_token": "your_oauth_token",
  "snowflake_database": "DATABASE",
  "snowflake_schema": "SCHEMA",
  "snowflake_warehouse": "WAREHOUSE",
  "snowflake_role": "ROLE"
}
```

## Building from Source

Requires [Go 1.21+](https://golang.org/dl/)

```bash
cd code
go build -o ../bin/metamulate-proxy metamulate-proxy.go
```

Or build for all platforms:

```bash
cd build
./build-proxy.sh
```

## Folder Structure

```text
proxy/
├── proxy-config.json       # Your configuration
├── proxy-config.example.json
├── bin/                    # Compiled binaries
│   ├── metamulate-proxy-mac-arm
│   ├── metamulate-proxy-mac-intel
│   ├── metamulate-proxy.exe
│   └── metamulate-proxy-linux
├── build/                  # Build scripts
│   └── build-proxy.sh
└── code/                   # Source code
    ├── metamulate-proxy.go
    ├── go.mod
    └── go.sum
```

## Endpoints

### CORS Proxy (Port 8080)

```http
GET http://localhost:8080/?url=<encoded-target-url>
```

### Snowflake Proxy (Port 8081)

```http
POST http://localhost:8081/query
Content-Type: application/json

{"query": "SELECT * FROM table LIMIT 10"}
```
