Skip to content

Commit 686fbe1

Browse files
feat(database): support SeedFile import for memory database (#128)
<!-- Provide a brief summary of your changes --> Add SeedFile import support for memory database to ensure consistent seed data loading across both MongoDB and memory modes. ## Motivation and Context <!-- Why is this change needed? What problem does it solve? --> Previously, SeedFile import was only available when using MongoDB. After memory database support was added, it lacked this feature, leading to inconsistent behavior and missing seed data during development or testing. This PR solves the problem by enabling the application to load SeedFile data even when running with a memory database, ensuring consistency and improving the developer/testing experience. ## How Has This Been Tested? <!-- Have you tested this in a real application? Which scenarios were tested? --> ```bash $ export MCP_REGISTRY_DATABASE_TYPE="memory" $ ./scripts/test_endpoints.sh ================================================== MCP Registry Endpoint Test Script ================================================== This script expects the MCP Registry server to be running locally. Please ensure the server is started using one of the following methods: • Docker Compose: docker compose up • Direct execution: go run cmd/registry/main.go • Built binary: ./build/registry ================================================== Testing health endpoint: http://localhost:8080/v0/health Status Code: 200 Response: { "status": "ok", "github_client_id": "" } Health check successful ------------------------------------- Testing servers endpoint: http://localhost:8080/v0/servers Status Code: 200 Response Summary: Total registries: 30 servers Names: io.github.appcypher/awesome-mcp-servers io.github.kenjihikmatullah/productboard-mcp io.github.kyrietangsheng/mcp-server-nationalparks io.github.gongrzhe/redis-mcp-server io.github.zubeidhendricks/youtube-mcp-server io.github.zzaebok/mcp-wikidata io.github.ihor-sokoliuk/mcp-searxng io.github.sv/mcp-paradex-py io.github.felores/airtable-mcp io.github.eqtylab/mcp-guardian io.github.co-browser/attestable-mcp-server io.github.ubie-oss/mcp-vertexai-search io.github.kukapay/crypto-indicators-mcp io.github.pinecone-io/assistant-mcp io.github.metricool/mcp-metricool io.github.r-huijts/strava-mcp io.github.googleapis/genai-toolbox io.github.wilsonchenghy/shadertoy-mcp io.github.klavis-ai/klavis io.github.fireproof-storage/mcp-database-server io.github.xxxbrian/mcp-rquest io.github.geropl/linear-mcp-go io.github.amornpan/py-mcp-mssql io.github.chaindead/telegram-mcp io.github.bazinga012/mcp_code_executor io.github.lharries/whatsapp-mcp io.github.mektigboy/server-hyperliquid io.github.omar-v2/mcp-ical io.github.open-strategy-partners/osp_marketing_tools io.github.smn2gnt/mcp-salesforce Pagination Metadata: { "next_cursor": "0e45cb5a-5b45-47f3-8b5e-12bdf88d176a", "count": 30 } servers Details: { "servers": [ { "id": "0007544a-3948-4934-866b-b4a96fe53b55", "name": "io.github.appcypher/awesome-mcp-servers", "description": "Awesome MCP Servers - A curated list of Model Context Protocol servers", "repository": { "url": "https://github.com/appcypher/awesome-mcp-servers", "source": "github", "id": "895801050" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:16:40Z", "is_latest": true } }, { "id": "00613acb-73e2-4f93-8b96-296df17316c8", "name": "io.github.kenjihikmatullah/productboard-mcp", "description": "Integrate the Productboard API into agentic workflows via MCP", "repository": { "url": "https://github.com/kenjihikmatullah/productboard-mcp", "source": "github", "id": "939104710" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:12:44Z", "is_latest": true } }, { "id": "010904ec-6e39-4bdc-878a-75a6e79d0500", "name": "io.github.kyrietangsheng/mcp-server-nationalparks", "description": "", "repository": { "url": "https://github.com/KyrieTangSheng/mcp-server-nationalparks", "source": "github", "id": "951713109" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:11:05Z", "is_latest": true } }, { "id": "01129bff-3d65-4e3d-8e82-6f2f269f818c", "name": "io.github.gongrzhe/redis-mcp-server", "description": "A Redis MCP server (pushed to https://github.com/modelcontextprotocol/servers/tree/main/src/redis) implementation for interacting with Redis databases. This server enables LLMs to interact with Redis key-value stores through a set of standardized tools.", "repository": { "url": "https://github.com/GongRzhe/REDIS-MCP-Server", "source": "github", "id": "907849235" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:13:21Z", "is_latest": true } }, { "id": "0278fd92-a009-4a47-a6df-5f77db37e6ab", "name": "io.github.zubeidhendricks/youtube-mcp-server", "description": "MCP Server for YouTube API, enabling video management, Shorts creation, and advanced analytics", "repository": { "url": "https://github.com/ZubeidHendricks/youtube-mcp-server", "source": "github", "id": "909607678" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:16:14Z", "is_latest": true } }, { "id": "02b79b89-cce8-4401-aa1f-255b01587266", "name": "io.github.zzaebok/mcp-wikidata", "description": "A server implementation for Wikidata API using the Model Context Protocol (MCP).", "repository": { "url": "https://github.com/zzaebok/mcp-wikidata", "source": "github", "id": "959274715" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:15:49Z", "is_latest": true } }, { "id": "02cdb980-1dcd-4345-a8d1-d3f3f522d04b", "name": "io.github.ihor-sokoliuk/mcp-searxng", "description": "MCP Server for SearXNG", "repository": { "url": "https://github.com/ihor-sokoliuk/mcp-searxng", "source": "github", "id": "907172461" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:13:48Z", "is_latest": true } }, { "id": "038a3024-c319-4a07-bd7f-9cf699cfebf1", "name": "io.github.sv/mcp-paradex-py", "description": "Connect AI agents to the Paradex trading platform. Retrieve market data, manage accounts, and execute trades seamlessly. Enhance your trading experience with automated tools and real-time insights.", "repository": { "url": "https://github.com/sv/mcp-paradex-py", "source": "github", "id": "944258239" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:12:24Z", "is_latest": true } }, { "id": "04216a87-0760-4f25-b809-42ffbfbb8962", "name": "io.github.felores/airtable-mcp", "description": "Search, create and update Airtable bases, tables, fields, and records using Claude Desktop and MCP (Model Context Protocol) clients", "repository": { "url": "https://github.com/felores/airtable-mcp", "source": "github", "id": "904238387" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:02:48Z", "is_latest": true } }, { "id": "04b4489b-db48-4aaa-98f6-036bcf5fd483", "name": "io.github.eqtylab/mcp-guardian", "description": "Manage / Proxy / Secure your MCP Servers", "repository": { "url": "https://github.com/eqtylab/mcp-guardian", "source": "github", "id": "926222936" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:16:58Z", "is_latest": true } }, { "id": "05ca6ccc-d6e1-47ab-ab8d-25949db9ada7", "name": "io.github.co-browser/attestable-mcp-server", "description": "Verify that any MCP server is running the intended and untampered code via hardware attestation.", "repository": { "url": "https://github.com/co-browser/attestable-mcp-server", "source": "github", "id": "955641588" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:03:19Z", "is_latest": true } }, { "id": "07ccf835-680a-42b4-bf0b-db28367519ee", "name": "io.github.ubie-oss/mcp-vertexai-search", "description": "A MCP server for Vertex AI Search", "repository": { "url": "https://github.com/ubie-oss/mcp-vertexai-search", "source": "github", "id": "934580342" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:07:44Z", "is_latest": true } }, { "id": "0852caec-a861-4f9c-9e94-a7c8b67a1641", "name": "io.github.kukapay/crypto-indicators-mcp", "description": "An MCP server providing a range of cryptocurrency technical analysis indicators and strategies.", "repository": { "url": "https://github.com/kukapay/crypto-indicators-mcp", "source": "github", "id": "953321792" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:05:01Z", "is_latest": true } }, { "id": "08668c6a-c2f6-47b0-a4e0-75a8ee9bcf10", "name": "io.github.pinecone-io/assistant-mcp", "description": "Pinecone Assistant MCP server", "repository": { "url": "https://github.com/pinecone-io/assistant-mcp", "source": "github", "id": "940446625" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:01:06Z", "is_latest": true } }, { "id": "091ef217-8e6d-4e06-8cb9-bc5ffe5467b1", "name": "io.github.metricool/mcp-metricool", "description": "This is a Multi-Agent Collaboration Protocol (MCP) server for interacting with the Metricool API. It allows AI agents to access and analyze social media metrics and campaign data from your Metricool account.", "repository": { "url": "https://github.com/metricool/mcp-metricool", "source": "github", "id": "967888435" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:10:11Z", "is_latest": true } }, { "id": "09a2ecf8-1774-490e-8542-2b25a6445a58", "name": "io.github.r-huijts/strava-mcp", "description": "A Model Context Protocol (MCP) server that connects to Strava API, providing tools to access Strava data through LLMs", "repository": { "url": "https://github.com/r-huijts/strava-mcp", "source": "github", "id": "961429605" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:14:17Z", "is_latest": true } }, { "id": "09f19715-3a0c-4900-932b-1ff0035aff11", "name": "io.github.googleapis/genai-toolbox", "description": "MCP Toolbox for Databases is an open source MCP server for databases.", "repository": { "url": "https://github.com/googleapis/genai-toolbox", "source": "github", "id": "812044182" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:00:18Z", "is_latest": true } }, { "id": "0b245c5f-ba2b-4e8d-b9db-26681b0fa2e3", "name": "io.github.wilsonchenghy/shadertoy-mcp", "description": "", "repository": { "url": "https://github.com/wilsonchenghy/ShaderToy-MCP", "source": "github", "id": "958998300" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:14:23Z", "is_latest": true } }, { "id": "0bc33e11-876e-4b0f-9c72-75c126d89978", "name": "io.github.klavis-ai/klavis", "description": "Klavis AI (YC X25): Open Source MCP integration for AI applications", "repository": { "url": "https://github.com/Klavis-AI/klavis", "source": "github", "id": "965974441" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T18:59:59Z", "is_latest": true } }, { "id": "0bee6074-d16a-4482-a315-5b8f0f1abcf1", "name": "io.github.fireproof-storage/mcp-database-server", "description": "Store and load JSON documents from LLM tool use", "repository": { "url": "https://github.com/fireproof-storage/mcp-database-server", "source": "github", "id": "904432283" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T18:58:57Z", "is_latest": true } }, { "id": "0c43f128-7589-4ce6-9382-5399ba24bebd", "name": "io.github.xxxbrian/mcp-rquest", "description": "A MCP server providing realistic browser-like HTTP request capabilities with accurate TLS/JA3/JA4 fingerprints for bypassing anti-bot measures. It also supports converting PDF and HTML documents to Markdown for easier processing by LLMs.", "repository": { "url": "https://github.com/xxxbrian/mcp-rquest", "source": "github", "id": "950863558" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:13:30Z", "is_latest": true } }, { "id": "0c8e4aae-53ed-4acf-87d9-19d44ac4afd1", "name": "io.github.geropl/linear-mcp-go", "description": "linear MCP server based on mcp-go", "repository": { "url": "https://github.com/geropl/linear-mcp-go", "source": "github", "id": "940084523" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:09:19Z", "is_latest": true } }, { "id": "0c9a70a7-7523-42b7-a951-b4c4b8077710", "name": "io.github.amornpan/py-mcp-mssql", "description": "py-mcp-mssql", "repository": { "url": "https://github.com/amornpan/py-mcp-mssql", "source": "github", "id": "922607489" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:10:17Z", "is_latest": true } }, { "id": "0cd13ce0-2f67-414a-afb1-4e11dd3e4cba", "name": "io.github.chaindead/telegram-mcp", "description": "Telegram MCP for managing dialogs, messages, drafts, read statuses, and more.", "repository": { "url": "https://github.com/chaindead/telegram-mcp", "source": "github", "id": "958652024" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:14:34Z", "is_latest": true } }, { "id": "0cdfed38-6495-42a3-a837-3788f35aadd3", "name": "io.github.bazinga012/mcp_code_executor", "description": "The MCP Code Executor is an MCP server that allows LLMs to execute Python code within a specified Conda environment.", "repository": { "url": "https://github.com/bazinga012/mcp_code_executor", "source": "github", "id": "928420039" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:04:37Z", "is_latest": true } }, { "id": "0d2cbcc1-4b34-4b2b-af66-595d8fb5e78a", "name": "io.github.lharries/whatsapp-mcp", "description": "WhatsApp MCP server", "repository": { "url": "https://github.com/lharries/whatsapp-mcp", "source": "github", "id": "957053334" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:15:45Z", "is_latest": true } }, { "id": "0d57edf7-3fbb-42b4-aa9e-b255e7d6f8fa", "name": "io.github.mektigboy/server-hyperliquid", "description": "MCP Hyperliquid (https://app.hyperliquid.xyz) server", "repository": { "url": "https://github.com/mektigboy/server-hyperliquid", "source": "github", "id": "943734921" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:08:12Z", "is_latest": true } }, { "id": "0d7bc864-fdf3-4afd-8b44-48468b12d550", "name": "io.github.omar-v2/mcp-ical", "description": "A Model Context Protocol Server that allows you to interact with your MacOS Calendar through natural language.", "repository": { "url": "https://github.com/Omar-V2/mcp-ical", "source": "github", "id": "930007673" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:03:05Z", "is_latest": true } }, { "id": "0d8b287c-6023-4809-a5ba-963c5612efa2", "name": "io.github.open-strategy-partners/osp_marketing_tools", "description": "A Model Context Protocol (MCP) server that empowers LLMs to use some of Open Srategy Partners' core writing and product marketing techniques.", "repository": { "url": "https://github.com/open-strategy-partners/osp_marketing_tools", "source": "github", "id": "917730483" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:12:14Z", "is_latest": true } }, { "id": "0e45cb5a-5b45-47f3-8b5e-12bdf88d176a", "name": "io.github.smn2gnt/mcp-salesforce", "description": "MCP Salesforce connector", "repository": { "url": "https://github.com/smn2gnt/MCP-Salesforce", "source": "github", "id": "904538810" }, "version_detail": { "version": "0.0.1-seed", "release_date": "2025-05-16T19:13:41Z", "is_latest": true } } ], "metadata": { "next_cursor": "0e45cb5a-5b45-47f3-8b5e-12bdf88d176a", "count": 30 } } servers request successful ------------------------------------- Testing ping endpoint: http://localhost:8080/v0/ping Status Code: 200 Response: { "status": "ok", "version": "dev" } Ping successful ------------------------------------- All tests passed successfully! ``` ## Breaking Changes <!-- Will users need to update their code or configurations? --> No ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [x] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [x] My code follows the repository's style guidelines - [x] New and existing tests pass locally - [x] I have added appropriate error handling - [ ] I have added or updated documentation as needed ## Additional context <!-- Add any other context, implementation notes, or design decisions --> Close #127
1 parent 6b22cf0 commit 686fbe1

File tree

5 files changed

+129
-87
lines changed

5 files changed

+129
-87
lines changed

cmd/registry/main.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,56 +34,64 @@ func main() {
3434

3535
log.Printf("Starting MCP Registry Application v%s (commit: %s)", Version, GitCommit)
3636

37+
var (
38+
registryService service.RegistryService
39+
db database.Database
40+
err error
41+
)
42+
3743
// Initialize configuration
3844
cfg := config.NewConfig()
3945

4046
// Initialize services based on environment
41-
var registryService service.RegistryService
42-
4347
switch cfg.DatabaseType {
4448
case config.DatabaseTypeMemory:
45-
memoryDB := database.NewMemoryDB(map[string]*model.Server{})
46-
registryService = service.NewRegistryServiceWithDB(memoryDB)
49+
db = database.NewMemoryDB(map[string]*model.Server{})
50+
registryService = service.NewRegistryServiceWithDB(db)
4751
case config.DatabaseTypeMongoDB:
4852
// Use MongoDB for real registry service in production/other environments
4953
// Create a context with timeout for MongoDB connection
5054
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
5155
defer cancel()
5256

5357
// Connect to MongoDB
54-
mongoDB, err := database.NewMongoDB(ctx, cfg.DatabaseURL, cfg.DatabaseName, cfg.CollectionName)
58+
db, err = database.NewMongoDB(ctx, cfg.DatabaseURL, cfg.DatabaseName, cfg.CollectionName)
5559
if err != nil {
5660
log.Printf("Failed to connect to MongoDB: %v", err)
5761
return
5862
}
5963

6064
// Create registry service with MongoDB
61-
registryService = service.NewRegistryServiceWithDB(mongoDB)
65+
registryService = service.NewRegistryServiceWithDB(db)
6266
log.Printf("MongoDB database name: %s", cfg.DatabaseName)
6367
log.Printf("MongoDB collection name: %s", cfg.CollectionName)
6468

6569
// Store the MongoDB instance for later cleanup
6670
defer func() {
67-
if err := mongoDB.Close(); err != nil {
71+
if err := db.Close(); err != nil {
6872
log.Printf("Error closing MongoDB connection: %v", err)
6973
} else {
7074
log.Println("MongoDB connection closed successfully")
7175
}
7276
}()
73-
74-
if cfg.SeedImport {
75-
log.Println("Importing data...")
76-
if err := database.ImportSeedFile(mongoDB, cfg.SeedFilePath); err != nil {
77-
log.Printf("Failed to import seed file: %v", err)
78-
} else {
79-
log.Println("Data import completed successfully")
80-
}
81-
}
8277
default:
8378
log.Printf("Invalid database type: %s; supported types: %s, %s", cfg.DatabaseType, config.DatabaseTypeMemory, config.DatabaseTypeMongoDB)
8479
return
8580
}
8681

82+
// Import seed data if requested (works for both memory and MongoDB)
83+
if cfg.SeedImport {
84+
log.Println("Importing data...")
85+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
86+
defer cancel()
87+
88+
if err := db.ImportSeed(ctx, cfg.SeedFilePath); err != nil {
89+
log.Printf("Failed to import seed file: %v", err)
90+
} else {
91+
log.Println("Data import completed successfully")
92+
}
93+
}
94+
8795
// Initialize authentication services
8896
authService := auth.NewAuthService(cfg)
8997

internal/database/database.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type Database interface {
2424
GetByID(ctx context.Context, id string) (*model.ServerDetail, error)
2525
// Publish adds a new ServerDetail to the database
2626
Publish(ctx context.Context, serverDetail *model.ServerDetail) error
27+
// ImportSeed imports initial data from a seed file
28+
ImportSeed(ctx context.Context, seedFilePath string) error
2729
// Close closes the database connection
2830
Close() error
2931
}

internal/database/import.go

Lines changed: 8 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,28 @@
11
package database
22

33
import (
4-
"context"
54
"encoding/json"
65
"fmt"
76
"log"
87
"os"
98
"path/filepath"
10-
"time"
119

1210
"github.com/modelcontextprotocol/registry/internal/model"
13-
"go.mongodb.org/mongo-driver/bson"
14-
"go.mongodb.org/mongo-driver/mongo"
15-
"go.mongodb.org/mongo-driver/mongo/options"
1611
)
1712

18-
// ImportSeedFile populates the MongoDB database with initial data from a seed file.
19-
func ImportSeedFile(mongo *MongoDB, seedFilePath string) error {
13+
// ReadSeedFile reads and parses the seed.json file - exported for use by all database implementations
14+
func ReadSeedFile(path string) ([]model.ServerDetail, error) {
15+
log.Printf("Reading seed file from %s", path)
16+
2017
// Set default seed file path if not provided
21-
if seedFilePath == "" {
18+
if path == "" {
2219
// Try to find the seed.json in the data directory
23-
seedFilePath = filepath.Join("data", "seed.json")
24-
if _, err := os.Stat(seedFilePath); os.IsNotExist(err) {
25-
return fmt.Errorf("seed file not found at %s", seedFilePath)
20+
path = filepath.Join("data", "seed.json")
21+
if _, err := os.Stat(path); os.IsNotExist(err) {
22+
return nil, fmt.Errorf("seed file not found at %s", path)
2623
}
2724
}
2825

29-
// Create a context with timeout for the database operations
30-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
31-
defer cancel()
32-
33-
// Read the seed file
34-
seedData, err := readSeedFile(seedFilePath)
35-
if err != nil {
36-
return fmt.Errorf("failed to read seed file: %w", err)
37-
}
38-
39-
collection := mongo.collection
40-
importData(ctx, collection, seedData)
41-
return nil
42-
}
43-
44-
// readSeedFile reads and parses the seed.json file
45-
func readSeedFile(path string) ([]model.ServerDetail, error) {
46-
log.Printf("Reading seed file from %s", path)
47-
4826
// Read the file content
4927
fileContent, err := os.ReadFile(path)
5028
if err != nil {
@@ -64,44 +42,3 @@ func readSeedFile(path string) ([]model.ServerDetail, error) {
6442
log.Printf("Found %d server entries in seed file", len(servers))
6543
return servers, nil
6644
}
67-
68-
// importData imports the seed data into MongoDB
69-
func importData(ctx context.Context, collection *mongo.Collection, servers []model.ServerDetail) {
70-
log.Printf("Importing %d servers into collection %s", len(servers), collection.Name())
71-
72-
for i, server := range servers {
73-
if server.ID == "" || server.Name == "" {
74-
log.Printf("Skipping server %d: ID or Name is empty", i+1)
75-
continue
76-
}
77-
// Create filter based on server ID
78-
filter := bson.M{"id": server.ID}
79-
80-
if server.VersionDetail.Version == "" {
81-
server.VersionDetail.Version = "0.0.1-seed"
82-
server.VersionDetail.ReleaseDate = time.Now().Format(time.RFC3339)
83-
server.VersionDetail.IsLatest = true
84-
}
85-
// Create update document
86-
update := bson.M{"$set": server}
87-
88-
// Use upsert to create if not exists or update if exists
89-
opts := options.Update().SetUpsert(true)
90-
result, err := collection.UpdateOne(ctx, filter, update, opts)
91-
if err != nil {
92-
log.Printf("Error importing server %s: %v", server.ID, err)
93-
continue
94-
}
95-
96-
switch {
97-
case result.UpsertedCount > 0:
98-
log.Printf("[%d/%d] Created server: %s", i+1, len(servers), server.Name)
99-
case result.ModifiedCount > 0:
100-
log.Printf("[%d/%d] Updated server: %s", i+1, len(servers), server.Name)
101-
default:
102-
log.Printf("[%d/%d] Server already up to date: %s", i+1, len(servers), server.Name)
103-
}
104-
}
105-
106-
log.Println("Import completed successfully")
107-
}

internal/database/memory.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package database
22

33
import (
44
"context"
5+
"fmt"
6+
"log"
57
"sort"
68
"strconv"
79
"strings"
@@ -249,6 +251,47 @@ func (db *MemoryDB) Publish(ctx context.Context, serverDetail *model.ServerDetai
249251
return nil
250252
}
251253

254+
// ImportSeed imports initial data from a seed file into memory database
255+
func (db *MemoryDB) ImportSeed(ctx context.Context, seedFilePath string) error {
256+
if ctx.Err() != nil {
257+
return ctx.Err()
258+
}
259+
260+
// Read the seed file
261+
seedData, err := ReadSeedFile(seedFilePath)
262+
if err != nil {
263+
return fmt.Errorf("failed to read seed file: %w", err)
264+
}
265+
266+
log.Printf("Importing %d servers into memory database", len(seedData))
267+
268+
db.mu.Lock()
269+
defer db.mu.Unlock()
270+
271+
for i, server := range seedData {
272+
if server.ID == "" || server.Name == "" {
273+
log.Printf("Skipping server %d: ID or Name is empty", i+1)
274+
continue
275+
}
276+
277+
// Set default version information if missing
278+
if server.VersionDetail.Version == "" {
279+
server.VersionDetail.Version = "0.0.1-seed"
280+
server.VersionDetail.ReleaseDate = time.Now().Format(time.RFC3339)
281+
server.VersionDetail.IsLatest = true
282+
}
283+
284+
// Store a copy of the server detail
285+
serverDetailCopy := server
286+
db.entries[server.ID] = &serverDetailCopy
287+
288+
log.Printf("[%d/%d] Imported server: %s", i+1, len(seedData), server.Name)
289+
}
290+
291+
log.Println("Memory database import completed successfully")
292+
return nil
293+
}
294+
252295
// Close closes the database connection
253296
// For an in-memory database, this is a no-op
254297
func (db *MemoryDB) Close() error {

internal/database/mongo.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,58 @@ func (db *MongoDB) Publish(ctx context.Context, serverDetail *model.ServerDetail
232232
return nil
233233
}
234234

235+
// ImportSeed imports initial data from a seed file into MongoDB
236+
func (db *MongoDB) ImportSeed(ctx context.Context, seedFilePath string) error {
237+
// Read the seed file
238+
servers, err := ReadSeedFile(seedFilePath)
239+
if err != nil {
240+
return fmt.Errorf("failed to read seed file: %w", err)
241+
}
242+
243+
collection := db.collection
244+
245+
log.Printf("Importing %d servers into collection %s", len(servers), collection.Name())
246+
247+
for i, server := range servers {
248+
if server.ID == "" || server.Name == "" {
249+
log.Printf("Skipping server %d: ID or Name is empty", i+1)
250+
continue
251+
}
252+
253+
if server.VersionDetail.Version == "" {
254+
server.VersionDetail.Version = "0.0.1-seed"
255+
server.VersionDetail.ReleaseDate = time.Now().Format(time.RFC3339)
256+
server.VersionDetail.IsLatest = true
257+
}
258+
259+
// Create filter based on server ID
260+
filter := bson.M{"id": server.ID}
261+
262+
// Create update document
263+
update := bson.M{"$set": server}
264+
265+
// Use upsert to create if not exists or update if exists
266+
opts := options.Update().SetUpsert(true)
267+
result, err := collection.UpdateOne(ctx, filter, update, opts)
268+
if err != nil {
269+
log.Printf("Error importing server %s: %v", server.ID, err)
270+
continue
271+
}
272+
273+
switch {
274+
case result.UpsertedCount > 0:
275+
log.Printf("[%d/%d] Created server: %s", i+1, len(servers), server.Name)
276+
case result.ModifiedCount > 0:
277+
log.Printf("[%d/%d] Updated server: %s", i+1, len(servers), server.Name)
278+
default:
279+
log.Printf("[%d/%d] Server already up to date: %s", i+1, len(servers), server.Name)
280+
}
281+
}
282+
283+
log.Println("MongoDB database import completed successfully")
284+
return nil
285+
}
286+
235287
// Close closes the database connection
236288
func (db *MongoDB) Close() error {
237289
return db.client.Disconnect(context.Background())

0 commit comments

Comments
 (0)