connected-to-google

This commit is contained in:
mario
2025-04-07 15:46:07 +07:00
parent f340bc5916
commit 9b8e0260f3
15 changed files with 282 additions and 286 deletions

View File

@@ -1,94 +1,61 @@
package middleware
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/go-chi/chi/v5/middleware"
"go.uber.org/zap"
)
// Logger middleware adds request logging
func Logger(logger *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
query := c.Request.URL.RawQuery
func Logger(logger *zap.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
// Process request
c.Next()
// Create a wrapped response writer to capture the status code
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
// Calculate request time
latency := time.Since(start)
// Process request
next.ServeHTTP(ww, r)
// Get status
status := c.Writer.Status()
// Calculate request time
latency := time.Since(start)
// Log request details
logger.Info("API Request",
zap.String("method", c.Request.Method),
zap.String("path", path),
zap.String("query", query),
zap.Int("status", status),
zap.Duration("latency", latency),
zap.String("ip", c.ClientIP()),
zap.String("user-agent", c.Request.UserAgent()),
)
// Log request details
logger.Info("API Request",
zap.String("method", r.Method),
zap.String("path", r.URL.Path),
zap.String("query", r.URL.RawQuery),
zap.Int("status", ww.Status()),
zap.Duration("latency", latency),
zap.String("ip", r.RemoteAddr),
zap.String("user-agent", r.UserAgent()),
zap.Int("bytes", ww.BytesWritten()),
)
})
}
}
// AuditLog middleware records detailed information about DICOM requests
func AuditLog(logger *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
// We'll extract user info here when auth is implemented
userID := "anonymous"
if id, exists := c.Get("userID"); exists {
userID = id.(string)
}
func AuditLog(logger *zap.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Extract user info (placeholder for now)
userID := "TODO: userID"
path := c.Request.URL.Path
method := c.Request.Method
// Process request
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
next.ServeHTTP(ww, r)
// Process request
c.Next()
// Audit log after request completes
logger.Info("DICOM Access",
zap.String("userID", userID),
zap.String("action", method),
zap.String("resource", path),
zap.Int("status", c.Writer.Status()),
)
}
}
// CORS middleware to handle cross-origin requests
func CORS(allowedOrigins []string) gin.HandlerFunc {
return func(c *gin.Context) {
origin := c.Request.Header.Get("Origin")
// Check if origin is allowed
allowed := false
for _, o := range allowedOrigins {
if o == "*" || o == origin {
allowed = true
break
}
}
// Set CORS headers if allowed
if allowed {
c.Header("Access-Control-Allow-Origin", origin)
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
c.Header("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Accept-Encoding, Authorization")
c.Header("Access-Control-Allow-Credentials", "true")
}
// Handle preflight requests
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
// Audit log after request completes
logger.Info("DICOM Access",
zap.String("userID", userID),
zap.String("action", r.Method),
zap.String("resource", r.URL.Path),
zap.Int("status", ww.Status()),
)
})
}
}