swagger
This commit is contained in:
@@ -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"
|
||||
|
||||
55
internal/middleware/error_handler.go
Normal file
55
internal/middleware/error_handler.go
Normal file
@@ -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()
|
||||
})
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
42
pkg/models/health.go
Normal file
42
pkg/models/health.go
Normal file
@@ -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"`
|
||||
}
|
||||
Reference in New Issue
Block a user