initial commit

This commit is contained in:
2026-03-12 19:17:00 +01:00
commit 9030f2001e
25 changed files with 5089 additions and 0 deletions

51
internal/api/handler.go Normal file
View File

@@ -0,0 +1,51 @@
package api
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/paramah/ai_devs4/s01e03/internal/usecase"
)
// Handler handles HTTP requests
type Handler struct {
conversationUC *usecase.ConversationUseCase
}
// NewHandler creates a new API handler
func NewHandler(conversationUC *usecase.ConversationUseCase) *Handler {
return &Handler{
conversationUC: conversationUC,
}
}
// ShadowRequest represents the request for /shadow endpoint
type ShadowRequest struct {
SessionID string `json:"sessionID" binding:"required"`
Msg string `json:"msg" binding:"required"`
}
// ShadowResponse represents the response for /shadow endpoint
type ShadowResponse struct {
Msg string `json:"msg"`
}
// Shadow handles the /shadow endpoint
func (h *Handler) Shadow(c *gin.Context) {
var req ShadowRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Process message
response, err := h.conversationUC.ProcessMessage(c.Request.Context(), req.SessionID, req.Msg)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, ShadowResponse{
Msg: response,
})
}

View File

@@ -0,0 +1,77 @@
package api
import (
"bytes"
"encoding/json"
"io"
"log"
"time"
"github.com/gin-gonic/gin"
)
// VerboseLoggingMiddleware logs all requests and responses when verbose mode is enabled
func VerboseLoggingMiddleware(verbose bool) gin.HandlerFunc {
return func(c *gin.Context) {
if !verbose {
c.Next()
return
}
// Log request
var requestBody []byte
if c.Request.Body != nil {
requestBody, _ = io.ReadAll(c.Request.Body)
// Restore the body for the handler
c.Request.Body = io.NopCloser(bytes.NewBuffer(requestBody))
}
log.Printf("\n========== INCOMING REQUEST ==========")
log.Printf("Method: %s", c.Request.Method)
log.Printf("Path: %s", c.Request.URL.Path)
log.Printf("Headers: %v", c.Request.Header)
if len(requestBody) > 0 {
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, requestBody, "", " "); err == nil {
log.Printf("Body:\n%s", prettyJSON.String())
} else {
log.Printf("Body: %s", string(requestBody))
}
}
log.Printf("======================================\n")
// Create a custom response writer to capture the response
blw := &bodyLogWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer}
c.Writer = blw
start := time.Now()
c.Next()
duration := time.Since(start)
// Log response
log.Printf("\n========== OUTGOING RESPONSE ==========")
log.Printf("Status: %d", c.Writer.Status())
log.Printf("Duration: %v", duration)
log.Printf("Headers: %v", c.Writer.Header())
if blw.body.Len() > 0 {
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, blw.body.Bytes(), "", " "); err == nil {
log.Printf("Body:\n%s", prettyJSON.String())
} else {
log.Printf("Body: %s", blw.body.String())
}
}
log.Printf("======================================\n")
}
}
// bodyLogWriter is a custom response writer that captures the response body
type bodyLogWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}
func (w bodyLogWriter) Write(b []byte) (int, error) {
w.body.Write(b)
return w.ResponseWriter.Write(b)
}