Implement simple quote service

This commit is contained in:
Savely Krendelhoff 2025-08-22 20:05:48 +07:00
parent 8d8291e5b3
commit bd618a8193
No known key found for this signature in database
GPG key ID: F70DFD34F40238DE
3 changed files with 79 additions and 13 deletions

View file

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