[+]删除链路追踪,修改项目名称

This commit is contained in:
renzhiyuan 2025-09-11 10:11:16 +08:00
parent 285bba38f1
commit 0632b36a00
10 changed files with 72 additions and 19 deletions

View File

@ -30,10 +30,10 @@ func main() {
if os.Getenv("GIN_MODE") == "release" { if os.Getenv("GIN_MODE") == "release" {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
} else { } else {
config.SetEnv()
gin.SetMode(gin.DebugMode) gin.SetMode(gin.DebugMode)
} }
//config.SetEnv()
// Build dependency injection container // Build dependency injection container
c := container.BuildContainer(runtime.GetContainer()) c := container.BuildContainer(runtime.GetContainer())

View File

@ -1,6 +1,6 @@
services: services:
app: app:
image: wechatopenai/weknora-app:latest image: ai-app:latest
container_name: WeKnora-app container_name: WeKnora-app
ports: ports:
- "8080:8080" - "8080:8080"

View File

@ -4,11 +4,12 @@ import (
"context" "context"
"errors" "errors"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"knowlege-lsxd/internal/logger" "knowlege-lsxd/internal/logger"
"knowlege-lsxd/internal/types" "knowlege-lsxd/internal/types"
"knowlege-lsxd/internal/types/interfaces" "knowlege-lsxd/internal/types/interfaces"
"gorm.io/gorm"
"gorm.io/gorm/clause"
) )
var ( var (
@ -43,6 +44,18 @@ func (r *tenantRepository) GetTenantByID(ctx context.Context, id uint) (*types.T
return &tenant, nil return &tenant, nil
} }
// GetTenantByApiKey gets tenant by apikey
func (r *tenantRepository) GetTenantByApiKey(ctx context.Context, apikey string) (*types.Tenant, error) {
var tenant types.Tenant
if err := r.db.WithContext(ctx).Where("api_key = ?", apikey).First(&tenant).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrTenantNotFound
}
return nil, err
}
return &tenant, nil
}
// ListTenants lists all tenants // ListTenants lists all tenants
func (r *tenantRepository) ListTenants(ctx context.Context) ([]*types.Tenant, error) { func (r *tenantRepository) ListTenants(ctx context.Context) ([]*types.Tenant, error) {
var tenants []*types.Tenant var tenants []*types.Tenant

View File

@ -355,6 +355,7 @@ func (s *sessionService) KnowledgeQA(ctx context.Context, sessionID, query strin
Seed: session.SummaryParameters.Seed, Seed: session.SummaryParameters.Seed,
NoMatchPrefix: session.SummaryParameters.NoMatchPrefix, NoMatchPrefix: session.SummaryParameters.NoMatchPrefix,
MaxCompletionTokens: session.SummaryParameters.MaxCompletionTokens, MaxCompletionTokens: session.SummaryParameters.MaxCompletionTokens,
Thinking: session.SummaryParameters.Thinking,
}, },
FallbackResponse: session.FallbackResponse, FallbackResponse: session.FallbackResponse,
} }

View File

