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,32 +1,42 @@
package handlers
import (
"github.com/gin-gonic/gin"
"encoding/json"
"net/http"
"go.uber.org/zap"
)
// AuthHandler handles authentication-related requests
// AuthHandler handles authentication requests
type AuthHandler struct {
logger *zap.Logger
}
// NewAuthHandler creates a new AuthHandler
// NewAuthHandler creates a new auth handler
func NewAuthHandler(logger *zap.Logger) *AuthHandler {
return &AuthHandler{
logger: logger,
}
}
// Login handles user login
func (h *AuthHandler) Login(c *gin.Context) {
// TODO: Implement login logic
h.logger.Info("Login endpoint hit")
c.JSON(200, gin.H{"message": "Login not implemented"})
// Login handles user login - placeholder for future implementation
func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
response := map[string]string{
"message": "Login functionality will be implemented in a future version",
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}
// Logout handles user logout
func (h *AuthHandler) Logout(c *gin.Context) {
// TODO: Implement logout logic
h.logger.Info("Logout endpoint hit")
c.JSON(200, gin.H{"message": "Logout not implemented"})
// Logout handles user logout - placeholder for future implementation
func (h *AuthHandler) Logout(w http.ResponseWriter, r *http.Request) {
response := map[string]string{
"message": "Logout functionality will be implemented in a future version",
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}

View File

@@ -4,9 +4,10 @@ import (
"fmt"
"io"
"net/http"
"strings"
"devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/proxy"
"github.com/gin-gonic/gin"
"github.com/go-chi/chi/v5"
"go.uber.org/zap"
)
@@ -25,31 +26,49 @@ func NewDicomHandler(client *proxy.Client, logger *zap.Logger) *DicomHandler {
}
// 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)
func (h *DicomHandler) ForwardRequest(w http.ResponseWriter, r *http.Request) {
// Get the path after /dicomWeb
urlPath := chi.URLParam(r, "*")
// If the URL parameter is empty, try to extract it from the URL path
if urlPath == "" {
// Remove /dicomWeb prefix from the URL
prefix := "/dicomWeb"
if strings.HasPrefix(r.URL.Path, prefix) {
urlPath = r.URL.Path[len(prefix):]
}
}
h.logger.Debug("Forwarding request",
zap.String("path", path),
zap.String("method", c.Request.Method),
zap.String("type", requestType),
zap.String("path", urlPath),
zap.String("method", r.Method),
zap.String("url", r.URL.String()),
)
// Read request body if present
var bodyBytes []byte
if c.Request.Body != nil && c.Request.ContentLength > 0 {
if r.Body != nil && r.ContentLength > 0 {
var err error
bodyBytes, err = io.ReadAll(c.Request.Body)
bodyBytes, err = io.ReadAll(r.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"})
http.Error(w, "Failed to read request body", http.StatusInternalServerError)
return
}
}
// Copy query parameters
queryParams := r.URL.Query().Encode()
if queryParams != "" {
if !strings.HasPrefix(urlPath, "/") {
urlPath = "/" + urlPath
}
urlPath = urlPath + "?" + queryParams
}
// Get request headers
headers := make(map[string]string)
for k, v := range c.Request.Header {
for k, v := range r.Header {
if len(v) > 0 {
headers[k] = v[0]
}
@@ -57,45 +76,25 @@ func (h *DicomHandler) ForwardRequest(c *gin.Context) {
// Forward the request to Healthcare API
response, err := h.client.ForwardRequest(
c.Request.Context(),
c.Request.Method,
requestType,
path,
r.Context(),
r.Method,
urlPath,
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)})
http.Error(w, fmt.Sprintf("Request failed: %v", err), http.StatusInternalServerError)
return
}
// Set response headers
for k, v := range response.Headers {
c.Header(k, v)
w.Header().Set(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"
w.WriteHeader(response.StatusCode)
w.Write(response.Body)
}

View File

@@ -1,12 +1,19 @@
package handlers
import (
"encoding/json"
"net/http"
"github.com/gin-gonic/gin"
"time"
)
// HealthCheck is a simple health check handler
func HealthCheck(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})
// HealthCheck provides a simple health check endpoint
func HealthCheck(w http.ResponseWriter, r *http.Request) {
response := map[string]interface{}{
"status": "ok",
"timestamp": time.Now().Format(time.RFC3339),
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}