feat: opensearch客户端初始化 feat: 索引模板接口 ai: 开发准则 chore: TDD流水线脚本

This commit is contained in:
mouseleee
2025-11-16 22:17:16 +08:00
commit da3883205c
18 changed files with 2707 additions and 0 deletions

47
config/config.go Normal file
View File

@@ -0,0 +1,47 @@
// Package config provides global configuration management for the application.
// This is a simplified implementation suitable for the experimental phase.
package config
import (
"os"
"github.com/joho/godotenv"
)
var (
// Endpoint is the OpenSearch cluster endpoint URL.
Endpoint string
// Region is the AWS region where the OpenSearch cluster is located.
Region string
// AccessKey is the AWS access key ID for authentication.
AccessKey string
// SecretKey is the AWS secret access key for authentication.
SecretKey string
)
// Load loads configuration from a .env file and then from environment variables.
// If the file does not exist, it only loads from environment variables without error.
func Load(filePath string) error {
// Load .env file if it exists
// This will set environment variables from the file
_ = godotenv.Load(filePath)
// Initialize global config from environment variables
Endpoint = os.Getenv("ES_ENDPOINT")
AccessKey = os.Getenv("AWS_ACCESS_KEY_ID")
SecretKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
Region = os.Getenv("AWS_REGION")
return nil
}
// Init initializes the global configuration from environment variables.
func Init() {
Endpoint = os.Getenv("ES_ENDPOINT")
AccessKey = os.Getenv("AWS_ACCESS_KEY_ID")
SecretKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
Region = os.Getenv("AWS_REGION")
}

114
config/config_test.go Normal file
View File

@@ -0,0 +1,114 @@
package config
import (
"os"
"testing"
)
func TestInit(t *testing.T) {
// Save original env vars
origEndpoint := os.Getenv("ES_ENDPOINT")
origRegion := os.Getenv("AWS_REGION")
origAccessKey := os.Getenv("AWS_ACCESS_KEY_ID")
origSecretKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
// Restore original env vars after test
defer func() {
os.Setenv("ES_ENDPOINT", origEndpoint)
os.Setenv("AWS_REGION", origRegion)
os.Setenv("AWS_ACCESS_KEY_ID", origAccessKey)
os.Setenv("AWS_SECRET_ACCESS_KEY", origSecretKey)
}()
// Set test env vars
os.Setenv("ES_ENDPOINT", "https://test.example.com")
os.Setenv("AWS_REGION", "us-west-2")
os.Setenv("AWS_ACCESS_KEY_ID", "test-access-key")
os.Setenv("AWS_SECRET_ACCESS_KEY", "test-secret-key")
// Test Init
Init()
if Endpoint != "https://test.example.com" {
t.Errorf("Endpoint = %v, want %v", Endpoint, "https://test.example.com")
}
if Region != "us-west-2" {
t.Errorf("Region = %v, want %v", Region, "us-west-2")
}
if AccessKey != "test-access-key" {
t.Errorf("AccessKey = %v, want %v", AccessKey, "test-access-key")
}
if SecretKey != "test-secret-key" {
t.Errorf("SecretKey = %v, want %v", SecretKey, "test-secret-key")
}
}
func TestLoad(t *testing.T) {
// Save original env vars
origEndpoint := os.Getenv("ES_ENDPOINT")
origRegion := os.Getenv("AWS_REGION")
origAccessKey := os.Getenv("AWS_ACCESS_KEY_ID")
origSecretKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
// Clear environment variables before test
os.Unsetenv("ES_ENDPOINT")
os.Unsetenv("AWS_REGION")
os.Unsetenv("AWS_ACCESS_KEY_ID")
os.Unsetenv("AWS_SECRET_ACCESS_KEY")
// Restore original env vars after test
defer func() {
if origEndpoint != "" {
os.Setenv("ES_ENDPOINT", origEndpoint)
}
if origRegion != "" {
os.Setenv("AWS_REGION", origRegion)
}
if origAccessKey != "" {
os.Setenv("AWS_ACCESS_KEY_ID", origAccessKey)
}
if origSecretKey != "" {
os.Setenv("AWS_SECRET_ACCESS_KEY", origSecretKey)
}
// Reload original config
Init()
}()
// Create a temporary .env file
tmpFile := ".env.test"
content := `ES_ENDPOINT=https://test-load.example.com
AWS_REGION=eu-west-1
AWS_ACCESS_KEY_ID=load-access-key
AWS_SECRET_ACCESS_KEY=load-secret-key
`
if err := os.WriteFile(tmpFile, []byte(content), 0644); err != nil {
t.Fatalf("failed to create test .env file: %v", err)
}
defer os.Remove(tmpFile)
// Test Load
if err := Load(tmpFile); err != nil {
t.Fatalf("Load() error = %v", err)
}
if Endpoint != "https://test-load.example.com" {
t.Errorf("Endpoint = %v, want %v", Endpoint, "https://test-load.example.com")
}
if Region != "eu-west-1" {
t.Errorf("Region = %v, want %v", Region, "eu-west-1")
}
if AccessKey != "load-access-key" {
t.Errorf("AccessKey = %v, want %v", AccessKey, "load-access-key")
}
if SecretKey != "load-secret-key" {
t.Errorf("SecretKey = %v, want %v", SecretKey, "load-secret-key")
}
}
func TestLoad_FileNotFound(t *testing.T) {
// Test with non-existent file - should not return error
err := Load("non-existent-file.env")
if err != nil {
t.Errorf("Load() with non-existent file should not error, got %v", err)
}
}