@ -102,6 +102,21 @@ func (s *tenantService) GetTenantByID(ctx context.Context, id uint) (*types.Tena
return tenant, nil return tenant, nil
} }
// GetTenantByID retrieves a tenant by their ID
func (s *tenantService) GetTenantByApiKey(ctx context.Context, apikey string) (*types.Tenant, error) {
tenant, err := s.repo.GetTenantByApiKey(ctx, apikey)
if err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
"apikey": apikey,
})
return nil, err
}
logger.Infof(ctx, "Tenant retrieved successfully, ID: %d, name: %s", tenant.ID, tenant.Name)
return tenant, nil
}
// ListTenants retrieves a list of all tenants // ListTenants retrieves a list of all tenants
func (s *tenantService) ListTenants(ctx context.Context) ([]*types.Tenant, error) { func (s *tenantService) ListTenants(ctx context.Context) ([]*types.Tenant, error) {
logger.Info(ctx, "Start retrieving tenant list") logger.Info(ctx, "Start retrieving tenant list")

View File

@ -210,6 +210,8 @@ func SetEnv() {
envSet("DOCREADER_ADDR", "127.0.0.1:50051") envSet("DOCREADER_ADDR", "127.0.0.1:50051")
envSet("MINIO_ENDPOINT", "127.0.0.1:9000") envSet("MINIO_ENDPOINT", "127.0.0.1:9000")
envSet("REDIS_ADDR", "127.0.0.1:6379") envSet("REDIS_ADDR", "127.0.0.1:6379")
envSet("OLLAMA_BASE_URL", "http://localhost:11434")
envSet("TENANT_AES_KEY", "knowlege-lsxd-2025-ase-secret")
} }

View File

@ -8,10 +8,11 @@ import (
"slices" "slices"
"strings" "strings"
"github.com/gin-gonic/gin"
"knowlege-lsxd/internal/config" "knowlege-lsxd/internal/config"
"knowlege-lsxd/internal/types" "knowlege-lsxd/internal/types"
"knowlege-lsxd/internal/types/interfaces" "knowlege-lsxd/internal/types/interfaces"
"github.com/gin-gonic/gin"
) )
// 无需认证的API列表 // 无需认证的API列表
@ -60,19 +61,19 @@ func Auth(tenantService interfaces.TenantService, cfg *config.Config) gin.Handle
} }
// Get tenant information // Get tenant information
tenantID, err := tenantService.ExtractTenantIDFromAPIKey(apiKey) //tenantID, err := tenantService.ExtractTenantIDFromAPIKey(apiKey)
if err != nil { //if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{ // c.JSON(http.StatusUnauthorized, gin.H{
"error": "Unauthorized: invalid API key format", // "error": "Unauthorized: invalid API key format",
}) // })
c.Abort() // c.Abort()
return // return
} //}
// Verify API key validity (matches the one in database) // Verify API key validity (matches the one in database)
t, err := tenantService.GetTenantByID(c.Request.Context(), tenantID) t, err := tenantService.GetTenantByApiKey(c.Request.Context(), apiKey)
if err != nil { if err != nil {
log.Printf("Error getting tenant by ID: %v, tenantID: %d, apiKey: %s", err, tenantID, apiKey) log.Printf("Error getting tenant by ID: %v, tenantID: %d, apiKey: %s", err, t.ID, apiKey)
c.JSON(http.StatusUnauthorized, gin.H{ c.JSON(http.StatusUnauthorized, gin.H{
"error": "Unauthorized: invalid API key", "error": "Unauthorized: invalid API key",
}) })
@ -89,11 +90,11 @@ func Auth(tenantService interfaces.TenantService, cfg *config.Config) gin.Handle
} }
// Store tenant ID in context // Store tenant ID in context
c.Set(types.TenantIDContextKey.String(), tenantID) c.Set(types.TenantIDContextKey.String(), t.ID)
c.Set(types.TenantInfoContextKey.String(), t) c.Set(types.TenantInfoContextKey.String(), t)
c.Request = c.Request.WithContext( c.Request = c.Request.WithContext(
context.WithValue( context.WithValue(
context.WithValue(c.Request.Context(), types.TenantIDContextKey, tenantID), context.WithValue(c.Request.Context(), types.TenantIDContextKey, t.ID),
types.TenantInfoContextKey, t, types.TenantInfoContextKey, t,
), ),
) )

View File

