Skip to content

Commit

Permalink
split files and ad v2 preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
AchimGrolimund committed Dec 14, 2024
1 parent 630ec1e commit 018cbf9
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 89 deletions.
5 changes: 2 additions & 3 deletions cmd/csp-scout-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ func main() {
log.Fatalf("Failed to connect to MongoDB: %v", err)
}

// Create service and handler
// Create service
service := application.NewReportService(repo)
handler := handlers.NewReportHandler(service)

// Initialize Gin router
router := gin.Default()
Expand All @@ -45,7 +44,7 @@ func main() {
router.Use(cors.Default())

// Setup routes
handlers.SetupRoutes(router, handler)
handlers.RegisterRoutes(router, service)

// Start the server
router.Run()
Expand Down
46 changes: 46 additions & 0 deletions pkg/interfaces/http/handlers/base_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package handlers

import (
"github.com/AchimGrolimund/CSP-Scout-API/pkg/application"
"github.com/gin-gonic/gin"
)

// APIVersion represents the API version
type APIVersion string

const (
V1 APIVersion = "v1"
V2 APIVersion = "v2"
)

// RegisterRoutes configures all API routes with versioning
func RegisterRoutes(router *gin.Engine, service *application.ReportService) {
// Register V1 routes
apiV1 := router.Group("/api/v1")
setupV1Routes(apiV1, service)

// Register V2 routes when needed
// apiV2 := router.Group("/api/v2")
// setupV2Routes(apiV2, service)
}

// setupV1Routes configures all V1 API routes
func setupV1Routes(router *gin.RouterGroup, service *application.ReportService) {
// Reports CRUD routes
setupReportRoutesV1(router, service)

// Statistics routes
setupStatisticsRoutesV1(router, service)
}

// setupV2Routes configures all V2 API routes
// Uncomment and implement when V2 is needed
/*
func setupV2Routes(router *gin.RouterGroup, service *application.ReportService) {
// Reports CRUD routes
setupReportRoutesV2(router, service)
// Statistics routes
setupStatisticsRoutesV2(router, service)
}
*/
86 changes: 0 additions & 86 deletions pkg/interfaces/http/handlers/report_handler.go

This file was deleted.

94 changes: 94 additions & 0 deletions pkg/interfaces/http/handlers/reports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package handlers

import (
"net/http"

"github.com/AchimGrolimund/CSP-Scout-API/pkg/application"
"github.com/AchimGrolimund/CSP-Scout-API/pkg/domain"
"github.com/gin-gonic/gin"
)

type ReportsHandler struct {
service *application.ReportService
}

func NewReportsHandler(service *application.ReportService) *ReportsHandler {
return &ReportsHandler{
service: service,
}
}

// V1 Routes
func setupReportRoutesV1(router *gin.RouterGroup, service *application.ReportService) {
handler := NewReportsHandler(service)
reports := router.Group("/reports")
{
reports.POST("", handler.CreateV1)
reports.GET("", handler.ListV1)
reports.GET("/:id", handler.GetV1)
}
}

// V2 Routes (for future implementation)
func setupReportRoutesV2(router *gin.RouterGroup, service *application.ReportService) {
handler := NewReportsHandler(service)
reports := router.Group("/reports")
{
reports.POST("", handler.CreateV2)
reports.GET("", handler.ListV2)
reports.GET("/:id", handler.GetV2)
}
}

// V1 Handlers
func (h *ReportsHandler) CreateV1(c *gin.Context) {
var report domain.Report
if err := c.ShouldBindJSON(&report); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

if err := h.service.CreateReport(c.Request.Context(), &report); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusCreated, report)
}

func (h *ReportsHandler) GetV1(c *gin.Context) {
id := c.Param("id")
report, err := h.service.GetReport(c.Request.Context(), id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, report)
}

func (h *ReportsHandler) ListV1(c *gin.Context) {
reports, err := h.service.ListReports(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, reports)
}

// V2 Handlers (for future implementation)
func (h *ReportsHandler) CreateV2(c *gin.Context) {
// Implement V2 create logic when needed
c.JSON(http.StatusNotImplemented, gin.H{"error": "V2 not implemented yet"})
}

func (h *ReportsHandler) GetV2(c *gin.Context) {
// Implement V2 get logic when needed
c.JSON(http.StatusNotImplemented, gin.H{"error": "V2 not implemented yet"})
}

func (h *ReportsHandler) ListV2(c *gin.Context) {
// Implement V2 list logic when needed
c.JSON(http.StatusNotImplemented, gin.H{"error": "V2 not implemented yet"})
}
70 changes: 70 additions & 0 deletions pkg/interfaces/http/handlers/statistics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package handlers

import (
"net/http"

"github.com/AchimGrolimund/CSP-Scout-API/pkg/application"
"github.com/gin-gonic/gin"
)

type StatisticsHandler struct {
service *application.ReportService
}

func NewStatisticsHandler(service *application.ReportService) *StatisticsHandler {
return &StatisticsHandler{
service: service,
}
}

// V1 Routes
func setupStatisticsRoutesV1(router *gin.RouterGroup, service *application.ReportService) {
handler := NewStatisticsHandler(service)
stats := router.Group("/statistics")
{
stats.GET("/top-ips", handler.GetTopIPsV1)
stats.GET("/top-directives", handler.GetTopViolatedDirectivesV1)
}
}

// V2 Routes (for future implementation)
func setupStatisticsRoutesV2(router *gin.RouterGroup, service *application.ReportService) {
handler := NewStatisticsHandler(service)
stats := router.Group("/statistics")
{
stats.GET("/top-ips", handler.GetTopIPsV2)
stats.GET("/top-directives", handler.GetTopViolatedDirectivesV2)
}
}

// V1 Handlers
func (h *StatisticsHandler) GetTopIPsV1(c *gin.Context) {
topIPs, err := h.service.GetTopIPs(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, topIPs)
}

func (h *StatisticsHandler) GetTopViolatedDirectivesV1(c *gin.Context) {
topDirectives, err := h.service.GetTopViolatedDirectives(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, topDirectives)
}

// V2 Handlers (for future implementation)
func (h *StatisticsHandler) GetTopIPsV2(c *gin.Context) {
// Implement V2 top IPs logic when needed
c.JSON(http.StatusNotImplemented, gin.H{"error": "V2 not implemented yet"})
}

func (h *StatisticsHandler) GetTopViolatedDirectivesV2(c *gin.Context) {
// Implement V2 top directives logic when needed
c.JSON(http.StatusNotImplemented, gin.H{"error": "V2 not implemented yet"})
}

0 comments on commit 018cbf9

Please sign in to comment.