|
import logging |
|
import re |
|
import urllib.parse |
|
from utils.config import config |
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
class ConfigValidator: |
|
"""Utility for validating configuration values""" |
|
|
|
@staticmethod |
|
def validate_all() -> dict: |
|
"""Validate all configuration values and return report""" |
|
report = { |
|
'valid': True, |
|
'errors': [], |
|
'warnings': [] |
|
} |
|
|
|
|
|
logger.info(f"Current configuration:") |
|
logger.info(f" OLLAMA_HOST: '{config.ollama_host}'") |
|
|
|
|
|
if config.ollama_host: |
|
sanitized = config._sanitize_url(config.ollama_host) |
|
if sanitized != config.ollama_host: |
|
logger.info(f"Sanitized OLLAMA_HOST from '{config.ollama_host}' to '{sanitized}'") |
|
if not ConfigValidator._is_valid_url(sanitized): |
|
report['errors'].append(f"Invalid OLLAMA_HOST format: {config.ollama_host}") |
|
report['valid'] = False |
|
else: |
|
report['warnings'].append("OLLAMA_HOST not set, local Ollama won't be available") |
|
|
|
|
|
if config.hf_token: |
|
if len(config.hf_token) < 10: |
|
report['warnings'].append("HF_TOKEN seems too short to be valid") |
|
else: |
|
report['warnings'].append("HF_TOKEN not set, Hugging Face models won't be available") |
|
|
|
|
|
if config.openai_api_key: |
|
if not re.match(r'^sk-[a-zA-Z0-9]{32,}', config.openai_api_key): |
|
report['warnings'].append("OPENAI_API_KEY format looks invalid") |
|
else: |
|
report['warnings'].append("OPENAI_API_KEY not set, OpenAI models won't be available") |
|
|
|
return report |
|
|
|
@staticmethod |
|
def _is_valid_url(url: str) -> bool: |
|
"""Check if URL is valid""" |
|
try: |
|
result = urllib.parse.urlparse(url) |
|
return all([result.scheme, result.netloc]) |
|
except: |
|
return False |
|
|
|
@staticmethod |
|
def _is_valid_host(host: str) -> bool: |
|
"""Check if host is valid""" |
|
if not host: |
|
return False |
|
|
|
pattern = r'^[a-zA-Z0-9.-]+$' |
|
return bool(re.match(pattern, host)) |
|
|
|
def validate_configuration(): |
|
"""Validate configuration and log results""" |
|
validator = ConfigValidator() |
|
report = validator.validate_all() |
|
|
|
if report['errors']: |
|
logger.error("Configuration validation errors:") |
|
for error in report['errors']: |
|
logger.error(f" - {error}") |
|
|
|
if report['warnings']: |
|
logger.warning("Configuration warnings:") |
|
for warning in report['warnings']: |
|
logger.warning(f" - {warning}") |
|
|
|
return report['valid'] |
|
|
|
|
|
validate_configuration() |
|
|