This commit is contained in:
79
internal/delivery/http/handlers/auth_handler.go
Normal file
79
internal/delivery/http/handlers/auth_handler.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"counter/internal/domain/entities"
|
||||
"counter/internal/usecase/auth"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// AuthHandler handles authentication HTTP requests
|
||||
type AuthHandler struct {
|
||||
authService *auth.AuthService
|
||||
}
|
||||
|
||||
// NewAuthHandler creates a new authentication handler
|
||||
func NewAuthHandler(authService *auth.AuthService) *AuthHandler {
|
||||
return &AuthHandler{
|
||||
authService: authService,
|
||||
}
|
||||
}
|
||||
|
||||
// Register handles user registration
|
||||
func (h *AuthHandler) Register(c *gin.Context) {
|
||||
var req auth.RegisterRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
response, err := h.authService.Register(c.Request.Context(), &req)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case entities.ErrUserAlreadyExists:
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "Username or email already exists"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create user"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, response)
|
||||
}
|
||||
|
||||
// Login handles user login
|
||||
func (h *AuthHandler) Login(c *gin.Context) {
|
||||
var req auth.LoginRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
response, err := h.authService.Login(c.Request.Context(), &req)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case entities.ErrInvalidCredentials:
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Authentication failed"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// GetMe returns the current authenticated user
|
||||
func (h *AuthHandler) GetMe(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
|
||||
user, err := h.authService.GetCurrentUser(c.Request.Context(), userID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get user"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, user)
|
||||
}
|
||||
228
internal/delivery/http/handlers/counter_handler.go
Normal file
228
internal/delivery/http/handlers/counter_handler.go
Normal file
@@ -0,0 +1,228 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"counter/internal/domain/entities"
|
||||
"counter/internal/usecase/counter"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// CounterHandler handles counter HTTP requests
|
||||
type CounterHandler struct {
|
||||
counterService *counter.CounterService
|
||||
}
|
||||
|
||||
// NewCounterHandler creates a new counter handler
|
||||
func NewCounterHandler(counterService *counter.CounterService) *CounterHandler {
|
||||
return &CounterHandler{
|
||||
counterService: counterService,
|
||||
}
|
||||
}
|
||||
|
||||
// Create handles counter creation
|
||||
func (h *CounterHandler) Create(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
|
||||
var req counter.CreateCounterRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
counter, err := h.counterService.Create(c.Request.Context(), userID, &req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create counter"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, counter)
|
||||
}
|
||||
|
||||
// Get handles retrieving a specific counter
|
||||
func (h *CounterHandler) Get(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
counterID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
|
||||
return
|
||||
}
|
||||
|
||||
counter, err := h.counterService.Get(c.Request.Context(), counterID, userID)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case entities.ErrCounterNotFound:
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch counter"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, counter)
|
||||
}
|
||||
|
||||
// List handles retrieving all counters for a user
|
||||
func (h *CounterHandler) List(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
search := c.Query("search")
|
||||
|
||||
counters, err := h.counterService.List(c.Request.Context(), userID, search)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch counters"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, counters)
|
||||
}
|
||||
|
||||
// Update handles counter updates
|
||||
func (h *CounterHandler) Update(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
counterID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
|
||||
return
|
||||
}
|
||||
|
||||
var req counter.UpdateCounterRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
counter, err := h.counterService.Update(c.Request.Context(), counterID, userID, &req)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case entities.ErrCounterNotFound:
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update counter"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, counter)
|
||||
}
|
||||
|
||||
// Delete handles counter deletion
|
||||
func (h *CounterHandler) Delete(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
counterID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
|
||||
return
|
||||
}
|
||||
|
||||
err = h.counterService.Delete(c.Request.Context(), counterID, userID)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case entities.ErrCounterNotFound:
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete counter"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Counter deleted successfully"})
|
||||
}
|
||||
|
||||
// Increment handles counter increment/decrement
|
||||
func (h *CounterHandler) Increment(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
counterID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
|
||||
return
|
||||
}
|
||||
|
||||
var req counter.IncrementRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
entry, err := h.counterService.Increment(c.Request.Context(), counterID, userID, &req)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case entities.ErrCounterNotFound:
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create counter entry"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, entry)
|
||||
}
|
||||
|
||||
// GetEntries handles retrieving counter entries
|
||||
func (h *CounterHandler) GetEntries(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
counterID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
|
||||
return
|
||||
}
|
||||
|
||||
// Parse date range parameters
|
||||
var startDate, endDate *time.Time
|
||||
if startDateStr := c.Query("start_date"); startDateStr != "" {
|
||||
if parsed, err := time.Parse("2006-01-02", startDateStr); err == nil {
|
||||
startDate = &parsed
|
||||
}
|
||||
}
|
||||
if endDateStr := c.Query("end_date"); endDateStr != "" {
|
||||
if parsed, err := time.Parse("2006-01-02", endDateStr); err == nil {
|
||||
endDate = &parsed
|
||||
}
|
||||
}
|
||||
|
||||
entries, err := h.counterService.GetEntries(c.Request.Context(), counterID, userID, startDate, endDate)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case entities.ErrCounterNotFound:
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch counter entries"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, entries)
|
||||
}
|
||||
|
||||
// GetStats handles retrieving counter statistics
|
||||
func (h *CounterHandler) GetStats(c *gin.Context) {
|
||||
userID := c.GetInt("user_id")
|
||||
counterID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid counter ID"})
|
||||
return
|
||||
}
|
||||
|
||||
// Parse days parameter
|
||||
days := 30 // default
|
||||
if daysStr := c.Query("days"); daysStr != "" {
|
||||
if parsed, err := strconv.Atoi(daysStr); err == nil && parsed > 0 {
|
||||
days = parsed
|
||||
}
|
||||
}
|
||||
|
||||
stats, err := h.counterService.GetStats(c.Request.Context(), counterID, userID, days)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case entities.ErrCounterNotFound:
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Counter not found"})
|
||||
default:
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch statistics"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"daily_stats": stats})
|
||||
}
|
||||
57
internal/delivery/http/middleware/auth.go
Normal file
57
internal/delivery/http/middleware/auth.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"counter/internal/infrastructure/security"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// AuthMiddleware validates JWT token and sets user context
|
||||
func AuthMiddleware(jwtService security.JWTService) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
if authHeader == "" {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header required"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// Extract token from "Bearer <token>"
|
||||
tokenParts := strings.Split(authHeader, " ")
|
||||
if len(tokenParts) != 2 || tokenParts[0] != "Bearer" {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid authorization header format"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
tokenString := tokenParts[1]
|
||||
claims, err := jwtService.ValidateToken(tokenString)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// Set user information in context
|
||||
userID, ok := claims["user_id"].(float64)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token claims"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
username, ok := claims["username"].(string)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token claims"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("user_id", int(userID))
|
||||
c.Set("username", username)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
43
internal/delivery/http/middleware/logging.go
Normal file
43
internal/delivery/http/middleware/logging.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"counter/internal/infrastructure/config"
|
||||
"counter/internal/infrastructure/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// LoggingMiddleware creates a Gin middleware for HTTP request logging
|
||||
func LoggingMiddleware(logger logging.Logger, cfg *config.Config) gin.HandlerFunc {
|
||||
return gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
|
||||
// Create structured log entry with default fields
|
||||
entry := logger.WithFields(logrus.Fields{
|
||||
"service": "counter-app",
|
||||
"environment": string(cfg.Environment),
|
||||
"version": "1.0.0",
|
||||
"method": param.Method,
|
||||
"path": param.Path,
|
||||
"status": param.StatusCode,
|
||||
"latency": param.Latency.String(),
|
||||
"client_ip": param.ClientIP,
|
||||
"user_agent": param.Request.UserAgent(),
|
||||
"timestamp": param.TimeStamp.Format(time.RFC3339),
|
||||
})
|
||||
|
||||
// Set log level based on status code
|
||||
switch {
|
||||
case param.StatusCode >= 500:
|
||||
entry.Error("HTTP Request")
|
||||
case param.StatusCode >= 400:
|
||||
entry.Warn("HTTP Request")
|
||||
default:
|
||||
entry.Info("HTTP Request")
|
||||
}
|
||||
|
||||
// Return empty string since we're handling logging ourselves
|
||||
return ""
|
||||
})
|
||||
}
|
||||
21
internal/delivery/http/middleware/metrics.go
Normal file
21
internal/delivery/http/middleware/metrics.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"counter/internal/infrastructure/metrics"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// MetricsMiddleware creates a Gin middleware for HTTP metrics
|
||||
func MetricsMiddleware(metricsService metrics.MetricsService) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
|
||||
c.Next()
|
||||
|
||||
duration := time.Since(start)
|
||||
metricsService.RecordHTTPRequest(c.Request.Method, c.FullPath(), c.Writer.Status(), duration)
|
||||
}
|
||||
}
|
||||
107
internal/delivery/http/router.go
Normal file
107
internal/delivery/http/router.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"counter/internal/delivery/http/handlers"
|
||||
"counter/internal/delivery/http/middleware"
|
||||
"counter/internal/infrastructure/config"
|
||||
"counter/internal/infrastructure/logging"
|
||||
"counter/internal/infrastructure/metrics"
|
||||
"counter/internal/infrastructure/security"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Router sets up all HTTP routes and middleware
|
||||
type Router struct {
|
||||
router *gin.Engine
|
||||
authHandler *handlers.AuthHandler
|
||||
counterHandler *handlers.CounterHandler
|
||||
config *config.Config
|
||||
logger logging.Logger
|
||||
metricsService metrics.MetricsService
|
||||
jwtService security.JWTService
|
||||
}
|
||||
|
||||
// NewRouter creates a new router with all dependencies
|
||||
func NewRouter(
|
||||
authHandler *handlers.AuthHandler,
|
||||
counterHandler *handlers.CounterHandler,
|
||||
cfg *config.Config,
|
||||
logger logging.Logger,
|
||||
metricsService metrics.MetricsService,
|
||||
jwtService security.JWTService,
|
||||
) *Router {
|
||||
// Set Gin mode
|
||||
gin.SetMode(cfg.GinMode)
|
||||
|
||||
router := gin.Default()
|
||||
|
||||
return &Router{
|
||||
router: router,
|
||||
authHandler: authHandler,
|
||||
counterHandler: counterHandler,
|
||||
config: cfg,
|
||||
logger: logger,
|
||||
metricsService: metricsService,
|
||||
jwtService: jwtService,
|
||||
}
|
||||
}
|
||||
|
||||
// SetupRoutes configures all routes and middleware
|
||||
func (r *Router) SetupRoutes() {
|
||||
// Configure CORS
|
||||
corsConfig := cors.DefaultConfig()
|
||||
corsConfig.AllowOrigins = []string{"http://localhost:3000", "http://localhost:5173"} // React dev servers
|
||||
corsConfig.AllowMethods = []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}
|
||||
corsConfig.AllowHeaders = []string{"Origin", "Content-Type", "Accept", "Authorization"}
|
||||
corsConfig.AllowCredentials = true
|
||||
r.router.Use(cors.New(corsConfig))
|
||||
|
||||
// Add middleware
|
||||
r.router.Use(middleware.MetricsMiddleware(r.metricsService))
|
||||
r.router.Use(middleware.LoggingMiddleware(r.logger, r.config))
|
||||
|
||||
// Health check endpoint
|
||||
r.router.GET("/health", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"status": "ok"})
|
||||
})
|
||||
|
||||
// API routes
|
||||
api := r.router.Group("/api/v1")
|
||||
{
|
||||
// Authentication routes
|
||||
authGroup := api.Group("/auth")
|
||||
{
|
||||
authGroup.POST("/register", r.authHandler.Register)
|
||||
authGroup.POST("/login", r.authHandler.Login)
|
||||
authGroup.GET("/me", middleware.AuthMiddleware(r.jwtService), r.authHandler.GetMe)
|
||||
}
|
||||
|
||||
// Counter routes (protected)
|
||||
counterGroup := api.Group("/counters")
|
||||
counterGroup.Use(middleware.AuthMiddleware(r.jwtService))
|
||||
{
|
||||
counterGroup.POST("", r.counterHandler.Create)
|
||||
counterGroup.GET("", r.counterHandler.List)
|
||||
counterGroup.GET("/:id", r.counterHandler.Get)
|
||||
counterGroup.PUT("/:id", r.counterHandler.Update)
|
||||
counterGroup.DELETE("/:id", r.counterHandler.Delete)
|
||||
counterGroup.POST("/:id/increment", r.counterHandler.Increment)
|
||||
counterGroup.GET("/:id/entries", r.counterHandler.GetEntries)
|
||||
counterGroup.GET("/:id/stats", r.counterHandler.GetStats)
|
||||
}
|
||||
}
|
||||
|
||||
// Serve static files (React app)
|
||||
r.router.Static("/static", "./frontend/build/static")
|
||||
r.router.StaticFile("/", "./frontend/build/index.html")
|
||||
r.router.NoRoute(func(c *gin.Context) {
|
||||
c.File("./frontend/build/index.html")
|
||||
})
|
||||
}
|
||||
|
||||
// GetRouter returns the configured Gin router
|
||||
func (r *Router) GetRouter() *gin.Engine {
|
||||
return r.router
|
||||
}
|
||||
Reference in New Issue
Block a user