A Go-based backend service for Web3 data analysis platform, providing volume statistics and yield data query functionality.
query-web3/
├── cmd/
│ ├── server/ # Server entry point
│ └── cli/ # CLI tool entry point
├── internal/
│ ├── config/ # Configuration management
│ ├── database/ # Database connection
│ ├── models/ # Data models
│ ├── schemas/ # Request/response structures
│ ├── handlers/ # HTTP handlers
│ ├── server/ # Server configuration
│ ├── cli/ # CLI commands
│ ├── redis/ # Redis cache service
│ ├── cache/ # Cache interface definitions
│ ├── logger/ # Logging system
│ └── health/ # Health checks
├── configs/ # Configuration files
├── docs/ # Documentation directory
├── .env.example # Environment variables example
└── README.md # Project documentation
go mod tidy
Create MySQL database:
CREATE DATABASE query_web3 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Copy environment variables file:
cp .env.example .env
Edit .env file, configure database and Redis connection:
# Database Configuration
QUERY_WEB3_DATABASE_HOST=localhost
QUERY_WEB3_DATABASE_PORT=3306
QUERY_WEB3_DATABASE_USER=root
QUERY_WEB3_DATABASE_PASSWORD=your_password
QUERY_WEB3_DATABASE_NAME=query_web3
# Redis Configuration
QUERY_WEB3_REDIS_HOST=localhost
QUERY_WEB3_REDIS_PORT=6379
QUERY_WEB3_REDIS_PASSWORD=
QUERY_WEB3_REDIS_DB=0
QUERY_WEB3_REDIS_POOL_SIZE=100
# Build CLI tool
go build -o query-web3 cmd/cli/main.go
# Execute database migration
./query-web3 migrate
# Start server
./query-web3 server
go run cmd/server/main.go
After server starts, access:
POST /api/v1/vol-txns
Request body:
{
"from_date": "2024-01-01",
"to_date": "2024-01-31",
"chain": "ethereum",
"cycle": "daily"
}
POST /api/v1/yield
Request body:
{
"date": "2024-01-01",
"chain": "ethereum",
"asset_type": "defi",
"return_type": "lending",
"token": "USDC",
"page": 1,
"page_size": 10
}
/api/v1/chains - Get blockchain list/api/v1/cycles - Get statistics cycles list/api/v1/asset-types - Get asset types list/api/v1/return-types - Get return types list/api/v1/tokens - Get tokens list (with chain and asset type filtering)# Build server
go build -o bin/server cmd/server/main.go
# Build CLI tool
go build -o bin/query-web3 cmd/cli/main.go
go test ./...
go fmt ./...
FROM golang:1.21-alpine
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o main cmd/server/main.go
EXPOSE 8000
CMD ["./main"]
All configurations can be overridden by environment variables:
export QUERY_WEB3_DATABASE_HOST=mysql.example.com
export QUERY_WEB3_DATABASE_PORT=3306
export QUERY_WEB3_DATABASE_USER=app_user
export QUERY_WEB3_DATABASE_PASSWORD=secure_password
export QUERY_WEB3_DATABASE_NAME=query_web3
export QUERY_WEB3_SERVER_PORT=8080
QueryWeb3 project integrates Redis cache system, significantly improving system performance and response speed.
| Scenario | Without Cache QPS | With Cache QPS | Improvement |
|---|---|---|---|
| Metadata Queries | 200 | 5000 | 25x |
| Transaction Statistics | 50 | 1000 | 20x |
| Yield Queries | 30 | 800 | 26x |
For more detailed information about Redis performance optimization, please refer to Redis Performance Guide.
MIT License