init
This commit is contained in:
32
internal/api/handlers/auth.go
Normal file
32
internal/api/handlers/auth.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// AuthHandler handles authentication-related requests
|
||||
type AuthHandler struct {
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewAuthHandler creates a new AuthHandler
|
||||
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"})
|
||||
}
|
||||
|
||||
// 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"})
|
||||
}
|
||||
101
internal/api/handlers/dicom.go
Normal file
101
internal/api/handlers/dicom.go
Normal file
@@ -0,0 +1,101 @@
|
||||
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"
|
||||
}
|
||||
12
internal/api/handlers/healthcheck.go
Normal file
12
internal/api/handlers/healthcheck.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// HealthCheck is a simple health check handler
|
||||
func HealthCheck(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||
}
|
||||
94
internal/api/middleware/logging.go
Normal file
94
internal/api/middleware/logging.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"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
|
||||
|
||||
// Process request
|
||||
c.Next()
|
||||
|
||||
// Calculate request time
|
||||
latency := time.Since(start)
|
||||
|
||||
// Get status
|
||||
status := c.Writer.Status()
|
||||
|
||||
// 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()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
path := c.Request.URL.Path
|
||||
method := c.Request.Method
|
||||
|
||||
// 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()
|
||||
}
|
||||
}
|
||||
72
internal/api/routes.go
Normal file
72
internal/api/routes.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"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"
|
||||
"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"
|
||||
"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)
|
||||
}
|
||||
|
||||
// Initialize the router
|
||||
r := gin.New()
|
||||
|
||||
// Add middleware
|
||||
r.Use(gin.Recovery())
|
||||
r.Use(middleware.Logger(logger))
|
||||
r.Use(middleware.CORS(cfg.AllowedOrigins))
|
||||
|
||||
// Setup health check
|
||||
r.GET("/health", handlers.HealthCheck)
|
||||
|
||||
// Initialize Google auth client
|
||||
googleAuth, err := auth.NewGoogleClient(cfg.Google.CredentialsPath)
|
||||
if err != nil {
|
||||
logger.Fatal("Failed to initialize Google auth client", zap.Error(err))
|
||||
}
|
||||
|
||||
// Initialize Healthcare API client
|
||||
healthcareClient := proxy.NewClient(googleAuth, cfg.Google)
|
||||
|
||||
// DICOM Web routes
|
||||
dicomGroup := r.Group("/dicomWeb")
|
||||
{
|
||||
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)
|
||||
}
|
||||
|
||||
// Future auth routes for doctors
|
||||
authGroup := r.Group("/auth")
|
||||
{
|
||||
// Auth handlers will be implemented later
|
||||
authHandler := handlers.NewAuthHandler(logger)
|
||||
authGroup.POST("/login", authHandler.Login)
|
||||
authGroup.POST("/logout", authHandler.Logout)
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
Reference in New Issue
Block a user