Add full monorepo: virtual-banker, backend, frontend, docs, scripts, deployment
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
42
backend/api/search/cmd/main.go
Normal file
42
backend/api/search/cmd/main.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/elastic/go-elasticsearch/v8"
|
||||
"github.com/explorer/backend/api/search"
|
||||
"github.com/explorer/backend/search/config"
|
||||
)
|
||||
|
||||
func main() {
|
||||
searchConfig := config.LoadSearchConfig()
|
||||
|
||||
esConfig := elasticsearch.Config{
|
||||
Addresses: []string{searchConfig.URL},
|
||||
}
|
||||
|
||||
if searchConfig.Username != "" {
|
||||
esConfig.Username = searchConfig.Username
|
||||
esConfig.Password = searchConfig.Password
|
||||
}
|
||||
|
||||
client, err := elasticsearch.NewClient(esConfig)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create Elasticsearch client: %v", err)
|
||||
}
|
||||
|
||||
service := search.NewSearchService(client, searchConfig.IndexPrefix)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/api/v1/search", service.HandleSearch)
|
||||
|
||||
port := os.Getenv("SEARCH_PORT")
|
||||
if port == "" {
|
||||
port = "8082"
|
||||
}
|
||||
|
||||
log.Printf("Starting search service on :%s", port)
|
||||
log.Fatal(http.ListenAndServe(":"+port, mux))
|
||||
}
|
||||
172
backend/api/search/search.go
Normal file
172
backend/api/search/search.go
Normal file
@@ -0,0 +1,172 @@
|
||||
package search
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/elastic/go-elasticsearch/v8"
|
||||
"github.com/elastic/go-elasticsearch/v8/esapi"
|
||||
)
|
||||
|
||||
// SearchService handles unified search
|
||||
type SearchService struct {
|
||||
client *elasticsearch.Client
|
||||
indexPrefix string
|
||||
}
|
||||
|
||||
// NewSearchService creates a new search service
|
||||
func NewSearchService(client *elasticsearch.Client, indexPrefix string) *SearchService {
|
||||
return &SearchService{
|
||||
client: client,
|
||||
indexPrefix: indexPrefix,
|
||||
}
|
||||
}
|
||||
|
||||
// Search performs unified search across all indices
|
||||
func (s *SearchService) Search(ctx context.Context, query string, chainID *int, limit int) ([]SearchResult, error) {
|
||||
// Build search query
|
||||
var indices []string
|
||||
if chainID != nil {
|
||||
indices = []string{
|
||||
fmt.Sprintf("%s-blocks-%d", s.indexPrefix, *chainID),
|
||||
fmt.Sprintf("%s-transactions-%d", s.indexPrefix, *chainID),
|
||||
fmt.Sprintf("%s-addresses-%d", s.indexPrefix, *chainID),
|
||||
}
|
||||
} else {
|
||||
// Search all chains (simplified - would need to enumerate)
|
||||
indices = []string{
|
||||
fmt.Sprintf("%s-blocks-*", s.indexPrefix),
|
||||
fmt.Sprintf("%s-transactions-*", s.indexPrefix),
|
||||
fmt.Sprintf("%s-addresses-*", s.indexPrefix),
|
||||
}
|
||||
}
|
||||
|
||||
searchQuery := map[string]interface{}{
|
||||
"query": map[string]interface{}{
|
||||
"multi_match": map[string]interface{}{
|
||||
"query": query,
|
||||
"fields": []string{"hash", "address", "from_address", "to_address"},
|
||||
"type": "best_fields",
|
||||
},
|
||||
},
|
||||
"size": limit,
|
||||
}
|
||||
|
||||
queryJSON, _ := json.Marshal(searchQuery)
|
||||
queryString := string(queryJSON)
|
||||
|
||||
// Execute search
|
||||
req := esapi.SearchRequest{
|
||||
Index: indices,
|
||||
Body: strings.NewReader(queryString),
|
||||
Pretty: true,
|
||||
}
|
||||
|
||||
res, err := req.Do(ctx, s.client)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("search failed: %w", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.IsError() {
|
||||
return nil, fmt.Errorf("elasticsearch error: %s", res.String())
|
||||
}
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode response: %w", err)
|
||||
}
|
||||
|
||||
// Parse results
|
||||
results := []SearchResult{}
|
||||
if hits, ok := result["hits"].(map[string]interface{}); ok {
|
||||
if hitsList, ok := hits["hits"].([]interface{}); ok {
|
||||
for _, hit := range hitsList {
|
||||
if hitMap, ok := hit.(map[string]interface{}); ok {
|
||||
if source, ok := hitMap["_source"].(map[string]interface{}); ok {
|
||||
result := s.parseResult(source)
|
||||
results = append(results, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// SearchResult represents a search result
|
||||
type SearchResult struct {
|
||||
Type string `json:"type"`
|
||||
ChainID int `json:"chain_id"`
|
||||
Data map[string]interface{} `json:"data"`
|
||||
Score float64 `json:"score"`
|
||||
}
|
||||
|
||||
func (s *SearchService) parseResult(source map[string]interface{}) SearchResult {
|
||||
result := SearchResult{
|
||||
Data: source,
|
||||
}
|
||||
|
||||
if chainID, ok := source["chain_id"].(float64); ok {
|
||||
result.ChainID = int(chainID)
|
||||
}
|
||||
|
||||
// Determine type based on fields
|
||||
if _, ok := source["block_number"]; ok {
|
||||
result.Type = "block"
|
||||
} else if _, ok := source["transaction_index"]; ok {
|
||||
result.Type = "transaction"
|
||||
} else if _, ok := source["address"]; ok {
|
||||
result.Type = "address"
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// HandleSearch handles HTTP search requests
|
||||
func (s *SearchService) HandleSearch(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
query := r.URL.Query().Get("q")
|
||||
if query == "" {
|
||||
http.Error(w, "Query parameter 'q' is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var chainID *int
|
||||
if chainIDStr := r.URL.Query().Get("chain_id"); chainIDStr != "" {
|
||||
if id, err := strconv.Atoi(chainIDStr); err == nil {
|
||||
chainID = &id
|
||||
}
|
||||
}
|
||||
|
||||
limit := 50
|
||||
if limitStr := r.URL.Query().Get("limit"); limitStr != "" {
|
||||
if l, err := strconv.Atoi(limitStr); err == nil {
|
||||
limit = l
|
||||
}
|
||||
}
|
||||
|
||||
results, err := s.Search(r.Context(), query, chainID, limit)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Search failed: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response := map[string]interface{}{
|
||||
"query": query,
|
||||
"results": results,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user