Switch from net/http to gin-gonic web framework
- Added gin-gonic v1.10.0 dependency - Refactored router.go: clean route groups with middleware chains - Refactored all handlers to use gin.Context instead of http.ResponseWriter/*http.Request - Simplified response helpers (JSON, Error, Success, Created, NoContent) - Clean auth middleware using Gin's c.Set() for context - Cleaner route definitions with path parameters (e.g., /domains/:name/users/:id) - Admin routes moved to /api/admin group with RequireAdmin middleware
This commit is contained in:
parent
68285d861a
commit
2834657125
13 changed files with 474 additions and 812 deletions
|
|
@ -1,11 +1,10 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/imc-vibe/backend/internal/db"
|
||||
)
|
||||
|
||||
|
|
@ -18,143 +17,115 @@ func NewAliasHandler(database *db.DB) *AliasHandler {
|
|||
}
|
||||
|
||||
type CreateAliasRequest struct {
|
||||
Source string `json:"source"`
|
||||
Destination string `json:"destination"`
|
||||
Source string `json:"source" binding:"required"`
|
||||
Destination string `json:"destination" binding:"required"`
|
||||
}
|
||||
|
||||
func (h *AliasHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
Error(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
return
|
||||
}
|
||||
|
||||
domainName := extractDomainNameFromPath(r.URL.Path)
|
||||
func (h *AliasHandler) List(c *gin.Context) {
|
||||
domainName := c.Param("name")
|
||||
if domainName == "" {
|
||||
Error(w, http.StatusBadRequest, "domain name required")
|
||||
Error(c, http.StatusBadRequest, "domain name required")
|
||||
return
|
||||
}
|
||||
|
||||
authCtx := GetAuthContext(r)
|
||||
authCtx := GetAuthContext(c)
|
||||
if authCtx == nil {
|
||||
Error(w, http.StatusUnauthorized, "authentication required")
|
||||
Error(c, http.StatusUnauthorized, "authentication required")
|
||||
return
|
||||
}
|
||||
|
||||
canAccess, _ := h.db.CanAccessDomain(authCtx.UserID, domainName, authCtx.IsAdmin())
|
||||
if !canAccess {
|
||||
Error(w, http.StatusForbidden, "access denied")
|
||||
Error(c, http.StatusForbidden, "access denied")
|
||||
return
|
||||
}
|
||||
|
||||
domain, err := h.db.GetDomainByName(domainName)
|
||||
if err != nil {
|
||||
Error(w, http.StatusNotFound, "domain not found")
|
||||
Error(c, http.StatusNotFound, "domain not found")
|
||||
return
|
||||
}
|
||||
|
||||
aliases, err := h.db.GetAliasesByDomain(domain.ID)
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "database error")
|
||||
Error(c, http.StatusInternalServerError, "database error")
|
||||
return
|
||||
}
|
||||
|
||||
Success(w, aliases)
|
||||
Success(c, aliases)
|
||||
}
|
||||
|
||||
func (h *AliasHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
Error(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
return
|
||||
}
|
||||
|
||||
domainName := extractDomainNameFromPath(r.URL.Path)
|
||||
func (h *AliasHandler) Create(c *gin.Context) {
|
||||
domainName := c.Param("name")
|
||||
if domainName == "" {
|
||||
Error(w, http.StatusBadRequest, "domain name required")
|
||||
Error(c, http.StatusBadRequest, "domain name required")
|
||||
return
|
||||
}
|
||||
|
||||
authCtx := GetAuthContext(r)
|
||||
authCtx := GetAuthContext(c)
|
||||
if authCtx == nil {
|
||||
Error(w, http.StatusUnauthorized, "authentication required")
|
||||
Error(c, http.StatusUnauthorized, "authentication required")
|
||||
return
|
||||
}
|
||||
|
||||
canAccess, _ := h.db.CanAccessDomain(authCtx.UserID, domainName, authCtx.IsAdmin())
|
||||
if !canAccess {
|
||||
Error(w, http.StatusForbidden, "access denied")
|
||||
Error(c, http.StatusForbidden, "access denied")
|
||||
return
|
||||
}
|
||||
|
||||
domain, err := h.db.GetDomainByName(domainName)
|
||||
if err != nil {
|
||||
Error(w, http.StatusNotFound, "domain not found")
|
||||
Error(c, http.StatusNotFound, "domain not found")
|
||||
return
|
||||
}
|
||||
|
||||
var req CreateAliasRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
Error(w, http.StatusBadRequest, "invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
if req.Source == "" || req.Destination == "" {
|
||||
Error(w, http.StatusBadRequest, "source and destination required")
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
Error(c, http.StatusBadRequest, "invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
alias, err := h.db.CreateAliasInDomain(req.Source, req.Destination, domain.ID)
|
||||
if err != nil {
|
||||
Error(w, http.StatusInternalServerError, "failed to create alias")
|
||||
Error(c, http.StatusInternalServerError, "failed to create alias")
|
||||
return
|
||||
}
|
||||
|
||||
Created(w, alias)
|
||||
Created(c, alias)
|
||||
}
|
||||
|
||||
func (h *AliasHandler) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodDelete {
|
||||
Error(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
return
|
||||
}
|
||||
|
||||
pathParts := strings.Split(r.URL.Path, "/")
|
||||
var domainName, idStr string
|
||||
for i, part := range pathParts {
|
||||
if part == "domains" && i+1 < len(pathParts) {
|
||||
domainName = pathParts[i+1]
|
||||
}
|
||||
if part == "aliases" && i+1 < len(pathParts) {
|
||||
idStr = pathParts[i+1]
|
||||
}
|
||||
}
|
||||
func (h *AliasHandler) Delete(c *gin.Context) {
|
||||
domainName := c.Param("name")
|
||||
idStr := c.Param("id")
|
||||
|
||||
if domainName == "" || idStr == "" {
|
||||
Error(w, http.StatusBadRequest, "invalid path")
|
||||
Error(c, http.StatusBadRequest, "domain name and alias id required")
|
||||
return
|
||||
}
|
||||
|
||||
authCtx := GetAuthContext(r)
|
||||
authCtx := GetAuthContext(c)
|
||||
if authCtx == nil {
|
||||
Error(w, http.StatusUnauthorized, "authentication required")
|
||||
Error(c, http.StatusUnauthorized, "authentication required")
|
||||
return
|
||||
}
|
||||
|
||||
canAccess, _ := h.db.CanAccessDomain(authCtx.UserID, domainName, authCtx.IsAdmin())
|
||||
if !canAccess {
|
||||
Error(w, http.StatusForbidden, "access denied")
|
||||
Error(c, http.StatusForbidden, "access denied")
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
Error(w, http.StatusBadRequest, "invalid alias id")
|
||||
Error(c, http.StatusBadRequest, "invalid alias id")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.db.DeleteAlias(uint(id)); err != nil {
|
||||
Error(w, http.StatusInternalServerError, "failed to delete alias")
|
||||
Error(c, http.StatusInternalServerError, "failed to delete alias")
|
||||
return
|
||||
}
|
||||
|
||||
NoContent(w)
|
||||
NoContent(c)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue