Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Non-Functional Requirements #40

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 36 additions & 20 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
*.dylib
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# Test binary, built with `go test -c`
*.test
# Virtual Environment
venv/
env/
ENV/

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# IDE
.idea/
.vscode/
*.swp
*.swo

# Dependency directories (remove the comment below to include it)
# vendor/
# Testing
.coverage
coverage.xml
htmlcov/
.pytest_cache/

# Go workspace file
go.work
go.work.sum

# env file
.env
# Misc
.DS_Store
76 changes: 75 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# CodeGate
# Codegate

A configurable Generative AI gateway, protecting developers from the dangers of AI.

## Features

- Secrets exflitration prevention
- Secure Coding recommendations
- Preventing AI from recommending deprecated and / or malicious libraries


### Installation

Expand Down Expand Up @@ -30,5 +39,70 @@ VSCode editor.

![Continue Chat](./static/image.png)

## Usage

### Basic Usage (Manual)

Start the server with default settings:

```bash
codegate serve
```

### Custom Configuration

Start with custom settings:

```bash
codegate serve --port 8989 --host localhost --log-level DEBUG
```

### Configuration File

Use a YAML configuration file:

```bash
codegate serve --config my_config.yaml
```

Example `config.yaml`:

```yaml
port: 8989
host: "localhost"
log_level: "DEBUG"
```

### Environment Variables

Configure using environment variables:

```bash
export CODEGATE_APP_PORT=8989
export CODEGATE_APP_HOST=localhost
export CODEGATE_APP_LOG_LEVEL=DEBUG
codegate serve
```

## Development

### Setup

```bash
# Clone the repository
git clone https://github.com/stacklok/codegate.git
cd codegate

# Create and activate a virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate

# Install development dependencies
pip install -e ".[dev]"
```

### Testing

```bash
pytest
```
12 changes: 12 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Codegate Example Configuration

# Network settings
port: 8989 # Port to listen on (1-65535)
host: "localhost" # Host to bind to (use localhost for all interfaces)

# Logging configuration
log_level: "INFO" # One of: ERROR, WARNING, INFO, DEBUG

# Note: This configuration can be overridden by:
# 1. CLI arguments (--port, --host, --log-level)
# 2. Environment variables (CODEGATE_APP_PORT, CODEGATE_APP_HOST, CODEGATE_APP_LOG_LEVEL)
17 changes: 17 additions & 0 deletions config.yaml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Codegate Example Configuration

# Network settings
port: 8989 # Port to listen on (1-65535)
host: "localhost" # Host to bind to (use localhost for all interfaces)

# Logging configuration
log_level: "INFO" # One of: ERROR, WARNING, INFO, DEBUG
log_format: "JSON" # One of: JSON, TEXT

# Note: This configuration can be overridden by:
# 1. CLI arguments (--port, --host, --log-level, --log-format)
# 2. Environment variables:
# - CODEGATE_APP_PORT
# - CODEGATE_APP_HOST
# - CODEGATE_APP_LOG_LEVEL
# - CODEGATE_LOG_FORMAT
74 changes: 74 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# CLI Commands and Flags

Codegate provides a command-line interface through `cli.py` with the following
structure:

## Main Command

```bash
codegate [OPTIONS] COMMAND [ARGS]...
```

## Available Commands

### serve

Start the Codegate server:

```bash
codegate serve [OPTIONS]
```

#### Options

- `--port INTEGER`: Port to listen on (default: 8989)
- Must be between 1 and 65535
- Overrides configuration file and environment variables

- `--host TEXT`: Host to bind to (default: localhost)
- Overrides configuration file and environment variables

- `--log-level [ERROR|WARNING|INFO|DEBUG]`: Set the log level (default: INFO)
- Case-insensitive
- Overrides configuration file and environment variables

- `--log-format [JSON|TEXT]`: Set the log format (default: JSON)
- Case-insensitive
- Overrides configuration file and environment variables

- `--config FILE`: Path to YAML config file
- Optional
- Must be a valid YAML file
- Configuration values can be overridden by environment variables and CLI options

## Error Handling

The CLI provides user-friendly error messages for:
- Invalid port numbers
- Invalid log levels
- Invalid log formats
- Configuration file errors
- Server startup failures

All errors are output to stderr with appropriate exit codes.

## Examples

Start server with default settings:
```bash
codegate serve
```

Start server on specific port and host:
```bash
codegate serve --port 8989 --host localhost
```

Start server with custom logging:
```bash
codegate serve --log-level DEBUG --log-format TEXT
```

Start server with configuration file:
```bash
codegate serve --config my-config.yaml
68 changes: 68 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Configuration System

The configuration system in Codegate is managed through the `Config` class in `config.py`. It supports multiple configuration sources with a clear priority order.

## Configuration Priority (highest to lowest)

1. CLI arguments
2. Environment variables
3. Config file (YAML)
4. Default values

## Default Configuration Values

- Port: 8989
- Host: "localhost"
- Log Level: "INFO"
- Log Format: "JSON"

## Configuration Methods

### From File

Load configuration from a YAML file:

```python
config = Config.from_file("config.yaml")
```

### From Environment Variables

Environment variables are automatically loaded with these mappings:

- `CODEGATE_APP_PORT`: Server port
- `CODEGATE_APP_HOST`: Server host
- `CODEGATE_APP_LOG_LEVEL`: Logging level
- `CODEGATE_LOG_FORMAT`: Log format

```python
config = Config.from_env()
```

## Configuration Options

### Log Levels

Available log levels (case-insensitive):

- `ERROR`
- `WARNING`
- `INFO`
- `DEBUG`

### Log Formats

Available log formats (case-insensitive):

- `JSON`
- `TEXT`

## Error Handling

The configuration system uses a custom `ConfigurationError` exception for handling configuration-related errors, such as:

- Invalid port numbers (must be between 1 and 65535)
- Invalid log levels
- Invalid log formats
- YAML parsing errors
- File reading errors
Loading