Add missing comments to response.go and middleware.go for better code documentation

This commit is contained in:
Christoph Haas 2026-03-23 22:14:35 +01:00
parent 94d504ee1e
commit 9c8d982230
2 changed files with 16 additions and 0 deletions

View file

@ -1,3 +1,4 @@
// Package handlers provides HTTP request handlers for the API.
package handlers package handlers
import ( import (
@ -8,8 +9,11 @@ import (
"github.com/imc-vibe/backend/internal/auth" "github.com/imc-vibe/backend/internal/auth"
) )
// AuthContextKey is the key used to store auth context in gin.Context.
const AuthContextKey = "auth" const AuthContextKey = "auth"
// AuthMiddleware validates JWT tokens and sets up the auth context.
// Returns 401 if the token is missing, invalid, or expired.
func AuthMiddleware(jwtManager *auth.JWTManager) gin.HandlerFunc { func AuthMiddleware(jwtManager *auth.JWTManager) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
authHeader := c.GetHeader("Authorization") authHeader := c.GetHeader("Authorization")
@ -41,6 +45,8 @@ func AuthMiddleware(jwtManager *auth.JWTManager) gin.HandlerFunc {
} }
} }
// GetAuthContext retrieves the auth context from the gin.Context.
// Returns nil if the context is not set (auth middleware was not used).
func GetAuthContext(c *gin.Context) *auth.Context { func GetAuthContext(c *gin.Context) *auth.Context {
if ctx, exists := c.Get(AuthContextKey); exists { if ctx, exists := c.Get(AuthContextKey); exists {
if authCtx, ok := ctx.(*auth.Context); ok { if authCtx, ok := ctx.(*auth.Context); ok {
@ -50,6 +56,8 @@ func GetAuthContext(c *gin.Context) *auth.Context {
return nil return nil
} }
// RequireAdmin is a middleware that checks if the current user is an admin.
// Returns 403 Forbidden if the user is not an admin.
func RequireAdmin() gin.HandlerFunc { func RequireAdmin() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
authCtx := GetAuthContext(c) authCtx := GetAuthContext(c)

View file

@ -1,36 +1,44 @@
// Package handlers provides HTTP request handlers for the API.
package handlers package handlers
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// Response is the standard JSON response format for all API endpoints.
type Response struct { type Response struct {
Data interface{} `json:"data"` Data interface{} `json:"data"`
Error string `json:"error,omitempty"` Error string `json:"error,omitempty"`
Meta *Meta `json:"meta,omitempty"` Meta *Meta `json:"meta,omitempty"`
} }
// Meta contains pagination information for list endpoints.
type Meta struct { type Meta struct {
Total int `json:"total,omitempty"` Total int `json:"total,omitempty"`
Page int `json:"page,omitempty"` Page int `json:"page,omitempty"`
} }
// JSON sends a response with the given status code and data.
func JSON(c *gin.Context, status int, data interface{}) { func JSON(c *gin.Context, status int, data interface{}) {
c.JSON(status, Response{Data: data}) c.JSON(status, Response{Data: data})
} }
// Error sends an error response with the given status code and message.
func Error(c *gin.Context, status int, message string) { func Error(c *gin.Context, status int, message string) {
c.JSON(status, Response{Error: message}) c.JSON(status, Response{Error: message})
} }
// Success sends a 200 OK response with the given data.
func Success(c *gin.Context, data interface{}) { func Success(c *gin.Context, data interface{}) {
JSON(c, 200, data) JSON(c, 200, data)
} }
// Created sends a 201 Created response with the given data.
func Created(c *gin.Context, data interface{}) { func Created(c *gin.Context, data interface{}) {
JSON(c, 201, data) JSON(c, 201, data)
} }
// NoContent sends a 204 No Content response.
func NoContent(c *gin.Context) { func NoContent(c *gin.Context) {
c.Status(204) c.Status(204)
} }