@ -8,6 +8,7 @@ import (
"os" "os"
"strings" "strings"
"sync" "sync"
"time"
"knowlege-lsxd/internal/logger" "knowlege-lsxd/internal/logger"
@ -28,7 +29,7 @@ func GetOllamaService() (*OllamaService, error) {
// Get Ollama base URL from environment variable, if not set use provided baseURL or default value // Get Ollama base URL from environment variable, if not set use provided baseURL or default value
logger.GetLogger(context.Background()).Infof("Ollama base URL: %s", os.Getenv("OLLAMA_BASE_URL")) logger.GetLogger(context.Background()).Infof("Ollama base URL: %s", os.Getenv("OLLAMA_BASE_URL"))
baseURL := "http://localhost:11434" baseURL := "http://localhost:11434"
envURL := "" //os.Getenv("OLLAMA_BASE_URL") envURL := os.Getenv("OLLAMA_BASE_URL")
if envURL != "" { if envURL != "" {
baseURL = envURL baseURL = envURL
} }
@ -285,6 +286,21 @@ func (s *OllamaService) Chat(ctx context.Context, req *api.ChatRequest, fn api.C
if err := s.StartService(ctx); err != nil { if err := s.StartService(ctx); err != nil {
return err return err
} }
if req.KeepAlive == nil {
req.KeepAlive = &api.Duration{
time.Hour * 24,
}
}
if req.Think != nil {
switch req.Think.Value {
case true:
req.Think = nil
case false:
req.Think = &api.ThinkValue{
Value: false,
}
}
}
// Use official client Chat method // Use official client Chat method
return s.client.Chat(ctx, req, fn) return s.client.Chat(ctx, req, fn)

View File

@ -66,6 +66,7 @@ func NewRouter(params RouterParams) *gin.Engine {
// 初始化接口(不需要认证) // 初始化接口(不需要认证)
r.GET("/api/v1/initialization/status", params.InitializationHandler.CheckStatus) r.GET("/api/v1/initialization/status", params.InitializationHandler.CheckStatus)
r.GET("/api/v1/initialization/config", params.InitializationHandler.GetCurrentConfig) r.GET("/api/v1/initialization/config", params.InitializationHandler.GetCurrentConfig)
r.POST("/api/v1/initialization/initialize", params.InitializationHandler.Initialize)
// Ollama相关接口不需要认证 // Ollama相关接口不需要认证
r.GET("/api/v1/initialization/ollama/status", params.InitializationHandler.CheckOllamaStatus) r.GET("/api/v1/initialization/ollama/status", params.InitializationHandler.CheckOllamaStatus)

View File

@ -12,6 +12,8 @@ type TenantService interface {
CreateTenant(ctx context.Context, tenant *types.Tenant) (*types.Tenant, error) CreateTenant(ctx context.Context, tenant *types.Tenant) (*types.Tenant, error)
// GetTenantByID gets a tenant by ID // GetTenantByID gets a tenant by ID
GetTenantByID(ctx context.Context, id uint) (*types.Tenant, error) GetTenantByID(ctx context.Context, id uint) (*types.Tenant, error)
// GetTenantByID gets a tenant by ID
GetTenantByApiKey(ctx context.Context, apikey string) (*types.Tenant, error)
// ListTenants lists all tenants // ListTenants lists all tenants
ListTenants(ctx context.Context) ([]*types.Tenant, error) ListTenants(ctx context.Context) ([]*types.Tenant, error)
// UpdateTenant updates a tenant // UpdateTenant updates a tenant
@ -30,6 +32,8 @@ type TenantRepository interface {
CreateTenant(ctx context.Context, tenant *types.Tenant) error CreateTenant(ctx context.Context, tenant *types.Tenant) error
// GetTenantByID gets a tenant by ID // GetTenantByID gets a tenant by ID
GetTenantByID(ctx context.Context, id uint) (*types.Tenant, error) GetTenantByID(ctx context.Context, id uint) (*types.Tenant, error)
// GetTenantByID gets a tenant by ID
GetTenantByApiKey(ctx context.Context, apikey string) (*types.Tenant, error)
// ListTenants lists all tenants // ListTenants lists all tenants
ListTenants(ctx context.Context) ([]*types.Tenant, error) ListTenants(ctx context.Context) ([]*types.Tenant, error)
// UpdateTenant updates a tenant // UpdateTenant updates a tenant