-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
630ec1e
commit 018cbf9
Showing
5 changed files
with
212 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
*/ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) | ||
} |