Compare commits

...
Author SHA1 Message Date
adminandClaude Opus 4.5 d15ef08214 Support API keys from environment variables
Check OPENAI_API_KEY and ANTHROPIC_API_KEY environment variables first,
fall back to api_keys.txt file if not set. This allows the Docker
container to receive API keys via environment without needing a file.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-24 11:35:38 -08:00
2 changed files with 26 additions and 15 deletions
+1
View File
@@ -69,6 +69,7 @@ services:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./certbot/conf:/etc/letsencrypt:ro
- ./certbot/www:/var/www/certbot:ro
- ./auth:/etc/nginx/auth:ro
depends_on:
- eagle
restart: unless-stopped
@@ -7,7 +7,11 @@ object ApiKeys {
private val apiKeyFilePath =
"src/main/scala/net/eagle0/common/llm_integration/api_keys.txt"
// These must have valid values set in api_keys.txt
// Environment variable names
private val openAIEnvVar = "OPENAI_API_KEY"
private val anthropicEnvVar = "ANTHROPIC_API_KEY"
// File key names
private val openAIKeyName = "openai_api_key"
private val anthropicKeyName = "anthropic_api_key"
@@ -23,25 +27,31 @@ object ApiKeys {
)
}.toMap
}.getOrElse {
throw new ApiKeyException(
"Failed to read api_keys.txt. Make sure you have copied api_keys_template.txt to api_keys.txt and set the keys."
)
Map.empty // Return empty map if file doesn't exist; we'll check env vars
}
private val dictionary = readDictionary()
private def getKey(key: String): String =
dictionary.get(key) match {
case None =>
throw new ApiKeyException(s"$key not found")
case Some(value) if value.startsWith("your_") =>
throw new ApiKeyException(
s"API key for $key is not set. Please set it in $apiKeyFilePath."
)
case Some(value) => value
private def getKey(key: String, envVar: String): String =
// First check environment variable
Option(System.getenv(envVar)).filter(_.nonEmpty) match {
case Some(value) => value
case None =>
// Fall back to file-based dictionary
dictionary.get(key) match {
case None =>
throw new ApiKeyException(
s"$key not found. Set $envVar environment variable or add to $apiKeyFilePath."
)
case Some(value) if value.startsWith("your_") =>
throw new ApiKeyException(
s"API key for $key is not set. Set $envVar environment variable or update $apiKeyFilePath."
)
case Some(value) => value
}
}
lazy val openAI: String = getKey(openAIKeyName)
lazy val openAI: String = getKey(openAIKeyName, openAIEnvVar)
lazy val anthropic: String = getKey(anthropicKeyName)
lazy val anthropic: String = getKey(anthropicKeyName, anthropicEnvVar)
}