package handlers import ( "fmt" "io" "net/http" "devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/proxy" "github.com/gin-gonic/gin" "go.uber.org/zap" ) // DicomHandler handles DICOM Web requests type DicomHandler struct { client *proxy.Client logger *zap.Logger } // NewDicomHandler creates a new DICOM handler func NewDicomHandler(client *proxy.Client, logger *zap.Logger) *DicomHandler { return &DicomHandler{ client: client, logger: logger, } } // ForwardRequest forwards the request to Google Healthcare API func (h *DicomHandler) ForwardRequest(c *gin.Context) { path := c.Param("path") requestType := getRequestType(c.Request.URL.Path) h.logger.Debug("Forwarding request", zap.String("path", path), zap.String("method", c.Request.Method), zap.String("type", requestType), ) // Read request body if present var bodyBytes []byte if c.Request.Body != nil && c.Request.ContentLength > 0 { var err error bodyBytes, err = io.ReadAll(c.Request.Body) if err != nil { h.logger.Error("Failed to read request body", zap.Error(err)) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read request body"}) return } } // Get request headers headers := make(map[string]string) for k, v := range c.Request.Header { if len(v) > 0 { headers[k] = v[0] } } // Forward the request to Healthcare API response, err := h.client.ForwardRequest( c.Request.Context(), c.Request.Method, requestType, path, headers, bodyBytes, ) if err != nil { h.logger.Error("Request forwarding failed", zap.Error(err)) c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Request failed: %v", err)}) return } // Set response headers for k, v := range response.Headers { c.Header(k, v) } // Set status and write response body c.Status(response.StatusCode) c.Writer.Write(response.Body) } // getRequestType determines if the request is WADO, QIDO or STOW func getRequestType(path string) string { if len(path) < 9 { return "unknown" } pathComponent := path[9:] // Remove "/dicomWeb/" if len(pathComponent) >= 4 && pathComponent[:4] == "wado" { return "wado" } else if len(pathComponent) >= 4 && pathComponent[:4] == "qido" { return "qido" } else if len(pathComponent) >= 4 && pathComponent[:4] == "stow" { return "stow" } return "unknown" }