✨ feat: implement API v2 with feature flag control
- Added /api/v2/greet POST endpoint with JSON request/response - Implemented ServiceV2 with Hello my friend <name>! greeting format - Added api.v2_enabled feature flag (default: false) - Extended BDD tests to cover v2 scenarios - Maintained full backward compatibility with v1 API - Added DLC_API_V2_ENABLED environment variable support - Created ADR 0010-api-v2-feature-flag.md - Updated configuration system to support API versioning
This commit is contained in:
68
pkg/greet/api_v2.go
Normal file
68
pkg/greet/api_v2.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package greet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type GreeterV2 interface {
|
||||
GreetV2(ctx context.Context, name string) string
|
||||
}
|
||||
|
||||
type ApiV2Greet interface {
|
||||
RegisterRoutes(router chi.Router)
|
||||
}
|
||||
|
||||
type apiV2GreetHandler struct {
|
||||
greeter GreeterV2
|
||||
}
|
||||
|
||||
func NewApiV2GreetHandler(greeter GreeterV2) ApiV2Greet {
|
||||
return &apiV2GreetHandler{greeter: greeter}
|
||||
}
|
||||
|
||||
func (h *apiV2GreetHandler) RegisterRoutes(router chi.Router) {
|
||||
log.Trace().Msg("Registering v2 greet routes")
|
||||
router.Post("/", h.handleGreetPost)
|
||||
log.Trace().Msg("v2 Greet routes registered")
|
||||
}
|
||||
|
||||
type greetRequest struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type greetResponse struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func (h *apiV2GreetHandler) handleGreetPost(w http.ResponseWriter, r *http.Request) {
|
||||
// Read request body
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, `{"error":"failed to read request body"}`, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse JSON
|
||||
var req greetRequest
|
||||
if err := json.Unmarshal(body, &req); err != nil {
|
||||
http.Error(w, `{"error":"invalid JSON format"}`, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Call service
|
||||
message := h.greeter.GreetV2(r.Context(), req.Name)
|
||||
|
||||
// Write response
|
||||
h.writeJSONResponse(w, message)
|
||||
}
|
||||
|
||||
func (h *apiV2GreetHandler) writeJSONResponse(w http.ResponseWriter, message string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(greetResponse{Message: message})
|
||||
}
|
||||
24
pkg/greet/greet_v2.go
Normal file
24
pkg/greet/greet_v2.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package greet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type ServiceV2 struct{}
|
||||
|
||||
func NewServiceV2() *ServiceV2 {
|
||||
return &ServiceV2{}
|
||||
}
|
||||
|
||||
// GreetV2 returns a v2 greeting message for the given name.
|
||||
// If name is empty, it defaults to "friend".
|
||||
// This is the v2 implementation that returns "Hello my friend <name>!"
|
||||
func (s *ServiceV2) GreetV2(ctx context.Context, name string) string {
|
||||
log.Trace().Ctx(ctx).Str("name", name).Msg("GreetV2 function called")
|
||||
|
||||
if name == "" {
|
||||
return "Hello my friend!"
|
||||
}
|
||||
return "Hello my friend " + name + "!"
|
||||
}
|
||||
28
pkg/greet/greet_v2_test.go
Normal file
28
pkg/greet/greet_v2_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package greet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestServiceV2_GreetV2(t *testing.T) {
|
||||
service := NewServiceV2()
|
||||
tests := []struct {
|
||||
name string
|
||||
expected string
|
||||
}{
|
||||
{"", "Hello my friend!"},
|
||||
{"John", "Hello my friend John!"},
|
||||
{"Alice", "Hello my friend Alice!"},
|
||||
{" ", "Hello my friend !"}, // spaces are not considered empty
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := service.GreetV2(context.Background(), tt.name)
|
||||
if result != tt.expected {
|
||||
t.Errorf("GreetV2(%q) = %q, want %q", tt.name, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user