passing logger context

This commit is contained in:
SwiftyOS 2024-09-13 20:04:54 +02:00
parent f598ba27b6
commit 5e0dad5a96
4 changed files with 37 additions and 36 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
"github.com/swiftyos/market/database"
"github.com/swiftyos/market/models"
"go.uber.org/zap"
)
func requireAdminUser() gin.HandlerFunc {
@ -44,7 +45,7 @@ func requireAdminUser() gin.HandlerFunc {
// @Param request body models.AddAgentRequest true "Agent details"
// @Success 200 {object} models.Agent
// @Router /agents [post]
func CreateAgentEntry(db *pgxpool.Pool) gin.HandlerFunc {
func CreateAgentEntry(db *pgxpool.Pool, log_ctx *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
requireAdminUser()(c)
if c.IsAborted() {
@ -85,7 +86,7 @@ func CreateAgentEntry(db *pgxpool.Pool) gin.HandlerFunc {
// @Param category query string false "Category"
// @Success 200 {object} models.Agent
// @Router /agent/featured/{agent_id} [post]
func SetAgentFeatured(db *pgxpool.Pool) gin.HandlerFunc {
func SetAgentFeatured(db *pgxpool.Pool, log_ctx *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
requireAdminUser()(c)
if c.IsAborted() {
@ -118,7 +119,7 @@ func SetAgentFeatured(db *pgxpool.Pool) gin.HandlerFunc {
// @Param category query string false "Category"
// @Success 200 {object} models.Agent
// @Router /agent/featured/{agent_id} [get]
func GetAgentFeatured(db *pgxpool.Pool) gin.HandlerFunc {
func GetAgentFeatured(db *pgxpool.Pool, log_ctx *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
requireAdminUser()(c)
if c.IsAborted() {
@ -152,7 +153,7 @@ func GetAgentFeatured(db *pgxpool.Pool) gin.HandlerFunc {
// @Param category query string false "Category"
// @Success 200 {object} models.Agent
// @Router /agent/featured/{agent_id} [delete]
func UnsetAgentFeatured(db *pgxpool.Pool) gin.HandlerFunc {
func UnsetAgentFeatured(db *pgxpool.Pool, log_ctx *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
requireAdminUser()(c)
if c.IsAborted() {
@ -187,7 +188,7 @@ func UnsetAgentFeatured(db *pgxpool.Pool) gin.HandlerFunc {
// @Param page_size query int false "Page size"
// @Success 200 {object} models.Agent
// @Router /agent/not-featured [get]
func GetNotFeaturedAgents(db *pgxpool.Pool) gin.HandlerFunc {
func GetNotFeaturedAgents(db *pgxpool.Pool, log_ctx *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
requireAdminUser()(c)
if c.IsAborted() {
@ -222,7 +223,7 @@ func GetNotFeaturedAgents(db *pgxpool.Pool) gin.HandlerFunc {
// @Param page_size query int false "Page size"
// @Success 200 {object} models.Agent
// @Router /agent/submissions [get]
func GetAgentSubmissions(db *pgxpool.Pool) gin.HandlerFunc {
func GetAgentSubmissions(db *pgxpool.Pool, log_ctx *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
requireAdminUser()(c)
if c.IsAborted() {
@ -244,7 +245,7 @@ func GetAgentSubmissions(db *pgxpool.Pool) gin.HandlerFunc {
// @Param status query string true "Status"
// @Success 200 {object} models.Agent
// @Router /agent/submissions/{agent_id} [post]
func ReviewSubmission(db *pgxpool.Pool) gin.HandlerFunc {
func ReviewSubmission(db *pgxpool.Pool, log_ctx *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
requireAdminUser()(c)
if c.IsAborted() {
@ -264,7 +265,7 @@ func ReviewSubmission(db *pgxpool.Pool) gin.HandlerFunc {
// @Produce json
// @Success 200 {array} string
// @Router /categories [get]
func GetCategories(db *pgxpool.Pool) gin.HandlerFunc {
func GetCategories(db *pgxpool.Pool, log_ctx *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
if c.IsAborted() {
return

View File

@ -71,9 +71,9 @@ func GetAgents(db *pgxpool.Pool, log_ctx *zap.Logger) gin.HandlerFunc {
// @Param agent body models.AddAgentRequest true "Agent details"
// @Success 200 {object} models.Agent
// @Router /agents [post]
func SubmitAgent(db *pgxpool.Pool) gin.HandlerFunc {
func SubmitAgent(db *pgxpool.Pool, log_ctx *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
logger := zap.L().With(zap.String("function", "SubmitAgent"))
logger := log_ctx.With(zap.String("function", "SubmitAgent"))
var request models.AddAgentRequest
logger.Debug("Add Agent Request body", zap.Any("request", request))
if err := c.ShouldBindJSON(&request); err != nil {
@ -106,9 +106,9 @@ func SubmitAgent(db *pgxpool.Pool) gin.HandlerFunc {
// @Param id path string true "Agent ID"
// @Success 200 {object} models.Agent
// @Router /agents/{id} [get]
func GetAgentDetails(db *pgxpool.Pool) gin.HandlerFunc {
func GetAgentDetails(db *pgxpool.Pool, log_ctx *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
logger := zap.L().With(zap.String("function", "GetAgentDetails"))
logger := log_ctx.With(zap.String("function", "GetAgentDetails"))
agentID := c.Param("id")
logger.Debug("Agent ID", zap.String("agentID", agentID))
@ -143,9 +143,9 @@ func GetAgentDetails(db *pgxpool.Pool) gin.HandlerFunc {
// @Param id path string true "Agent ID"
// @Success 200 {object} models.Agent
// @Router /agents/{id}/download [get]
func DownloadAgent(db *pgxpool.Pool) gin.HandlerFunc {
func DownloadAgent(db *pgxpool.Pool, log_ctx *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
logger := zap.L().With(zap.String("function", "DownloadAgent"))
logger := log_ctx.With(zap.String("function", "DownloadAgent"))
agentID := c.Param("id")
if agentID == "" {
@ -184,9 +184,9 @@ func DownloadAgent(db *pgxpool.Pool) gin.HandlerFunc {
// @Param id path string true "Agent ID"
// @Success 200 {object} models.Agent
// @Router /agents/{id}/download [get]
func DownloadAgentFile(db *pgxpool.Pool) gin.HandlerFunc {
func DownloadAgentFile(db *pgxpool.Pool, log_ctx *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
logger := zap.L().With(zap.String("function", "DownloadAgentFile"))
logger := log_ctx.With(zap.String("function", "DownloadAgentFile"))
agentID := c.Param("id")
if agentID == "" {
@ -219,9 +219,9 @@ func DownloadAgentFile(db *pgxpool.Pool) gin.HandlerFunc {
}
}
func TopAgentsByDownloads(db *pgxpool.Pool) gin.HandlerFunc {
func TopAgentsByDownloads(db *pgxpool.Pool, log_ctx *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
logger := zap.L().With(zap.String("function", "TopAgentsByDownloads"))
logger := log_ctx.With(zap.String("function", "TopAgentsByDownloads"))
logger.Info("Handling request for top agents by downloads")
page, err := strconv.Atoi(c.DefaultQuery("page", "1"))
@ -321,9 +321,9 @@ func GetFeaturedAgents(db *pgxpool.Pool, logger *zap.Logger) gin.HandlerFunc {
// @Param sortOrder query string false "Sort order"
// @Success 200 {array} models.Agent
// @Router /agents/search [get]
func SearchAgents(db *pgxpool.Pool) gin.HandlerFunc {
func SearchAgents(db *pgxpool.Pool, log_ctx *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
logger := zap.L().With(zap.String("function", "Search"))
logger := log_ctx.With(zap.String("function", "Search"))
logger.Info("Handling search request")
query := c.Query("q")

View File

@ -11,7 +11,7 @@ import (
"github.com/swiftyos/market/models"
)
func AgentInstalled(db *pgxpool.Pool) gin.HandlerFunc {
func AgentInstalled(db *pgxpool.Pool, log_ctx *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
logger := zap.L().With(zap.String("function", "AgentInstalled"))
var eventData models.InstallTracker

View File

@ -82,30 +82,30 @@ func main() {
agents := api.Group("/agents")
{
agents.GET("", handlers.GetAgents(db, logger))
agents.GET("/:agent_id", handlers.GetAgentDetails(db))
agents.GET("/:agent_id/download", handlers.DownloadAgent(db))
agents.GET("/:agent_id/download-file", handlers.DownloadAgentFile(db))
agents.GET("/top-downloads", handlers.TopAgentsByDownloads(db))
agents.GET("/:agent_id", handlers.GetAgentDetails(db, logger))
agents.GET("/:agent_id/download", handlers.DownloadAgent(db, logger))
agents.GET("/:agent_id/download-file", handlers.DownloadAgentFile(db, logger))
agents.GET("/top-downloads", handlers.TopAgentsByDownloads(db, logger))
agents.GET("/featured", handlers.GetFeaturedAgents(db, logger))
agents.GET("/search", handlers.SearchAgents(db))
agents.POST("/submit", middleware.Auth(cfg), handlers.SubmitAgent(db))
agents.GET("/search", handlers.SearchAgents(db, logger))
agents.POST("/submit", middleware.Auth(cfg), handlers.SubmitAgent(db, logger))
}
// Admin routes
admin := api.Group("/admin")
{
admin.POST("/agent", middleware.Auth(cfg), handlers.CreateAgentEntry(db))
admin.POST("/agent/featured/:agent_id", middleware.Auth(cfg), handlers.SetAgentFeatured(db))
admin.GET("/agent/featured/:agent_id", middleware.Auth(cfg), handlers.GetAgentFeatured(db))
admin.DELETE("/agent/featured/:agent_id", middleware.Auth(cfg), handlers.UnsetAgentFeatured(db))
admin.GET("/agent/not-featured", middleware.Auth(cfg), handlers.GetNotFeaturedAgents(db))
admin.GET("/agent/submissions", middleware.Auth(cfg), handlers.GetAgentSubmissions(db))
admin.POST("/agent/submissions", middleware.Auth(cfg), handlers.ReviewSubmission(db))
admin.POST("/agent", middleware.Auth(cfg), handlers.CreateAgentEntry(db, logger))
admin.POST("/agent/featured/:agent_id", middleware.Auth(cfg), handlers.SetAgentFeatured(db, logger))
admin.GET("/agent/featured/:agent_id", middleware.Auth(cfg), handlers.GetAgentFeatured(db, logger))
admin.DELETE("/agent/featured/:agent_id", middleware.Auth(cfg), handlers.UnsetAgentFeatured(db, logger))
admin.GET("/agent/not-featured", middleware.Auth(cfg), handlers.GetNotFeaturedAgents(db, logger))
admin.GET("/agent/submissions", middleware.Auth(cfg), handlers.GetAgentSubmissions(db, logger))
admin.POST("/agent/submissions", middleware.Auth(cfg), handlers.ReviewSubmission(db, logger))
}
api.GET("/categories", handlers.GetCategories(db))
api.GET("/categories", handlers.GetCategories(db, logger))
// Analytics routes
api.POST("/agent-installed", handlers.AgentInstalled(db))
api.POST("/agent-installed", handlers.AgentInstalled(db, logger))
}
r.GET("/docs/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))