msyql 容器化, aichatservice 容器化
This commit is contained in:
@@ -1,13 +1,9 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"github.com/spf13/viper"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
@@ -45,7 +41,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 {
|
||||
@@ -61,7 +56,6 @@ func InitConfig(filePath string, typ ...string) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
normalizeConfig(conf)
|
||||
overrideConfigFromEnv(conf)
|
||||
|
||||
}
|
||||
|
||||
@@ -98,88 +92,3 @@ func normalizeConfig(conf *Config) {
|
||||
conf.DependOn.AiChatService.Address = "localhost:50055"
|
||||
}
|
||||
}
|
||||
|
||||
func overrideConfigFromEnv(conf *Config) {
|
||||
overrideString(&conf.Http.IP, os.Getenv("SERVER_HOST"))
|
||||
overrideInt(&conf.Http.Port, os.Getenv("SERVER_PORT"))
|
||||
overrideString(&conf.BasicAuthUser, os.Getenv("BASIC_AUTH_USER"))
|
||||
overrideString(&conf.BasicAuthPassword, os.Getenv("BASIC_AUTH_PASSWORD"))
|
||||
overrideString(&conf.FrontendPath, os.Getenv("FRONTEND_PATH"))
|
||||
overrideString(&conf.Chat.Model, os.Getenv("OPENAI_MODEL"))
|
||||
overrideInt(&conf.Chat.MaxTokens, os.Getenv("OPENAI_MAX_TOKENS"))
|
||||
overrideScaledFloat32(&conf.Chat.Temperature, os.Getenv("OPENAI_TEMPERATURE"))
|
||||
overrideScaledFloat32(&conf.Chat.PresencePenalty, os.Getenv("OPENAI_PRESENCE_PENALTY"))
|
||||
overrideScaledFloat32(&conf.Chat.FrequencyPenalty, os.Getenv("OPENAI_FREQUENCY_PENALTY"))
|
||||
overrideInt(&conf.Chat.MinResponseTokens, os.Getenv("CHAT_MIN_RESPONSE_TOKENS"))
|
||||
overrideString(&conf.DependOn.AiChatService.Address, firstNonEmpty(
|
||||
os.Getenv("AI_CHAT_SERVICE_ADDRESS"),
|
||||
os.Getenv("AI_CHAT_SERVICE_ADDR"),
|
||||
))
|
||||
overrideString(&conf.DependOn.AiChatService.AccessToken, os.Getenv("AI_CHAT_SERVICE_ACCESS_TOKEN"))
|
||||
}
|
||||
|
||||
func loadProjectDotEnv(configFilePath string) {
|
||||
projectRoot := filepath.Dir(filepath.Dir(configFilePath))
|
||||
envPath := filepath.Join(projectRoot, ".env")
|
||||
file, err := os.Open(envPath)
|
||||
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)
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
if _, exists := os.LookupEnv(key); exists {
|
||||
continue
|
||||
}
|
||||
_ = os.Setenv(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
func firstNonEmpty(values ...string) string {
|
||||
for _, value := range values {
|
||||
if strings.TrimSpace(value) != "" {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func overrideString(target *string, value string) {
|
||||
if strings.TrimSpace(value) != "" {
|
||||
*target = strings.TrimSpace(value)
|
||||
}
|
||||
}
|
||||
|
||||
func overrideInt(target *int, value string) {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return
|
||||
}
|
||||
if parsed, err := strconv.Atoi(value); err == nil {
|
||||
*target = parsed
|
||||
}
|
||||
}
|
||||
|
||||
func overrideScaledFloat32(target *float32, value string) {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return
|
||||
}
|
||||
if parsed, err := strconv.ParseFloat(value, 32); err == nil {
|
||||
*target = float32(parsed / 100.0)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user