- Add OAuth 2.0 support with authorization code flow and dynamic client registration. - Introduce new handlers for OAuth metadata, client registration, authorization, and token issuance. - Enhance authentication middleware to support OAuth client credentials. - Create in-memory stores for authorization codes and tokens. - Update configuration to include OAuth client details. - Ensure validation checks for OAuth clients in the configuration.
121 lines
2.7 KiB
Go
121 lines
2.7 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func Load(explicitPath string) (*Config, string, error) {
|
|
path := ResolvePath(explicitPath)
|
|
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, path, fmt.Errorf("read config %q: %w", path, err)
|
|
}
|
|
|
|
cfg := defaultConfig()
|
|
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
|
return nil, path, fmt.Errorf("decode config %q: %w", path, err)
|
|
}
|
|
|
|
applyEnvOverrides(&cfg)
|
|
if err := cfg.Validate(); err != nil {
|
|
return nil, path, err
|
|
}
|
|
|
|
return &cfg, path, nil
|
|
}
|
|
|
|
func ResolvePath(explicitPath string) string {
|
|
if path := strings.TrimSpace(explicitPath); path != "" {
|
|
if path != ".yaml" && path != ".yml" {
|
|
return path
|
|
}
|
|
}
|
|
|
|
if envPath := strings.TrimSpace(os.Getenv("AMCS_CONFIG")); envPath != "" {
|
|
return envPath
|
|
}
|
|
|
|
return DefaultConfigPath
|
|
}
|
|
|
|
func defaultConfig() Config {
|
|
return Config{
|
|
Server: ServerConfig{
|
|
Host: "0.0.0.0",
|
|
Port: 8080,
|
|
ReadTimeout: 15 * time.Second,
|
|
WriteTimeout: 30 * time.Second,
|
|
IdleTimeout: 60 * time.Second,
|
|
},
|
|
MCP: MCPConfig{
|
|
Path: "/mcp",
|
|
ServerName: "amcs",
|
|
Version: "0.1.0",
|
|
Transport: "streamable_http",
|
|
},
|
|
Auth: AuthConfig{
|
|
HeaderName: "x-brain-key",
|
|
QueryParam: "key",
|
|
},
|
|
AI: AIConfig{
|
|
Provider: "litellm",
|
|
Embeddings: AIEmbeddingConfig{
|
|
Model: "openai/text-embedding-3-small",
|
|
Dimensions: 1536,
|
|
},
|
|
Metadata: AIMetadataConfig{
|
|
Model: "gpt-4o-mini",
|
|
Temperature: 0.1,
|
|
},
|
|
Ollama: OllamaConfig{
|
|
BaseURL: "http://localhost:11434/v1",
|
|
APIKey: "ollama",
|
|
},
|
|
},
|
|
Capture: CaptureConfig{
|
|
Source: DefaultSource,
|
|
MetadataDefaults: CaptureMetadataDefault{
|
|
Type: "observation",
|
|
TopicFallback: "uncategorized",
|
|
},
|
|
},
|
|
Search: SearchConfig{
|
|
DefaultLimit: 10,
|
|
DefaultThreshold: 0.5,
|
|
MaxLimit: 50,
|
|
},
|
|
Logging: LoggingConfig{
|
|
Level: "info",
|
|
Format: "json",
|
|
},
|
|
}
|
|
}
|
|
|
|
func applyEnvOverrides(cfg *Config) {
|
|
overrideString(&cfg.Database.URL, "AMCS_DATABASE_URL")
|
|
overrideString(&cfg.AI.LiteLLM.BaseURL, "AMCS_LITELLM_BASE_URL")
|
|
overrideString(&cfg.AI.LiteLLM.APIKey, "AMCS_LITELLM_API_KEY")
|
|
overrideString(&cfg.AI.Ollama.BaseURL, "AMCS_OLLAMA_BASE_URL")
|
|
overrideString(&cfg.AI.Ollama.APIKey, "AMCS_OLLAMA_API_KEY")
|
|
overrideString(&cfg.AI.OpenRouter.APIKey, "AMCS_OPENROUTER_API_KEY")
|
|
|
|
if value, ok := os.LookupEnv("AMCS_SERVER_PORT"); ok {
|
|
if port, err := strconv.Atoi(strings.TrimSpace(value)); err == nil {
|
|
cfg.Server.Port = port
|
|
}
|
|
}
|
|
}
|
|
|
|
func overrideString(target *string, envKey string) {
|
|
if value, ok := os.LookupEnv(envKey); ok {
|
|
*target = strings.TrimSpace(value)
|
|
}
|
|
}
|