msyql 容器化, aichatservice 容器化

This commit is contained in:
1iaan
2026-04-05 17:49:10 +08:00
parent 9d7c416737
commit f433490e0d
9 changed files with 133 additions and 208 deletions

View File

@@ -1,11 +1,7 @@
package config
import (
"bufio"
"log"
"os"
"path/filepath"
"strings"
"github.com/spf13/viper"
)
@@ -102,7 +98,6 @@ type Config struct {
var conf *Config
func InitConfig(filePath string, typ ...string) {
loadProjectDotEnv(filePath)
v := viper.New()
v.SetConfigFile(filePath)
if len(typ) > 0 {
@@ -171,83 +166,4 @@ func normalizeConfig(conf *Config) {
if conf.Embedding.Timeout == 0 {
conf.Embedding.Timeout = 10
}
overrideChatFromEnv(conf)
overrideEmbeddingFromEnv(conf)
}
func overrideChatFromEnv(conf *Config) {
if value := os.Getenv("AI_CHAT_OPENAI_BASE_URL"); value != "" {
conf.Chat.BaseUrl = value
} else if value := os.Getenv("OPENAI_BASE_URL"); value != "" {
conf.Chat.BaseUrl = value
}
if value := os.Getenv("AI_CHAT_OPENAI_MODEL"); value != "" {
conf.Chat.Model = value
} else if value := os.Getenv("OPENAI_MODEL"); value != "" {
conf.Chat.Model = value
}
if value := os.Getenv("AI_CHAT_OPENAI_API_KEY"); value != "" {
conf.Chat.ApiKey = value
return
}
if value := os.Getenv("OPENAI_API_KEY"); value != "" {
conf.Chat.ApiKey = value
return
}
if value := os.Getenv("MOONSHOT_API_KEY"); value != "" {
conf.Chat.ApiKey = value
}
}
func overrideEmbeddingFromEnv(conf *Config) {
if value := os.Getenv("AI_CHAT_EMBEDDING_BASE_URL"); value != "" {
conf.Embedding.BaseUrl = value
}
if value := os.Getenv("AI_CHAT_EMBEDDING_MODEL"); value != "" {
conf.Embedding.Model = value
}
if value := os.Getenv("AI_CHAT_EMBEDDING_API_KEY"); value != "" {
conf.Embedding.ApiKey = value
return
}
if value := os.Getenv("ZAI_API_KEY"); value != "" {
conf.Embedding.ApiKey = value
}
}
func loadProjectDotEnv(configFilePath string) {
projectRoot := filepath.Dir(filepath.Dir(configFilePath))
loadDotEnvFile(filepath.Join(projectRoot, ".env"))
}
func loadDotEnvFile(path string) {
file, err := os.Open(path)
if err != nil {
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
key, value, ok := strings.Cut(line, "=")
if !ok {
continue
}
key = strings.TrimSpace(key)
value = strings.TrimSpace(value)
value = strings.Trim(value, `"'`)
if key == "" {
continue
}
if _, exists := os.LookupEnv(key); exists {
continue
}
_ = os.Setenv(key, value)
}
}