connected-to-google
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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()),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +1,44 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/config"
|
||||
"devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/api/handlers"
|
||||
"devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/api/middleware"
|
||||
apiMiddleware "devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/api/middleware"
|
||||
"devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/auth"
|
||||
"devone.aplikasi.web.id/gitea/mario/go-ohif-proxy/internal/proxy"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/go-chi/cors"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// TODO: Refactor dari gin dengan chi
|
||||
|
||||
// SetupRouter configures and returns the API router
|
||||
func SetupRouter(cfg *config.Config, logger *zap.Logger) *gin.Engine {
|
||||
// Set Gin mode
|
||||
if cfg.LogLevel == "debug" {
|
||||
gin.SetMode(gin.DebugMode)
|
||||
} else {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
func SetupRouter(cfg *config.Config, logger *zap.Logger) http.Handler {
|
||||
r := chi.NewRouter()
|
||||
|
||||
// Initialize the router
|
||||
r := gin.New()
|
||||
// Built-in Chi middleware
|
||||
r.Use(middleware.RequestID)
|
||||
r.Use(middleware.RealIP)
|
||||
r.Use(middleware.Recoverer)
|
||||
r.Use(middleware.StripSlashes)
|
||||
|
||||
// Add middleware
|
||||
r.Use(gin.Recovery())
|
||||
r.Use(middleware.Logger(logger))
|
||||
r.Use(middleware.CORS(cfg.AllowedOrigins))
|
||||
// Custom middleware
|
||||
r.Use(apiMiddleware.Logger(logger))
|
||||
|
||||
// CORS middleware
|
||||
r.Use(cors.Handler(cors.Options{
|
||||
AllowedOrigins: cfg.AllowedOrigins,
|
||||
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
||||
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
|
||||
ExposedHeaders: []string{"Link"},
|
||||
AllowCredentials: true,
|
||||
MaxAge: 300,
|
||||
}))
|
||||
|
||||
// Setup health check
|
||||
r.GET("/health", handlers.HealthCheck)
|
||||
r.Get("/health", handlers.HealthCheck)
|
||||
|
||||
// Initialize Google auth client
|
||||
googleAuth, err := auth.NewGoogleClient(cfg.Google.CredentialsPath)
|
||||
@@ -41,32 +49,24 @@ func SetupRouter(cfg *config.Config, logger *zap.Logger) *gin.Engine {
|
||||
// Initialize Healthcare API client
|
||||
healthcareClient := proxy.NewClient(googleAuth, cfg.Google)
|
||||
|
||||
// DICOM Web routes
|
||||
dicomGroup := r.Group("/dicomWeb")
|
||||
{
|
||||
// DICOM Web routes - simplified approach
|
||||
r.Route("/dicomWeb", func(r chi.Router) {
|
||||
// Add audit logging middleware to DICOM routes
|
||||
r.Use(apiMiddleware.AuditLog(logger))
|
||||
|
||||
// Create single handler for all DICOM requests
|
||||
dicomHandler := handlers.NewDicomHandler(healthcareClient, logger)
|
||||
|
||||
// Add audit logging middleware to DICOM routes
|
||||
dicomGroup.Use(middleware.AuditLog(logger))
|
||||
|
||||
// WADO routes
|
||||
dicomGroup.Any("/wado/*path", dicomHandler.ForwardRequest)
|
||||
|
||||
// QIDO routes
|
||||
dicomGroup.Any("/qido/*path", dicomHandler.ForwardRequest)
|
||||
|
||||
// STOW routes
|
||||
dicomGroup.Any("/stow/*path", dicomHandler.ForwardRequest)
|
||||
}
|
||||
// Catch all routes under /dicomWeb and forward them
|
||||
r.HandleFunc("/*", dicomHandler.ForwardRequest)
|
||||
})
|
||||
|
||||
// Future auth routes for doctors
|
||||
authGroup := r.Group("/auth")
|
||||
{
|
||||
// Auth handlers will be implemented later
|
||||
r.Route("/auth", func(r chi.Router) {
|
||||
authHandler := handlers.NewAuthHandler(logger)
|
||||
authGroup.POST("/login", authHandler.Login)
|
||||
authGroup.POST("/logout", authHandler.Logout)
|
||||
}
|
||||
r.Post("/login", authHandler.Login)
|
||||
r.Post("/logout", authHandler.Logout)
|
||||
})
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user