Skip to content

Commit d8d736f

Browse files
committed
add authoriztion
1 parent 66822cc commit d8d736f

File tree

4 files changed

+47
-21
lines changed

4 files changed

+47
-21
lines changed

internal/handler/semantic.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package handler
22

3-
43
import (
5-
"github.com/zgsm-ai/codebase-indexer/internal/response"
6-
"net/http"
74
"fmt"
5+
"net/http"
6+
87
"github.com/zeromicro/go-zero/rest/httpx"
98
"github.com/zgsm-ai/codebase-indexer/internal/logic"
9+
"github.com/zgsm-ai/codebase-indexer/internal/response"
1010
"github.com/zgsm-ai/codebase-indexer/internal/svc"
1111
"github.com/zgsm-ai/codebase-indexer/internal/types"
1212
)
@@ -20,12 +20,21 @@ func semanticSearchHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
2020
return
2121
}
2222

23+
// 从请求头获取 Authorization
24+
authorization := r.Header.Get("Authorization")
25+
26+
// 验证 Authorization 头是否存在
27+
if authorization == "" {
28+
response.Error(w, response.NewAuthError("missing Authorization header"))
29+
return
30+
}
31+
2332
// 打印请求参数用于调试
24-
fmt.Printf("Semantic search request received - ClientId: %s, CodebasePath: %s, Query: %s, TopK: %d, ScoreThreshold: %f\n",
25-
req.ClientId, req.CodebasePath, req.Query, req.TopK, req.ScoreThreshold)
33+
fmt.Printf("Semantic search request received - ClientId: %s, CodebasePath: %s, Query: %s, TopK: %d, ScoreThreshold: %f\n",
34+
req.ClientId, req.CodebasePath, req.Query, req.TopK, req.ScoreThreshold)
2635

2736
l := logic.NewSemanticSearchLogic(r.Context(), svcCtx)
28-
resp, err := l.SemanticSearch(&req)
37+
resp, err := l.SemanticSearch(&req, authorization)
2938
if err != nil {
3039
response.Error(w, err)
3140
} else {

internal/logic/semantic.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func NewSemanticSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Se
3737
}
3838
}
3939

40-
func (l *SemanticLogic) SemanticSearch(req *types.SemanticSearchRequest) (resp *types.SemanticSearchResponseData, err error) {
40+
func (l *SemanticLogic) SemanticSearch(req *types.SemanticSearchRequest, authorization string) (resp *types.SemanticSearchResponseData, err error) {
4141
topK := req.TopK
4242
if topK < minPositive {
4343
topK = defaultTopK
@@ -61,8 +61,13 @@ func (l *SemanticLogic) SemanticSearch(req *types.SemanticSearchRequest) (resp *
6161
ctx := context.WithValue(l.ctx, tracer.Key, tracer.RequestTraceId(int(codebase.ID)))
6262

6363
documents, err := l.svcCtx.VectorStore.Query(ctx, req.Query, topK,
64-
vector.Options{CodebaseId: codebase.ID, ClientId: clientId,
65-
CodebasePath: codebase.Path, CodebaseName: codebase.Name})
64+
vector.Options{
65+
CodebaseId: codebase.ID,
66+
ClientId: clientId,
67+
CodebasePath: codebase.Path,
68+
CodebaseName: codebase.Name,
69+
Authorization: authorization,
70+
})
6671
if err != nil {
6772
return nil, err
6873
}

internal/store/vector/vector_store.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ type Store interface {
2525
const vectorWeaviate = "weaviate"
2626

2727
type Options struct {
28-
CodebaseId int32
29-
SyncId int32
30-
RequestId string
31-
CodebasePath string
32-
CodebaseName string
33-
TotalFiles int
34-
ClientId string
28+
CodebaseId int32
29+
SyncId int32
30+
RequestId string
31+
CodebasePath string
32+
CodebaseName string
33+
TotalFiles int
34+
ClientId string
35+
Authorization string
3536
}
3637

3738
func NewVectorStore(cfg config.VectorStoreConf, embedder Embedder, reranker Reranker) (Store, error) {

internal/store/vector/weaviate_wrapper.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,15 @@ func (r *weaviateWrapper) SimilaritySearch(ctx context.Context, query string, nu
248248
return nil, fmt.Errorf("query weaviate failed: %w", err)
249249
}
250250

251-
items, err := r.unmarshalSimilarSearchResponse(res, options.CodebasePath, options.ClientId)
251+
items, err := r.unmarshalSimilarSearchResponse(res, options.CodebasePath, options.ClientId, options.Authorization)
252252
if err != nil {
253253
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
254254
}
255255

256256
return items, nil
257257
}
258258

259-
func (r *weaviateWrapper) unmarshalSimilarSearchResponse(res *models.GraphQLResponse, codebasePath, clientId string) ([]*types.SemanticFileItem, error) {
259+
func (r *weaviateWrapper) unmarshalSimilarSearchResponse(res *models.GraphQLResponse, codebasePath, clientId string, authorization string) ([]*types.SemanticFileItem, error) {
260260
// Get the data for our class
261261
data, ok := res.Data["Get"].(map[string]interface{})
262262
if !ok {
@@ -299,7 +299,7 @@ func (r *weaviateWrapper) unmarshalSimilarSearchResponse(res *models.GraphQLResp
299299

300300
// 通过fetchCodeContent接口获取代码片段
301301
if codebasePath != "" {
302-
fetchedContent, err := fetchCodeContent(context.Background(), r.cfg, clientId, codebasePath, filePath, startLine, endLine)
302+
fetchedContent, err := fetchCodeContent(context.Background(), r.cfg, clientId, codebasePath, filePath, startLine, endLine, authorization)
303303
if err == nil && fetchedContent != "" {
304304
content = fetchedContent
305305
}
@@ -694,7 +694,7 @@ func (r *weaviateWrapper) Query(ctx context.Context, query string, topK int, opt
694694
}
695695

696696
// fetchCodeContent 通过API获取代码片段的Content
697-
func fetchCodeContent(ctx context.Context, cfg config.VectorStoreConf, clientId, codebasePath, filePath string, startLine, endLine int) (string, error) {
697+
func fetchCodeContent(ctx context.Context, cfg config.VectorStoreConf, clientId, codebasePath, filePath string, startLine, endLine int, authorization string) (string, error) {
698698
// 构建API请求URL
699699
baseURL := cfg.BaseURL
700700

@@ -730,8 +730,19 @@ func fetchCodeContent(ctx context.Context, cfg config.VectorStoreConf, clientId,
730730

731731
tracer.WithTrace(ctx).Infof("fetchCodeContent %s: ", requestURL)
732732

733+
// 创建HTTP请求
734+
req, err := http.NewRequestWithContext(ctx, "GET", requestURL, nil)
735+
if err != nil {
736+
return "", fmt.Errorf("failed to create request: %w", err)
737+
}
738+
739+
// 添加Authorization头
740+
if authorization != "" {
741+
req.Header.Set("Authorization", authorization)
742+
}
743+
733744
// 发送HTTP GET请求
734-
resp, err := http.Get(requestURL)
745+
resp, err := http.DefaultClient.Do(req)
735746
if err != nil {
736747
return "", fmt.Errorf("failed to fetch code content: %w", err)
737748
}

0 commit comments

Comments
 (0)