Implement quote service #2

Merged
krendelhoff merged 2 commits from phase-2-quote into master 2025-08-22 16:06:32 +03:00
3 changed files with 79 additions and 13 deletions
Showing only changes of commit bd618a8193 - Show all commits

View file

@ -38,15 +38,23 @@
- [X] Test edge cases (expired challenges, invalid HMAC, wrong difficulty)
- [X] Performance tests for concurrent challenge operations
## Phase 2: Basic Server Architecture
## Phase 2: Quote Handler
**Goal**: Simple quote service with public API using resty
- [X] Add resty dependency to go.mod
- [X] Create quote service package
- [X] Implement quote fetching with HTTP client
- [X] Add basic error handling
## Phase 3: Basic Server Architecture
- [ ] Set up dependency injection framework (wire/dig)
- [ ] Create core interfaces and contracts
- [ ] Set up structured logging (zerolog/logrus)
- [ ] Set up metrics collection (prometheus)
- [ ] Create configuration management
- [ ] Integrate PoW package into server architecture
- [ ] Integrate PoW and quote packages into server architecture
## Phase 3: Quote Management System
## Phase 4: Quote Management System
- [ ] Define quote storage interface
- [ ] Implement in-memory quote repository (fake)
- [ ] Create quote selection service (random)
@ -54,7 +62,7 @@
- [ ] Add quote validation and sanitization
- [ ] Write unit tests for quote management
## Phase 4: TCP Protocol Implementation
## Phase 5: TCP Protocol Implementation
- [ ] Implement binary message protocol codec
- [ ] Create protocol message types and structures
- [ ] Implement connection handler with proper error handling
@ -63,7 +71,7 @@
- [ ] Implement connection lifecycle management
- [ ] Write unit tests for protocol components
## Phase 5: Server Core & Request Handling
## Phase 6: Server Core & Request Handling
- [ ] Implement TCP server with connection pooling
- [ ] Create request router and handler dispatcher
- [ ] Add connection timeout and lifecycle management
@ -72,7 +80,7 @@
- [ ] Create health check endpoints
- [ ] Write integration tests for server core
## Phase 6: DDOS Protection & Rate Limiting
## Phase 7: DDOS Protection & Rate Limiting
- [ ] Implement IP-based connection limiting
- [ ] Create rate limiting service with time windows
- [ ] Add automatic difficulty adjustment based on load
@ -81,7 +89,7 @@
- [ ] Add monitoring for attack detection
- [ ] Write tests for protection mechanisms
## Phase 7: Observability & Monitoring
## Phase 8: Observability & Monitoring
- [ ] Add structured logging throughout application
- [ ] Implement metrics for key performance indicators:
- [ ] Active connections count
@ -93,7 +101,7 @@
- [ ] Add error categorization and reporting
- [ ] Implement health check endpoints
## Phase 8: Configuration & Environment Setup
## Phase 9: Configuration & Environment Setup
- [ ] Create configuration structure with validation
- [ ] Support environment variables and config files
- [ ] Add configuration for different environments (dev/prod)
@ -101,7 +109,7 @@
- [ ] Create deployment configuration templates
- [ ] Add configuration validation and defaults
## Phase 9: Client Implementation
## Phase 10: Client Implementation
- [ ] Create client application structure
- [ ] Implement PoW solver algorithm
- [ ] Create client-side protocol implementation
@ -111,7 +119,7 @@
- [ ] Add client metrics and logging
- [ ] Write client unit and integration tests
## Phase 10: Docker & Deployment
## Phase 11: Docker & Deployment
- [ ] Create multi-stage Dockerfile for server
- [ ] Create Dockerfile for client
- [ ] Create docker-compose.yml for local development
@ -120,7 +128,7 @@
- [ ] Add environment-specific configurations
- [ ] Create deployment documentation
## Phase 11: Testing & Quality Assurance
## Phase 12: Testing & Quality Assurance
- [ ] Write comprehensive unit tests (>80% coverage):
- [ ] PoW algorithm tests
- [ ] Protocol handler tests
@ -135,7 +143,7 @@
- [ ] Add benchmark tests for performance validation
- [ ] Create stress testing scenarios
## Phase 12: Documentation & Final Polish
## Phase 13: Documentation & Final Polish
- [ ] Write comprehensive README with setup instructions
- [ ] Create API documentation for all interfaces
- [ ] Add inline code documentation
@ -144,7 +152,7 @@
- [ ] Add performance tuning recommendations
- [ ] Create monitoring and alerting guide
## Phase 13: Production Readiness Checklist
## Phase 14: Production Readiness Checklist
- [ ] Security audit of all components
- [ ] Performance benchmarking and optimization
- [ ] Memory leak detection and prevention

View file

@ -0,0 +1,46 @@
package quotes
import (
"context"
"fmt"
"github.com/go-resty/resty/v2"
)
type HTTPService struct {
client *resty.Client
}
func NewHTTPService() *HTTPService {
return &HTTPService{
client: resty.New(),
}
}
func (s *HTTPService) GetRandomQuote(ctx context.Context) (*Quote, error) {
var response []struct {
Q string `json:"q"`
A string `json:"a"`
}
resp, err := s.client.R().
SetContext(ctx).
SetResult(&response).
Get("https://zenquotes.io/api/random")
if err != nil {
return nil, fmt.Errorf("failed to fetch quote: %w", err)
}
if resp.IsError() {
return nil, fmt.Errorf("API error: %s", resp.Status())
}
if len(response) == 0 {
return nil, fmt.Errorf("no quotes returned from API")
}
return &Quote{
Text: response[0].Q,
Author: response[0].A,
}, nil
}

12
internal/quotes/types.go Normal file
View file

@ -0,0 +1,12 @@
package quotes
import "context"
type Quote struct {
Text string `json:"text"`
Author string `json:"author"`
}
type Service interface {
GetRandomQuote(ctx context.Context) (*Quote, error)
}