From 71fd910b2945a6567e39829e6669d5d879fb6b48 Mon Sep 17 00:00:00 2001 From: "davila.erdianita.1005" Date: Wed, 13 Aug 2025 13:58:55 +0700 Subject: [PATCH] swagger --- {docs => cmd/api/docs}/docs.go | 0 {docs => cmd/api/docs}/swagger.json | 0 {docs => cmd/api/docs}/swagger.yaml | 0 cmd/api/main.go | 4 +- internal/middleware/error_handler.go | 55 ++++++++++++++++++++++++++++ internal/server/routes.go | 12 +++--- pkg/handlers/retribusi/retribusi.go | 2 +- pkg/models/health.go | 42 +++++++++++++++++++++ 8 files changed, 106 insertions(+), 9 deletions(-) rename {docs => cmd/api/docs}/docs.go (100%) rename {docs => cmd/api/docs}/swagger.json (100%) rename {docs => cmd/api/docs}/swagger.yaml (100%) create mode 100644 internal/middleware/error_handler.go create mode 100644 pkg/models/health.go diff --git a/docs/docs.go b/cmd/api/docs/docs.go similarity index 100% rename from docs/docs.go rename to cmd/api/docs/docs.go diff --git a/docs/swagger.json b/cmd/api/docs/swagger.json similarity index 100% rename from docs/swagger.json rename to cmd/api/docs/swagger.json diff --git a/docs/swagger.yaml b/cmd/api/docs/swagger.yaml similarity index 100% rename from docs/swagger.yaml rename to cmd/api/docs/swagger.yaml diff --git a/cmd/api/main.go b/cmd/api/main.go index 0837b4e..81be56c 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -8,8 +8,8 @@ import ( "os/signal" "syscall" - _ "template_blueprint/docs" - //_ "template_blueprint/cmd/api/docs" + //_ "template_blueprint/docs" + _ "template_blueprint/cmd/api/docs" //_ "template_blueprint/internal/docs" "template_blueprint/internal/server" "time" diff --git a/internal/middleware/error_handler.go b/internal/middleware/error_handler.go new file mode 100644 index 0000000..cb1564c --- /dev/null +++ b/internal/middleware/error_handler.go @@ -0,0 +1,55 @@ +package middleware + +import ( + "net/http" + + "template_blueprint/pkg/models" + + "github.com/gin-gonic/gin" +) + +// ErrorHandler handles errors globally +func ErrorHandler() gin.HandlerFunc { + return func(c *gin.Context) { + c.Next() + + if len(c.Errors) > 0 { + err := c.Errors.Last() + status := http.StatusInternalServerError + + // Determine status code based on error type + switch err.Type { + case gin.ErrorTypeBind: + status = http.StatusBadRequest + case gin.ErrorTypeRender: + status = http.StatusUnprocessableEntity + case gin.ErrorTypePrivate: + status = http.StatusInternalServerError + } + + response := models.ErrorResponse{ + Error: "internal_error", + Message: err.Error(), + Code: status, + } + + c.JSON(status, response) + } + } +} + +// CORS middleware configuration +func CORSConfig() gin.HandlerFunc { + return gin.HandlerFunc(func(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", "*") + c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH") + c.Header("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") + + if c.Request.Method == "OPTIONS" { + c.AbortWithStatus(204) + return + } + + c.Next() + }) +} diff --git a/internal/server/routes.go b/internal/server/routes.go index 793e824..2b4fd40 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -7,6 +7,9 @@ import ( "github.com/gin-gonic/gin" swaggerFiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" + patientHandler "template_blueprint/pkg/handlers/patient" + datapoliklinikHandler "template_blueprint/pkg/handlers/poliklinik" + dataRetribusi "template_blueprint/pkg/handlers/retribusi" //_ "template_blueprint/docs" //swaggerFiles "github.com/swaggo/files" @@ -16,11 +19,7 @@ import ( "net/http" //_ "template_blueprint/cmd/api/docs" "template_blueprint/cmd/web" - //_ "template_blueprint/internal/docs" - _ "template_blueprint/docs" - patientHandler "template_blueprint/pkg/handlers/patient" - datapoliklinikHandler "template_blueprint/pkg/handlers/poliklinik" - dataRetribusi "template_blueprint/pkg/handlers/retribusi" + "time" ) @@ -32,7 +31,8 @@ func (s *Server) RegisterRoutes() http.Handler { AllowHeaders: []string{"Origin", "Content-Type"}, AllowCredentials: true, })) - + //r.Use(middleware.CORSConfig()) + //r.Use(middleware.ErrorHandler()) //r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) r.GET("/", s.HelloWorldHandler) diff --git a/pkg/handlers/retribusi/retribusi.go b/pkg/handlers/retribusi/retribusi.go index 22f9f73..e49386e 100644 --- a/pkg/handlers/retribusi/retribusi.go +++ b/pkg/handlers/retribusi/retribusi.go @@ -8,7 +8,7 @@ import ( "template_blueprint/internal/database" //_ "template_blueprint/cmd/api/docs" //_ "template_blueprint/docs" - _ "template_blueprint/docs" + //_ "template_blueprint/docs" //_ "template_blueprint/internal/docs" connDatabase "template_blueprint/pkg/database/satu_data" "template_blueprint/pkg/models/satu_data" diff --git a/pkg/models/health.go b/pkg/models/health.go new file mode 100644 index 0000000..5f26e4e --- /dev/null +++ b/pkg/models/health.go @@ -0,0 +1,42 @@ +package models + +// HealthResponse represents the health check response +type HealthResponse struct { + Status string `json:"status"` + Timestamp string `json:"timestamp"` + Details map[string]string `json:"details"` +} + +// HelloWorldResponse represents the hello world response +type HelloWorldResponse struct { + Message string `json:"message"` + Version string `json:"version"` +} + +// ErrorResponse represents an error response +type ErrorResponse struct { + Error string `json:"error"` + Message string `json:"message"` + Code int `json:"code"` +} + +// SuccessResponse represents a generic success response +type SuccessResponse struct { + Success bool `json:"success"` + Message string `json:"message"` + Data interface{} `json:"data,omitempty"` +} + +// Pagination represents pagination metadata +type Pagination struct { + Page int `json:"page"` + Limit int `json:"limit"` + Total int `json:"total"` + TotalPages int `json:"total_pages"` +} + +// PaginatedResponse represents a paginated response +type PaginatedResponse struct { + Data interface{} `json:"data"` + Pagination Pagination `json:"pagination"` +}