# AtlasOS - Calypso Backend

Enterprise-grade backup appliance platform backend API.

## Prerequisites

- Go 1.22 or later
- PostgreSQL 14 or later
- Ubuntu Server 24.04 LTS

## Installation

1. Install system requirements:
   ```bash
   sudo ./scripts/install-requirements.sh
   ```

2. Create PostgreSQL database:
   ```bash
   sudo -u postgres createdb calypso
   sudo -u postgres createuser calypso
   sudo -u postgres psql -c "ALTER USER calypso WITH PASSWORD 'your_password';"
   sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE calypso TO calypso;"
   ```

3. Install Go dependencies:
   ```bash
   cd backend
   go mod download
   ```

4. Configure the application:
   ```bash
   sudo mkdir -p /etc/calypso
   sudo cp config.yaml.example /etc/calypso/config.yaml
   sudo nano /etc/calypso/config.yaml
   ```

   Set environment variables:
   ```bash
   export CALYPSO_DB_PASSWORD="your_database_password"
   export CALYPSO_JWT_SECRET="your_jwt_secret_key_min_32_chars"
   ```

## Building

```bash
cd backend
go build -o bin/calypso-api ./cmd/calypso-api
```

## Running Locally

```bash
cd backend
export CALYPSO_DB_PASSWORD="your_password"
export CALYPSO_JWT_SECRET="your_jwt_secret"
go run ./cmd/calypso-api -config config.yaml.example
```

The API will be available at `http://localhost:8080`

## API Endpoints

### Health Check
- `GET /api/v1/health` - System health status

### Authentication
- `POST /api/v1/auth/login` - User login
- `POST /api/v1/auth/logout` - User logout
- `GET /api/v1/auth/me` - Get current user info (requires auth)

### Tasks
- `GET /api/v1/tasks/{id}` - Get task status (requires auth)

### IAM (Admin only)
- `GET /api/v1/iam/users` - List users
- `GET /api/v1/iam/users/{id}` - Get user
- `POST /api/v1/iam/users` - Create user
- `PUT /api/v1/iam/users/{id}` - Update user
- `DELETE /api/v1/iam/users/{id}` - Delete user

## Database Migrations

Migrations are automatically run on startup. They are located in:
- `internal/common/database/migrations/`

## Project Structure

```
backend/
├── cmd/
│   └── calypso-api/          # Main application entry point
├── internal/
│   ├── auth/                  # Authentication handlers
│   ├── iam/                   # Identity and access management
│   ├── audit/                 # Audit logging middleware
│   ├── tasks/                  # Async task engine
│   ├── system/                 # System management (future)
│   ├── monitoring/             # Monitoring (future)
│   └── common/                 # Shared utilities
│       ├── config/            # Configuration management
│       ├── database/          # Database connection and migrations
│       ├── logger/            # Structured logging
│       └── router/            # HTTP router setup
├── db/
│   └── migrations/            # Database migration files
└── config.yaml.example        # Example configuration
```

## Development

### Running Tests
```bash
go test ./...
```

### Code Formatting
```bash
go fmt ./...
```

### Building for Production
```bash
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o bin/calypso-api ./cmd/calypso-api
```

## Systemd Service

To install as a systemd service:

```bash
sudo cp deploy/systemd/calypso-api.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable calypso-api
sudo systemctl start calypso-api
```

## Security Notes

- The JWT secret must be a strong random string (minimum 32 characters)
- Database passwords should be set via environment variables, not config files
- The service runs as non-root user `calypso`
- All mutating operations are audited

## License

Proprietary - AtlasOS Calypso

