Components
This page describes each component of the Code Search system in detail.
API Server
Section titled “API Server”The API server is the central hub for all client interactions.
Responsibilities
Section titled “Responsibilities”- Serve REST API endpoints
- Handle search requests
- Manage connections and repositories
- Queue indexing jobs
- Serve the web UI
Endpoints
Section titled “Endpoints”All API endpoints are prefixed with /api/v1/:
| Endpoint | Description |
|---|---|
/api/v1/search | Code search |
/api/v1/repos | Repository management |
/api/v1/connections | Connection management |
/api/v1/jobs | Job monitoring |
Configuration
Section titled “Configuration”server: addr: ":8080" read_timeout: 15s write_timeout: 60sHealth Check
Section titled “Health Check”GET /healthReturns 200 OK when healthy.
Indexer Service
Section titled “Indexer Service”The indexer runs as a separate process and handles all repository operations.
Responsibilities
Section titled “Responsibilities”- Discover repositories from connections
- Clone and update repositories
- Build search indexes
- Process job queue
Components
Section titled “Components”Scheduler
Section titled “Scheduler”Manages periodic tasks:
- Connection syncing (repository discovery)
- Repository updates (git fetch)
- Index rebuilding
Worker Pool
Section titled “Worker Pool”Concurrent job processing:
- Configurable worker count
- Job prioritization
- Failure handling
Cloner
Section titled “Cloner”Git operations:
- Initial clone
- Incremental fetch
- Branch management
Index Builder
Section titled “Index Builder”Search index creation:
- File analysis
- Language detection
- Zoekt index building
Configuration
Section titled “Configuration”indexer: concurrency: 2 index_path: "./data/index" repos_path: "./data/repos" reindex_interval: 1h zoekt_bin: "zoekt-git-index"Zoekt is the search engine powering Code Search.
What is Zoekt?
Section titled “What is Zoekt?”Zoekt is a fast text search engine intended for source code, originally developed by Google. It uses trigram indexing for fast substring matches.
How It Works
Section titled “How It Works”- Indexing: Files are split into trigrams (3-character sequences)
- Index: Trigrams are stored in an inverted index
- Search: Query trigrams are matched against the index
- Ranking: Results are ranked by match quality
Index Structure
Section titled “Index Structure”./data/├── repos/│ ├── github.com/│ │ └── myorg/│ │ └── api/│ │ └── .git/│ └── gitlab.com/│ └── ...└── index/ └── myorg_api_v1.zoektConfiguration
Section titled “Configuration”zoekt: url: "http://localhost:6070" index_path: "./data/index" shards: 0Database (PostgreSQL / MySQL)
Section titled “Database (PostgreSQL / MySQL)”Code Search supports both PostgreSQL and MySQL for storing metadata. The database driver is auto-detected from the connection URL.
Schema
Section titled “Schema”Connections Table
CREATE TABLE connections ( id SERIAL PRIMARY KEY, name VARCHAR(255) UNIQUE NOT NULL, type VARCHAR(50) NOT NULL, url TEXT, token_encrypted BYTEA, config JSONB, status VARCHAR(50), last_synced_at TIMESTAMP, created_at TIMESTAMP DEFAULT NOW());Repositories Table
CREATE TABLE repositories ( id SERIAL PRIMARY KEY, connection_id INTEGER REFERENCES connections(id), name VARCHAR(255) NOT NULL, url TEXT NOT NULL, default_branch VARCHAR(255), status VARCHAR(50), last_indexed_at TIMESTAMP, created_at TIMESTAMP DEFAULT NOW());Jobs Table
CREATE TABLE jobs ( id SERIAL PRIMARY KEY, type VARCHAR(50) NOT NULL, status VARCHAR(50) NOT NULL, repository_id INTEGER REFERENCES repositories(id), payload JSONB, error TEXT, created_at TIMESTAMP DEFAULT NOW(), started_at TIMESTAMP, finished_at TIMESTAMP);Configuration
Section titled “Configuration”database: url: "postgres://codesearch:codesearch@localhost:5432/codesearch?sslmode=disable" max_open_conns: 25 max_idle_conns: 5 conn_max_lifetime: 5mRedis provides job queuing and distributed locking.
Job Queue
Section titled “Job Queue”Jobs are queued in Redis lists:
code-search:queue:pending # Pending jobscode-search:queue:running # Running jobsDistributed Locks
Section titled “Distributed Locks”Prevent concurrent operations on the same repository:
code-search:lock:repo:123 # Lock for repo ID 123Configuration
Section titled “Configuration”redis: addr: "localhost:6379" password: "" db: 0Web UI
Section titled “Web UI”The web UI is a Next.js application.
| Page | Description |
|---|---|
/ | Search interface |
/repos | Repository list |
/connections | Connection management |
/jobs | Job monitoring |
- Framework: Next.js 14
- Styling: TailwindCSS
- Components: shadcn/ui
- State: React Query
cd webpnpm installpnpm buildThe CLI provides command-line access to all features.
Architecture
Section titled “Architecture”cmd/cli/├── main.go├── cmd/│ ├── root.go│ ├── search.go│ ├── replace.go│ ├── repo.go│ ├── find.go│ └── config.go└── client/ └── client.goCommunication
Section titled “Communication”The CLI communicates with the API server via HTTP REST.
// Example API callfunc (c *Client) Search(ctx context.Context, query string) (*SearchResponse, error) { resp, err := c.http.Get(c.baseURL + "/api/v1/search?q=" + query) // ...}