A high-performance Go-based API proxy service for the Akash Network's image generation capabilities. This service provides a clean REST API interface to interact with Akash Network's decentralized AI image generation infrastructure.
- π High Performance: Built with Go and Gin framework for optimal performance
- π Asynchronous Processing: Non-blocking job submission with status polling
- π‘οΈ Concurrency Control: Built-in rate limiting to prevent resource exhaustion
- π Structured Logging: Comprehensive logging with Zap for production monitoring
- ποΈ Clean Architecture: Modular design following Go best practices
- π REST API: Simple and intuitive HTTP endpoints
- β‘ Graceful Shutdown: Proper cleanup and connection handling
- π― GPU Preference: Automatic GPU selection from available options
- Go 1.21 or higher
- Internet connection to access Akash Network
-
Clone the repository
git clone https://github.com/006lp/akashgen-api-go.git cd akashgen-api-go
-
Initialize Go modules
go mod init akashgen-api-go go mod tidy
-
Build and run
go build -o akashgen-api . ./akashgen-api
Or run directly:
go run main.go
The server will start on port 6571
by default.
Generate an AI image using Akash Network's decentralized infrastructure.
Endpoint: POST /api/generate
Request Body:
{
"prompt": "A beautiful sunset over mountains",
"negative": "blurry, low quality",
"sampler": "DPM++ 2M Karras",
"scheduler": "karras"
}
Parameters:
prompt
(required): Text description of the desired imagenegative
(optional): Text describing what to avoid in the imagesampler
(required): The sampling method to usescheduler
(required): The scheduling algorithm
Supported Samplers:
euler, euler_cfg_pp, euler_ancestral, euler_ancestral_cfg_pp, heun, heunpp2,
dpm_2, dpm_2_ancestral, lms, dpm_fast, dpm_adaptive, dpmpp_2s_ancestral,
dpmpp_2s_ancestral_cfg_pp, dpmpp_sde, dpmpp_sde_gpu, dpmpp_2m, dpmpp_2m_cfg_pp,
dpmpp_2m_sde, dpmpp_2m_sde_gpu, dpmpp_3m_sde, dpmpp_3m_sde_gpu, ddpm, lcm,
ipndm, ipndm_v, deis, ddim, uni_pc, uni_pc_bh2
Supported Schedulers:
normal, karras, exponential, sgm_uniform, simple, ddim_uniform, beta, linear_quadratic
Response:
- Success (200): Returns the generated image as binary data
- Error (400): Invalid request format
- Error (500): Generation failed or timeout
Example using curl:
curl -X POST http://localhost:6571/api/generate \
-H "Content-Type: application/json" \
-d '{
"prompt": "A serene lake with mountains in the background",
"negative": "ugly, blurry, low resolution",
"sampler": "DPM++ 2M Karras",
"scheduler": "karras"
}' \
--output generated_image.png
Check if the service is running properly.
Endpoint: GET /health
Response:
{
"status": "ok"
}
The service can be configured by modifying the constants in config/config.go
:
const (
// Timeout configurations
GenerateTimeout = 30 * time.Second // Max time for generation request
StatusTimeout = 10 * time.Second // Max time for status check
ImageFetchTimeout = 30 * time.Second // Max time for image download
PollingInterval = 1 * time.Second // How often to check job status
MaxPollingDuration = 5 * time.Minute // Max time to wait for completion
// Concurrency
MaxConcurrentRequests = 10 // Max simultaneous requests
// Server
ServerPort = ":6571" // Port to listen on
)
akashgen-api-go/
βββ main.go # Application entry point
βββ config/
β βββ config.go # Configuration constants
βββ handlers/
β βββ generate.go # HTTP request handlers
βββ models/
β βββ types.go # Data structures and types
βββ services/
β βββ akash.go # Akash Network API client
β βββ image.go # Image fetching service
βββ middleware/
β βββ logger.go # HTTP logging middleware
βββ utils/
βββ http.go # HTTP utilities
- Handlers: Process HTTP requests and responses
- Services: Business logic for interacting with external APIs
- Models: Data structures for requests and responses
- Middleware: Cross-cutting concerns like logging
- Config: Centralized configuration management
- Utils: Reusable utility functions
This project follows Go best practices with clear separation of concerns:
- Clean architecture with dependency injection
- Modular design for easy testing and maintenance
- Proper error handling and logging
- Context-based timeout management
# Build optimized binary
go build -ldflags="-w -s" -o akashgen-api .
# Or build for different platforms
GOOS=linux GOARCH=amd64 go build -o akashgen-api-linux .
Create a Dockerfile
:
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o akashgen-api .
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/akashgen-api .
EXPOSE 6571
CMD ["./akashgen-api"]
Build and run:
docker build -t akashgen-api .
docker run -p 6571:6571 akashgen-api
The service automatically selects from preferred GPU types in order:
- RTX4090
- A10
- A100
- V100-32Gi
- H100
The API provides detailed error responses:
{
"error": "Failed to generate image",
"details": "specific error description"
}
Common error scenarios:
- Invalid JSON format
- Missing required fields
- Network timeouts
- Upstream service failures
- Job execution failures
- Concurrency: Configurable request limiting prevents overload
- Timeouts: Multiple timeout layers prevent hanging requests
- Connection Pooling: HTTP client reuse for efficiency
- Memory Management: Proper cleanup of resources
- Graceful Shutdown: Clean termination handling
The service provides structured JSON logs suitable for aggregation:
{
"level": "info",
"ts": 1640995200.123456,
"msg": "HTTP Request",
"method": "POST",
"path": "/api/generate",
"status": 200,
"duration": "2.5s",
"client_ip": "192.168.1.100"
}
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- Follow Go conventions and best practices
- Add tests for new functionality
- Update documentation for API changes
- Use meaningful commit messages
- Ensure code passes
go fmt
andgo vet
# Run tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run race condition detection
go test -race ./...
- Port already in use: Change the port in
config/config.go
- Timeout errors: Increase timeout values for slower networks
- Memory issues: Reduce
MaxConcurrentRequests
if experiencing memory pressure - Connection refused: Ensure Akash Network endpoints are accessible
For development, you can enable debug logging by modifying the logger initialization in main.go
:
// Replace zap.NewProduction() with:
logger, err = zap.NewDevelopment()
This project is licensed under the AGPL v3 License - see the LICENSE file for details.
- Create an issue for bugs or feature requests
- Check Akash Network documentation for network-related questions
- Join the discussion in Akash Community
- Akash Network for providing decentralized cloud infrastructure
- Gin Web Framework for HTTP routing
- Zap for structured logging
Note: This is an unofficial proxy service for Akash Network's image generation API. Please refer to Akash Network's official documentation for the most up-to-date information about their services.